【问题标题】:Android : unable to get the ids of the items in listviewAndroid:无法获取列表视图中项目的 ID
【发布时间】:2012-07-12 10:00:43
【问题描述】:

我正在尝试将在listview 中单击的项目的 ID 传递给下一个活动,但是每当我单击该项目时,它总是会获取 listview 中最后一个项目的 ID 这是我的代码

String src = a.getText().toString();

            ListView srceve  = (ListView)findViewById(R.id.list_scr_planner);
            ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
            if(src.length()>0)
            try{

            HttpGet post=new HttpGet("xxxxxxxxxxxxxxxxxxxxxxxxxxxx");
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
            nameValuePairs.add(new BasicNameValuePair("eventname", src));
            HttpResponse resp = login.client.execute(post);
            String re = EntityUtils.toString(resp.getEntity());

            Log.i("loggg", "error" + re);


            re= "{\"root\": " + re + "}";
            JSONObject root = new JSONObject(re);  
            JSONArray sessions = root.getJSONArray("root");
            Log.i("loggg", "error" + sessions);
            for(int i=0;i<sessions.length();i++){
                HashMap<String, String> map2 = new HashMap<String, String>();
                JSONObject e = sessions.getJSONObject(i);
                map2.put("event_name", e.getString("name"));
                map2.put("event_date", e.getString("eventdate"));
                mylist.add(map2);
                id = e.getString("id");

            }

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        SimpleAdapter adapt= new SimpleAdapter(searchevent.this,mylist, R.layout.list_custom ,new String[]{"event_name","event_date"},new int []{R.id.textnam,R.id.textdate});
        srceve.setAdapter(adapt);

        srceve.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View arg1,
                    int pos, long arg3) {
                // TODO Auto-generated method stub
                parent.getItemAtPosition(pos);
                Toast.makeText(getApplicationContext(), ""+ id + evename, 0).show();
                Intent i = new Intent(searchevent.this, eventDetails.class);
                i.putExtra("ar", id);
                startActivity(i);
            }
        });

请告诉我我哪里错了,我正在检索来自服务器的项目的 ID。谢谢

【问题讨论】:

  • 试试这个,i.putExtra("ar", pos);
  • 您在哪里设置id 的值?
  • 请提供您的适配器代码。
  • with i.putExtra("ar", pos); 我将获得该项目的位置,但我传递了 id 这是该特定项目的 id。我正在使用 json 检索 id...
  • 那么你应该得到id,基于选中的item..i.e.inside onItemClick

标签: android json android-listview httpclient


【解决方案1】:

在循环中:

for(int i=0;i<sessions.length();i++){
            HashMap<String, String> map2 = new HashMap<String, String>();
            JSONObject e = sessions.getJSONObject(i);
            map2.put("event_name", e.getString("name"));
            map2.put("event_date", e.getString("eventdate"));
            mylist.add(map2);
            id = e.getString("id");

        }

每次你覆盖id 的值时,这就是为什么它总是最后一个......你应该把它保存在一个数组中......

String[] ids = new String[sessions.length()];
for(int i=0;i<sessions.length();i++){
            HashMap<String, String> map2 = new HashMap<String, String>();
            JSONObject e = sessions.getJSONObject(i);
            map2.put("event_name", e.getString("name"));
            map2.put("event_date", e.getString("eventdate"));
            mylist.add(map2);
            ids[i] = e.getString("id");

        }

然后在onItemClick中,根据选中项的位置获取:

public void onItemClick(AdapterView<?> parent, View arg1,
                int pos, long arg3) {
            // TODO Auto-generated method stub
            parent.getItemAtPosition(pos);

            String id = ids[pos];

            Toast.makeText(getApplicationContext(), ""+ id + evename, 0).show();
            Intent i = new Intent(searchevent.this, eventDetails.class);
            i.putExtra("ar", id);
            startActivity(i);
        }

【讨论】:

    【解决方案2】:
       srceve.setOnItemClickListener(new OnItemClickListener() {
    
            @Override
            public void onItemClick(AdapterView<?> parent, View arg1,
                    int pos, long arg3) {
            // TODO Auto-generated method stub
            String id=parent.getId();//this will give you the id of the selected item View
            parent.getItemAtPosition(pos);
            Toast.makeText(getApplicationContext(), ""+ id + evename, 0).show();
            Intent i = new Intent(searchevent.this, eventDetails.class);
            i.putExtra("ar", id);
            startActivity(i);
          }
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-22
      • 1970-01-01
      • 2015-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-06
      相关资源
      最近更新 更多