iT邦幫忙

0

android studio search firebase database

  • 分享至 

  • xImage

我想要搜尋firebase裡的資料,但是照以下的程式碼跑完,抓不到資料,想請問是哪裡出問題,還是要加什麼程式??

謝謝。

Mainactivity.java

public class MainActivity extends AppCompatActivity {

    private EditText mSearchField;
    private Button mSearchBtn;

    private RecyclerView mResultList;

    private DatabaseReference mUserDatabase;

    @Override
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mUserDatabase = FirebaseDatabase.getInstance().getReference("contacts");


        mSearchField = (EditText) findViewById(R.id.search_field);
        mSearchBtn = (Button) findViewById(R.id.search_btn);

        mResultList = (RecyclerView) findViewById(R.id.result_list);
        mResultList.setHasFixedSize(true);
        mResultList.setLayoutManager(new LinearLayoutManager(this));

        mSearchBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String searchText = mSearchField.getText().toString();

                firebaseUserSearch(searchText);

            }
        });

    }

    private void firebaseUserSearch(String searchText) {

        Toast.makeText(MainActivity.this, "Started Search", Toast.LENGTH_LONG).show();

        Query firebaseSearchQuery = mUserDatabase.orderByChild("name").startAt(searchText).endAt(searchText + "\uf8ff");
        FirebaseRecyclerOptions<Users> options = new FirebaseRecyclerOptions.Builder<Users>()
                                .setQuery(firebaseSearchQuery, Users.class)
                                .build();

        FirebaseRecyclerAdapter<Users, UsersViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Users, UsersViewHolder>(options) {
            @NonNull
            @Override
            public UsersViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

                    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.activity_users, viewGroup, false);


                    return new UsersViewHolder(view);
                }
                @Override 
                protected void onBindViewHolder(@NonNull UsersViewHolder holder, int position, @NonNull Users model) {
                    holder.setDetails(getApplicationContext(), model.getName(), model.getUrl(), model.getScore(), model.getWhere());
                }


                };

                mResultList.setAdapter(firebaseRecyclerAdapter);

        }


        // View Holder Class

    public static class UsersViewHolder extends RecyclerView.ViewHolder {

        View mView;

        public UsersViewHolder(View itemView) {
            super(itemView);

            mView = itemView;

        }
        public void setDetails(Context ctx, String userName, String userurl, String userScore,String userwhere){

            TextView user_name = (TextView) mView.findViewById(R.id.textView);
            TextView user_url = (TextView) mView.findViewById(R.id.textView2);
            TextView user_score = (TextView) mView.findViewById(R.id.textView3);
            TextView user_where= (TextView) mView.findViewById(R.id.textView4);

            user_name.setText(userName);
            user_url.setText(userurl);
            user_score.setText(userScore);
            user_where.setText(userwhere);


        }
    }
}


Users.java

public class Users{

    private String name, score, url,where;

    public Users(){

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getScore() {
        return score;
    }

    public void setScore(String score) {
        this.score = score;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url= url;
    }

    public String getWhere() {
        return where;
    }

    public void setWhere(String where) {
        this.where = where;
    }

    public Users(String name, String score, String url,String where) {
        this.name = name;
        this.score = score;
        this.url = url;
        this.where=where;
    }
}

教學範本:https://www.youtube.com/watch?v=b_tz8kbFUsU

dragonH iT邦超人 5 級 ‧ 2019-08-03 12:40:44 檢舉
看 log 有無錯誤

那個不是裝飾用的
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答