如何获取通过 FirebaseListAdapter 填充的 ListView 项的值
How to get value of a ListView item, populated through FirebaseListAdapter
我有一个 ListView
,我使用 FirebaseListAdapter
填充了它。现在我希望 OnItemClickListener
在 Toast
.
中显示单击的项目的值(项目上显示的文本)
ListView listView;
ListAdapter listAdapter;
mFirebaseRef = new Firebase("https://<firebase root ref>/posts");
mFirebaseQuery = mFirebaseRef.orderByChild("author_id").equalTo(id); //`id` is a variable defined earlier
listAdapter = new FirebaseListAdapter<Post>(this, Post.class, android.R.layout.simple_list_item_, mFirebaseQuery) {
@Override
protected void populateView(View view, Post post) {
String postTopic = post.getTopic();
((TextView) view.findViewById(android.R.id.text1)).setText(postTopic);
}
};
listView = (ListView) findViewById(R.id.postsList);
listView.setAdapter(listAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String topic = String.valueOf(parent.getItemAtPosition(position));
Toast.makeText(getApplicationContext(), "List Item Value: "+topic, Toast.LENGTH_LONG).show();
}
});
而不是返回项目上显示的文本(在使用 ArrayAdapter
的情况下),String.valueOf
returns 对模型的一些引用 class我使用 - Post.class
.
输出是这样的:com.example.android.model.Post@a1e1874
谁能告诉我在这种情况下如何获取列表项的值?
AdapterView.getItemAtPosition()
returns 一个 Post
对象,所以:
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Post post = (Post)parent.getItemAtPosition(position);
String topic = post.getTopic();
Toast.makeText(getApplicationContext(), "List Item Value: "+topic, Toast.LENGTH_LONG).show();
}
我有一个 ListView
,我使用 FirebaseListAdapter
填充了它。现在我希望 OnItemClickListener
在 Toast
.
ListView listView;
ListAdapter listAdapter;
mFirebaseRef = new Firebase("https://<firebase root ref>/posts");
mFirebaseQuery = mFirebaseRef.orderByChild("author_id").equalTo(id); //`id` is a variable defined earlier
listAdapter = new FirebaseListAdapter<Post>(this, Post.class, android.R.layout.simple_list_item_, mFirebaseQuery) {
@Override
protected void populateView(View view, Post post) {
String postTopic = post.getTopic();
((TextView) view.findViewById(android.R.id.text1)).setText(postTopic);
}
};
listView = (ListView) findViewById(R.id.postsList);
listView.setAdapter(listAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String topic = String.valueOf(parent.getItemAtPosition(position));
Toast.makeText(getApplicationContext(), "List Item Value: "+topic, Toast.LENGTH_LONG).show();
}
});
而不是返回项目上显示的文本(在使用 ArrayAdapter
的情况下),String.valueOf
returns 对模型的一些引用 class我使用 - Post.class
.
输出是这样的:com.example.android.model.Post@a1e1874
谁能告诉我在这种情况下如何获取列表项的值?
AdapterView.getItemAtPosition()
returns 一个 Post
对象,所以:
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Post post = (Post)parent.getItemAtPosition(position);
String topic = post.getTopic();
Toast.makeText(getApplicationContext(), "List Item Value: "+topic, Toast.LENGTH_LONG).show();
}