【问题标题】:How to get value of a ListView item, populated through FirebaseListAdapter如何获取通过 FirebaseListAdapter 填充的 ListView 项的值
【发布时间】:2016-07-28 23:05:03
【问题描述】:

我有一个ListView,我使用FirebaseListAdapter 填充它。现在我希望OnItemClickListenerToast 中显示单击的项目的值(项目上显示的文本)。

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

String.valueOf 不是返回项目上显示的文本(在使用 ArrayAdapter 的情况下),而是返回对我使用的模型类的一些引用 - Post.class
输出是这样的:com.example.android.model.Post@a1e1874

谁能告诉我在这种情况下如何获取列表项的值?

【问题讨论】:

    标签: android listview firebase firebase-realtime-database firebaseui


    【解决方案1】:

    AdapterView.getItemAtPosition() 返回一个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();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-22
      相关资源
      最近更新 更多