Android listview onselect item有两个值,如何分别获取

标签 android listview

我有一个 ListView,它通过从 mysql 数据库获取数据来填充... ListView 包含来自数据库的三个值...

  1. 姓名
  2. 类别
  3. 职位

我想将这些值传递给下一个 Activity。所以我用了

list.setOnItemClickListener(new OnItemClickListener() {

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

                String items = parent.getItemAtPosition(position).toString();

                Intent intent = new Intent();
                intent.setClass(mapping, CheckIn.class);
                intent.putExtra("name", items);
                startActivity(intent);
            }
        }); 

但是它产生一个数组{name = xxx , category = asdasd , position = ddxxddxx}

在第二个 Activity 中,我想获取用户对指定名称的评论并将其插入到另一个表中。如何处理数组?

完整代码

try {
            HttpParams params = new BasicHttpParams();
            HttpConnectionParams.setSoTimeout(params, 0);
            HttpClient httpClient = new DefaultHttpClient(params);

            // prepare the HTTP GET call
            HttpGet httpget = new HttpGet("http://hopscriber.com/place.php");
            // get the response entity
            HttpEntity entity = httpClient.execute(httpget).getEntity();

            if (entity != null) {
                // get the response content as a string
                String response = EntityUtils.toString(entity);
                // consume the entity
                entity.consumeContent();

                // When HttpClient instance is no longer needed, shut down the
                // connection manager to ensure immediate deallocation of all
                // system resources
                httpClient.getConnectionManager().shutdown();

                // return the JSON response
                ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();

                JSONArray jsonArray = new JSONArray(response);

                if (jsonArray != null) {
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject object = (JSONObject) jsonArray.get(i);

                        HashMap<String, String> map = new HashMap<String, String>();

                        map.put(TAG_Name, object.getString("name"));
                        map.put(TAG_Category, object.getString("category"));

                        contactList.add(map);

                    }
                }

                ListAdapter adapter = new SimpleAdapter(this, contactList,
                        R.layout.menu_list_row, new String[] { TAG_Name,
                                TAG_Category }, new int[] { R.id.LR_Name,
                                R.id.LR_date });
                list.setAdapter(adapter);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        // Launching new screen on Selecting Single ListItem
        list.setOnItemClickListener(new OnItemClickListener() {

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

                String items = parent.getItemAtPosition(position).toString();


                Intent intent = new Intent();
                intent.setClass(mapping, CheckIn.class);
                intent.putExtra("name", items);
                startActivity(intent);
            }
        });
    }

最佳答案

试试这个,

 listview.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {
                Object obj = parent.getItemAtPosition(position);
                if (obj instanceof SQLiteCursor) {
                    SQLiteCursor Val = (SQLiteCursor) obj;
                    // this is storing to  textview
                    txtview.setText(cmnt.getString(0)); 
                    txtview.setText(cmnt.getString(1));
                    name = cmnt.getString(0);// this is store to string.
                    comments = cmnt.getString(1);
                }
            }
        });

Intent 传递:

  intent.putExtra("name",name);
  intent.putExtra("comments",comments);

从第二个 Activity 中获取值:

  name=getIntent().getStringExtra("name", null);
  comments=getIntent().getStringExtra("comments", null);

关于Android listview onselect item有两个值,如何分别获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11516626/

相关文章:

android - 所有 OpenGL ES 2 应用程序在 Visual Studio Emulator for Android 中崩溃

android - 如何处理 Android Activity 中的通知

android - 在 Android Studio 上为增强的 GeofencingApi 轮询 GPS 位置的正确程序

android - 替换 fragment 时停止 AsyncTask

android - 构建者 - 每次创建一个新实例?

java - 我的 "ACTION_MOVE"从未在我的 longPressListener 中调用

mysql - 连接表并显示

c# - Listitem 添加到 c# 中的第二行

android - 如何在 2 个 TextView 中拆分文本,就像有 1 个 TextView 一样?

java - 如何将数据放入XML的ListView中?