Java / Android,是否可以获取Object的内容?

时间:2012-08-13 13:26:36

标签: java android listview onclick

正如标题所暗示的那样。我已经做了一些研究并尝试过杰克逊,看起来应该有效,但是在尝试使用时我犯了很多错误,所以这似乎是不行的。

基本上我的问题是这个,我有一个由适配器填充的列表视图:

friendsArrayAdapter = new FriendsArrayAdapter(
        NetPlay.this, R.layout.rowlayout,      friends);
listView.setAdapter(friendsArrayAdapter);
friendsArrayAdapter.notifyDataSetChanged();

然后我想处理这个列表视图中的onClick事件,将该列表视图的内容复制到变量中:

listView.setClickable(true);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, 
                int position, long arg3) {

           Object o = listView.getItemAtPosition(position);
           Object o = arg0.getItemAtPosition(position);

         //mText.setText(o);
        }
 });

现在一切正常,但返回的值是实际的对象ID /参考 显示为something@Friends.21312。我想要的是实际内容,应该是名称+ ID。

这是否可能,或者我是否需要重做整个列表视图群?

1 个答案:

答案 0 :(得分:1)

public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {

       //friends is the array that is given to the Adapter.
       //given friends = Friend[]
       Friend f = friends[position];
       f.getName();
       f.getID();
}

这只是一个猜测,请发布您的整个代码以获得更好的帮助!