我有一个带有自定义适配器的 ListView ,其中包含 ListView 的每个项目上的文本+按钮。我可以毫无问题地从 firebase 检索文本,但我希望如果用户单击某个项目的按钮,它将在数据库中写入该用户单击该项目的信息。
如果有人可以帮助我、给我一个想法或发送链接,谢谢。
我有一个想法,检索我放在 listView 上的消息的索引(rec_1、rec_2、rec_3 等),如果用户单击按钮,我会在数据库索引上写入 - 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
编辑你会知道在 getView 方法中,列表中的每个项目都会调用该方法,因此当你单击按钮时,使用intposition来获取项目的特定位置
做:
@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) {
//upload the index which is position
}
});
return view;
}
编辑 2 使用 push().getKey() 方法在 Firebase 数据库中为每条消息提供一个 ID。所以在你的 getView() 中做:
String id = yourlist.get(position).getId();
然后用它做你想做的事。
关于java - 在 Firebase 数据库上写入用户单击 listView 项目上的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62305830/