8

如何从适配器位置获取价值,我在下面有代码:

CategoriesXmlParser categoryXmlParser = new CategoriesXmlParser();
List<HashMap<String, Object>> categories = null;

try {
  categories = categoryXmlParser.parse(reader);
} catch (Exception e) {
  Log.d("Exception", e.toString());
}

String[] from = { "name", "image" };
int[] to = { R.id.nama_category, R.id.logo_category };

final SimpleAdapter adapter = new SimpleAdapter(getBaseContext(),
                    categories, R.layout.per_item_category, from, to);

mListView.setOnItemClickListener(new OnItemClickListener() {
  public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
    Object obj = mListView.getAdapter().getItem(position);
    String value = obj.toString();
    Log.d("MyLog", "Value is: "+value);
    String name = // how code to get name value.
  }
});

如果我在 MyLog 中的 logcat 上查看它,我会得到:

值为: {position=12, image_path= http://192.168.103.121/xml/icon.png , link= http://192.168.103.121/xml/category.php?kat_id=13 , name=Category 13}

所以我的问题是,我想从名称中获取值并将其存储到变量字符串名称中,我只想在字符串名称中获得“Category 13”。因为我想将它传递给另一个活动。

4

6 回答 6

35

貌似你用hashmap做的对象,比如blablabla.put("name", "value")??如是。试试这个:

mListView.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
        Object obj = mListView.getAdapter().getItem(position);
        String value = obj.toString();
        Log.d("MyLog", "Value is: "+value);
        String name = // how code to get name value.
    }
});

改成:

mListView.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        HashMap<String, Object> obj = (HashMap<String, Object>) adapter.getItem(position);
            String name = (String) obj.get("name");
            Log.d("Yourtag", name);
    }
});
于 2013-01-18T20:17:18.797 回答
6
value = value.substring(value.indexOf("name=")+5,value.length()-1);
于 2013-01-18T06:12:22.537 回答
3
listID.setOnItemClickListener(new AdapterView.OnItemClickListener() 
                {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View v,
                            int position, long id) {
                        // TODO Auto-generated method stub

                        Object obj=listID.getAdapter().getItem(position);
                        String str=obj.toString();

                    Toast.makeText(MainActivity.this, "Item is= "+str, Toast.LENGTH_LONG).show();
                    }
                });
于 2015-06-05T18:28:45.160 回答
1

您还可以通过以下方式获取适配器数据AdapterView<?> parent

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    //setHasOptionsMenu(true);
    listView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            final Filedata mnotes = (Filedata) parent
                    .getItemAtPosition(position);
           Toast.makeText(getActivity(), Filedata.getName(),Toast.LENGTH_SHORT).show();
});
}
于 2014-02-14T07:07:08.590 回答
1

这就是我所做的:-

lvHomePage.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                   @Override
                   public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                       Transaction selectedFromList =(Transaction)parent.getAdapter().getItem(position);
                       Log.d("val:",selectedFromList.getDate());
                       Log.d("val:",selectedFromList.getMobile_no());
                   }
               });

玩得开心!

于 2017-03-30T12:34:31.033 回答
0

listID.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View v,
                        int position, long id) {
                                 

HashMap<String, Object> sdsa = (HashMap<String, Object>) adapter.getItem(position); Log.e("IndentNo", (String) sdsa.get("IndentNo")); Log.e("IndentId", (String) sdsa.get("IndentId"));

                }
            });
于 2021-09-15T12:29:28.910 回答