在尝试将Firebase数据从列表视图传递到单独的活动中时,我遇到了以下错误,最初将数据放入列表中。
对于此问题的任何帮助,我们将不胜感激。
当我将列表从字符串类型更改为ReportInformation类型时,出现了问题,这是在两个活动之间传递信息所必需的。在列表中选择一个项目查看引用的数据应传递到另一个活动(progressReports.activity)
我的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) {
}
});
}
答案 0 :(得分:1)
您正在向列表传递一个String,而不是一个声明列表的ReportInformation
对象。为了解决这个问题,您应该将数据作为该类的对象。因此,请更改以下代码行:
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);
到
ReportInformation reportInformation = ds.getValue(ReportInformation.class);
list.add(reportInformation);