In this case I want to get the firestore id which I added into array for each time the Document snapshot run. for example: onclick the first item of ListView and Toast the correct doc id of the data in firestore.
In my code it can only get the last one of the document id when I onClick there. By the way idlv is working well if I put it into snapshot. getString and display it but I just want to get the id and intent to next page so I can load the data of the doc.
sorry for the bad english and newbie coding(its really messy I know)
problem will might be in the itemOnclick function ("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));
}
});
}
You shouldn't access idlv like that will always give you last item id. what you should Ideally do is as follows
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();
}
});