我在将数据从一个活动传递到另一个活动时遇到了麻烦,我想将单击的项目的值传递给另一个活动,在下面的代码中,它仅是最后的数据,而不是单击的项目,非常感谢您的帮助。
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);
}
});
为了显示我的意思,无论我在下面的列表视图中按下哪个按钮,传递的数据都是Test 4
下面是我的Firebase结构
对象的模型类
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;
}
}
尝试执行此操作时出现以下错误
答案 0 :(得分:2)
要解决此问题,您首先需要实现Serializable
接口,如下所示:
ReportInformation implements Serializable {}
因此,要将整个ReportInformation
对象传递到下一个活动,您应该使用以下代码行:
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);
}
});