简体   繁体   English

通过可点击的 ListView 将 Firebase 数据从一个活动传递到另一个活动

[英]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.在我尝试将 firebase 数据从列表视图传递到单独的活动中时,我在最初将数据放入列表时遇到了以下错误。

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.当我将列表从类型字符串更改为类型 ReportInformation 时出现了问题,这是在两个活动之间传递信息所必需的。 One an item is selected within the listView the data referenced should be passed into another activity (progressReports.activity)在 listView 中选择一个项目,引用的数据应该传递到另一个活动(progressReports.activity)

My list_view.java我的 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.在声明列表时,您正在向列表传递一个字符串,而不是一个ReportInformation对象。 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将Firebase数据从一项活动传递到另一项活动,即ListView的onClick - Passing firebase data from one activty to another, onClick of ListView 将数据从列表视图传递到另一个活动的edittext - Passing data from listview to edittext of another activity 将listView数据从一个活动发送到另一个活动 - Send listView data from one activity to another 将Firebase数据从ListView传递到单独的活动editText - Passing firebase data from ListView to seperate activity editText 将数据从一个活动传递到另一个活动,然后打印 - Passing data from one activity to another and then printing 将数据从 Activity 传递到另一个 - Passing data from Activity to another one 单击ListView项时将_id从一个活动传递到另一个活动 - Passing an _id from one activity to another when ListView item is clicked 将数据从listview中的单击项传递到另一个活动时出错 - Error while passing data from clicked item in listview to another activity 如何使用setOnClickListener从一个活动到另一个活动获取ListView数据 - How to get ListView data from one activity to another using setOnClickListener Java将数据从一个片段的Lis​​tView传递到另一个片段 - Java Passing Data From One Fragment's ListView To Another Fragment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM