简体   繁体   中英

Passing firebase data from one activity to another via clickable ListView

In my attempt to pass firebase data from a list view into a separate activity I have come across the following error with putting the data into the list initially.

Any assistance is greatly appreciated with this issue.

The issue arose when I changed the list from type string to type ReportInformation, this is required to pass the information between the two activities. One an item is selected within the listView the data referenced should be passed into another activity (progressReports.activity)

My list_view.java

public class list_view extends AppCompatActivity {

private ArrayAdapter<ReportInformation> 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<ReportInformation> 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 i, 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) {
                            ReportInformation reportInformation = arrayAdapter.getItem(i);
                            Intent intent = new Intent(list_view.this, ProgressReports.class);
                            intent.putExtra("object", reportInformation);
                            startActivity(intent);
                        }
                    });
                }


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

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

    progressList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> adapterView, View view, final int j, long l) {
            final DatabaseReference delete = FirebaseDatabase.getInstance().getReference("ProgressReports/" + uid + "/" + id.get(j));
            final AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setMessage("Are you sure you want to delete?");
            builder.setCancelable(true);
            builder.setPositiveButton(
                    "yes",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            delete.removeValue();
                            Toast.makeText(list_view.this, "Report deleted", Toast.LENGTH_SHORT).show();
                            list.remove(j);
                            arrayAdapter.notifyDataSetChanged();
                        }
                    });
            builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.cancel();
                }
            });
            builder.show();
            return true;
        }
    });


    searchBarProgress.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            (list_view.this).arrayAdapter.getFilter().filter(charSequence);

        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    });
}

} 错误

You are passing to your list a String and not a ReportInformation object as the list is declared. To solve this, you should get the data as an object of that class. So please change the following lines of code:

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);

to

ReportInformation reportInformation = ds.getValue(ReportInformation.class);
list.add(reportInformation);

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