简体   繁体   中英

Passing firebase data from one activty to another, onClick of ListView

I'm having some trouble passing data from one activity to another, onItem click I want to pass the value of the item clicked to another activity, in the following code it only the final piece of data as opposed to the item clicked, any help greatly appreciated.

            public class list_view extends AppCompatActivity {

private ArrayAdapter<String> arrayAdapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_view);

    final String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
    DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    final DatabaseReference uidRef = rootRef.child("ProgressReports").child(uid);
    final List<String> list = new ArrayList<>();
    final ArrayList<String> id = new ArrayList<>();
    final Context context = this;
    EditText searchBarProgress = findViewById(R.id.searchBarProgress);
    arrayAdapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, list);

    String key = uidRef.push().getKey();
    ReportInformation report = new ReportInformation();
    report.setTitle("title");
    report.setContent("content");
    report.setKey(key);
    uidRef.child(key).setValue(report);


    ValueEventListener eventListener = new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for (DataSnapshot ds : dataSnapshot.getChildren()) {
                id.add(ds.getKey());
                String title = ds.child("title").getValue(String.class);
                String content = ds.child("content").getValue(String.class);
                String timestamp = ds.child("timestamp").getValue(String.class);
                list.add(title + "\n" + timestamp + "\n" + content);
                Log.d("TAG", title);
            }
            ListView listView = findViewById(R.id.progressList);
            listView.setAdapter(arrayAdapter);

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    };
    uidRef.addListenerForSingleValueEvent(eventListener);

    final ListView progressList = findViewById(R.id.progressList);

    progressList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int x, long l) {

            ValueEventListener eventListener = new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    ArrayList<ReportInformation> list = new ArrayList<>();
                    for (DataSnapshot ds : dataSnapshot.getChildren()) {
                        ReportInformation report = ds.getValue(ReportInformation.class);
                        list.add(report);
                    }
                    Intent intent = new Intent(list_view.this, ProgressReports.class);
                    ListView listView = findViewById(R.id.progressList);
                    listView.setAdapter(arrayAdapter);
                    startActivity(intent);
                    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                        }
                    });
                }


                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            };
            uidRef.addListenerForSingleValueEvent(eventListener);
        }
    });

To show what I mean, regardless of which button I press in the following listview, the data passed over is Test 4

列表显示

测试4通过

Below is my firebase structure

火基结构

Model class for objects

public class ReportInformation {
private String title;
private String content;
private String timestamp;
private String key;

public ReportInformation(){

}

public String getTitle() {
    return title;
}

public String getKey() {
    return key;
}

public void setKey(String key) {
    this.key = key;
}

public void setTitle(String title) {
    this.title = title;
}

public String getContent() {
    return content;
}

public void setContent(String content) {
    this.content = content;
}

public String getTimestamp() {
    return timestamp;
}

public void setTimestamp(String timestamp) {
    this.timestamp = timestamp;
}
}

Getting the following error when trying to do that

错误

New error 新错误

添加到列表时出错

To solve this, you need first to implement Serializable interface like this:

ReportInformation implements Serializable {}

So to pass your entire ReportInformation object to the next activity, you should use the following lines of code:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        ReportInformation reportInformation = arrayAdapter.getItem(i);
        Intent intent = new Intent(list_view.this, ProgressReports.class);
        intent.putExtra("object", reportInformation);
        startActivity(intent);
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM