Android 工作室使用 onItemClickListener (Firestore id) 获取 ListView 数组项值
Android studio getting ListView array item value with onItemClickListener (Firestore id)
(click for image)I think it will be better if I contain an image, all of these data are in firestore. I want to display the correct doc id when I click each of them, right now it can only display the last doc id which is the earlyiest data since I orderBy in time
在这种情况下,我想获取我为每次文档快照添加到数组中的 firestore id 运行。例如:点击 ListView 的第一项并 Toast firestore 中数据的正确文档 ID。
在我的代码中,当我点击那里时,它只能获得文档 ID 的最后一个。顺便说一句,如果我将它放入快照,idlv 运行良好。 getString 并显示它,但我只想获取下一页的 ID 和意图,以便我可以加载文档的数据。
抱歉英语不好和新手编码(我知道它真的很乱)
问题可能出在 itemOnclick 函数中 ("id is"+idlv)
public class announce_main extends AppCompatActivity {
Button button7;
ListView lv1;
ArrayList<itemAnnounce> ar = new ArrayList<>();
String userID,idlv;
FirebaseAuth firebaseAuth;
FirebaseFirestore firebaseFirestore;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.announce_listview);
button7 = findViewById(R.id.button7);
// item = (item) getIntent().getSerializableExtra("item");
firebaseAuth = FirebaseAuth.getInstance();
firebaseFirestore = FirebaseFirestore.getInstance();
userID = firebaseAuth.getCurrentUser().getUid();
firebaseFirestore.collection("announcement").orderBy("createdAt", Query.Direction.DESCENDING).addSnapshotListener((documentSnapshots, error) -> {
ar.clear();
for (DocumentSnapshot snapshot : documentSnapshots){
idlv = snapshot.getId();
Timestamp timestamp = (Timestamp) snapshot.getData().get("createdAt");
Date date = timestamp.toDate();
String date2 = date.toString();
String artLv = snapshot.getString("artLevel");
Log.d("TAG", " artLevel is" + artLv);
if(artLv.equals("緊急公告")){
ar.add(new itemAnnounce(R.drawable.alarm, snapshot.getString("title"),"by "+snapshot.getString("createdBy")+ " 類型:"+snapshot.getString("artLevel"),date2,snapshot.getString("content"),idlv));
}else if(artLv.equals("議題公告")){
ar.add(new itemAnnounce(R.drawable.newspaper, snapshot.getString("title"),"by "+snapshot.getString("createdBy")+ " 類型:"+snapshot.getString("artLevel"),date2,snapshot.getString("content"),idlv));
}else{
ar.add(new itemAnnounce(R.drawable.teatime, snapshot.getString("title"),"by "+snapshot.getString("createdBy")+ " 類型:"+snapshot.getString("artLevel"),date2,snapshot.getString("content"),idlv));
}
}
adapterAnnounce adapterAnnounce = new adapterAnnounce(getApplicationContext(), R.layout.list_row_announce, ar);
adapterAnnounce.notifyDataSetChanged();
lv1.setAdapter(adapterAnnounce);
lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(announce_main.this, "id is"+ idlv, Toast.LENGTH_SHORT).show();
}
});
});
lv1 = findViewById(R.id.lv2);
button7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(announce_main.this, HomePageActivity.class));
}
});
}
您不应该像那样访问 idlv,它总是会为您提供最后一个项目 ID。理想情况下你应该做的如下
lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selectedObj = ar.get(postion) // this will get you selected obj of itemAnnounce
String selectedId = selectedObj.get("<property name of itemAnnounce where you are assigning idlv>")
Toast.makeText(announce_main.this, "id is"+ selectedId, Toast.LENGTH_SHORT).show();
}
});
(click for image)I think it will be better if I contain an image, all of these data are in firestore. I want to display the correct doc id when I click each of them, right now it can only display the last doc id which is the earlyiest data since I orderBy in time
在这种情况下,我想获取我为每次文档快照添加到数组中的 firestore id 运行。例如:点击 ListView 的第一项并 Toast firestore 中数据的正确文档 ID。
在我的代码中,当我点击那里时,它只能获得文档 ID 的最后一个。顺便说一句,如果我将它放入快照,idlv 运行良好。 getString 并显示它,但我只想获取下一页的 ID 和意图,以便我可以加载文档的数据。
抱歉英语不好和新手编码(我知道它真的很乱)
问题可能出在 itemOnclick 函数中 ("id is"+idlv)
public class announce_main extends AppCompatActivity {
Button button7;
ListView lv1;
ArrayList<itemAnnounce> ar = new ArrayList<>();
String userID,idlv;
FirebaseAuth firebaseAuth;
FirebaseFirestore firebaseFirestore;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.announce_listview);
button7 = findViewById(R.id.button7);
// item = (item) getIntent().getSerializableExtra("item");
firebaseAuth = FirebaseAuth.getInstance();
firebaseFirestore = FirebaseFirestore.getInstance();
userID = firebaseAuth.getCurrentUser().getUid();
firebaseFirestore.collection("announcement").orderBy("createdAt", Query.Direction.DESCENDING).addSnapshotListener((documentSnapshots, error) -> {
ar.clear();
for (DocumentSnapshot snapshot : documentSnapshots){
idlv = snapshot.getId();
Timestamp timestamp = (Timestamp) snapshot.getData().get("createdAt");
Date date = timestamp.toDate();
String date2 = date.toString();
String artLv = snapshot.getString("artLevel");
Log.d("TAG", " artLevel is" + artLv);
if(artLv.equals("緊急公告")){
ar.add(new itemAnnounce(R.drawable.alarm, snapshot.getString("title"),"by "+snapshot.getString("createdBy")+ " 類型:"+snapshot.getString("artLevel"),date2,snapshot.getString("content"),idlv));
}else if(artLv.equals("議題公告")){
ar.add(new itemAnnounce(R.drawable.newspaper, snapshot.getString("title"),"by "+snapshot.getString("createdBy")+ " 類型:"+snapshot.getString("artLevel"),date2,snapshot.getString("content"),idlv));
}else{
ar.add(new itemAnnounce(R.drawable.teatime, snapshot.getString("title"),"by "+snapshot.getString("createdBy")+ " 類型:"+snapshot.getString("artLevel"),date2,snapshot.getString("content"),idlv));
}
}
adapterAnnounce adapterAnnounce = new adapterAnnounce(getApplicationContext(), R.layout.list_row_announce, ar);
adapterAnnounce.notifyDataSetChanged();
lv1.setAdapter(adapterAnnounce);
lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(announce_main.this, "id is"+ idlv, Toast.LENGTH_SHORT).show();
}
});
});
lv1 = findViewById(R.id.lv2);
button7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(announce_main.this, HomePageActivity.class));
}
});
}
您不应该像那样访问 idlv,它总是会为您提供最后一个项目 ID。理想情况下你应该做的如下
lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selectedObj = ar.get(postion) // this will get you selected obj of itemAnnounce
String selectedId = selectedObj.get("<property name of itemAnnounce where you are assigning idlv>")
Toast.makeText(announce_main.this, "id is"+ selectedId, Toast.LENGTH_SHORT).show();
}
});