如何在Firebase-ui ListVeiw中实现点击侦听器?

时间:2018-06-30 09:59:57

标签: android firebase-realtime-database firebaseui

我一直在使用this link中可用的Firebase-ui提供的ListVeiw实现。 我打算为每行放置一个OnClick侦听器。当按下它时,应该会改变背景色,但是我找不到任何方法可以完成它。任何帮助都会很棒。

-已完成-

活动

public class AddActivity extends AppCompatActivity {

//Databse refernce
FirebaseDatabase database= FirebaseDatabase.getInstance();
ListView listView;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add);
    listView = findViewById(R.id.listveiw);


    Query query = FirebaseDatabase.getInstance()
            .getReference("l")
            .child("p")
            .limitToLast(50);


    FirebaseListOptions<ListItem> options = new FirebaseListOptions.Builder<ListItem>()
            .setLayout(R.layout.listitem)
            .setQuery(query, ListItem.class)
            .build();
    FirebaseListAdapter<ListItem> adapter = new FirebaseListAdapter<ListItem>(options) {


        @Override
        protected void populateView(View v, ListItem model, int position) {
        String s = model.getName();
       TextView a= v.findViewById(R.id.textView2);
       a.setText(s);
       s=model.getWatch();
       a= v.findViewById(R.id.textView3);
       a.setText(s);
       ImageButton b= v.findViewById(R.id.imageButton);
        }
    };
    listView.setAdapter(adapter);


}
}

ListItem.class

public class ListItem {
private String name;
private String watch;

public ListItem(){}

public  ListItem(String name,String watch){
    this.name=name;
    this.watch=watch;
}

public String getName() {
    return name;
}

public String getWatch() {
    return watch;
}

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

public void setWatch(String watch) {
    this.watch = watch;
}
}

1 个答案:

答案 0 :(得分:1)

您可以在populateView(...)内的listView中设置这样的click事件

mView.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        Intent intent = new Intent(this, YourActivity.class);
                                        intent.putExtra(TITLE, title);
                                        intent.putExtra(CONTENT, content);
                                        intent.putExtra(KEY, key);
                                        startActivity(intent);

                                    }