我一直在使用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);
}
}
列表项.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;
}
}
您可以在填充视图(...(中的列表中设置这样的点击事件
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);
}