我有一个带有自定义适配器的listView,它在我的列表视图的每个项目上都包含文本+按钮。 我正在从firebase检索文本没有问题,但我希望如果用户点击了一个项目的按钮,它将写在数据库上该用户点击了该项目。
谢谢如果有人能帮助我,给我一个想法或发送一个链接。
我有一个想法,就是检索我放在listView上的消息的索引(rec_1,rec_2,rec_3等),如果用户单击按钮,我就在数据库上写index-userid:1或类似的东西,但是我如何将索引值隐藏在listView的每个项目上,以便在单击按钮时检索呢? 只是一个想法,如果有更好的选择,请告诉我!
我的代码:
activity_recados_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textRecados"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true" />
<Button
android:id="@+id/buttonRecadoVisto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textRecados"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:text="Visto" />
</RelativeLayout>
RecadoScustomAdapter.java
public class RecadosCustomAdapter extends BaseAdapter implements ListAdapter {
private ArrayList<String> recadosList = new ArrayList<String>();
private Context context;
public RecadosCustomAdapter(ArrayList<String> recadosList, Context context) {
this.recadosList = recadosList;
this.context = context;
}
@Override
public int getCount() {
return recadosList.size();
}
@Override
public Object getItem(int pos) {
return recadosList.get(pos);
}
@Override
public long getItemId(int position) {
//return recadosList.get(pos).getId();
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.activity_recados_item, null);
}
//Handle TextView and display string from your list
TextView listItemText = (TextView)view.findViewById(R.id.textRecados);
listItemText.setText(recadosList.get(position));
//Handle buttons and add onClickListeners
Button buttonVisto = (Button)view.findViewById(R.id.buttonRecadoVisto);
buttonVisto.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
//do something
}
});
return view;
}
}
recadosactivity.java
ValueEventListener valueEventListener = myRef.limitToLast(15).addValueEventListener(new ValueEventListener() {
ArrayList<String> recadosList = new ArrayList<String>();
RecadosCustomAdapter adapter = new RecadosCustomAdapter(recadosList, RecadosActivity.this);
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
recadosList.clear();
adapter.notifyDataSetChanged();
for (DataSnapshot ds : dataSnapshot.getChildren()) {
RecadosInformation rInfo = new RecadosInformation();
rInfo.setData(ds.getValue(RecadosInformation.class).getData());
rInfo.setMensagem(ds.getValue(RecadosInformation.class).getMensagem());
Log.d(TAG, "showData: Data: " + rInfo.getData());
Log.d(TAG, "showData: Mensagem: " + rInfo.getMensagem());
recadosList.add(rInfo.getData() + "\n" + rInfo.getMensagem() + "\n");
}
Collections.reverse(recadosList);
recados.setAdapter(adapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
如果有人能帮我的话,谢谢!
我认为您应该这样做:firebaseDatabase.getInstance().getReference(“your_node”).child(“child”).setValue(yourvalue);
我想你明白了吧? 点击此处获取更多信息:https://firebase.google.com/docs/database/android/read-and-write