1

我有一个ListView通过从 mysql db 获取数据来填充的......它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);
            }
        });
    }
4

4 回答 4

2
    list.setOnItemClickListener(new OnItemClickListener() {

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

            String name = ((TextView) view.findViewById(R.id.LR_Name)).getText().toString();
            String cost = ((TextView) view.findViewById(R.id.LR_date)).getText().toString();

            // Starting new intent
            Intent in = new Intent(getApplicationContext(), CheckIn.class);
            in.putExtra(TAG_Name, name);
            in.putExtra(TAG_Category, cost);
            startActivity(in);
        }
    });

这对我有用...谢谢您的支持

于 2012-07-17T07:17:25.123 回答
2

尝试这个,

 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.putExtra("name",name);
  intent.putExtra("comments",comments);

从第二个活动中获取值:

  name=getIntent().getStringExtra("name", null);
  comments=getIntent().getStringExtra("comments", null);
于 2012-07-17T05:53:53.650 回答
0

恢复 id 并在第二个活动中使用:

public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
        String rowid = String.valueOf(id)
        Bundle bundle = new Bundle();
        bundle.putString("ID", rowid);
        intent.putExtras(bundle);
        startActivity(intent);
}

使用以下方法检索第二个活动:

String rowid= getIntent().getExtras().getString("ID");

不要忘记将 id 从字符串转换回 long 并在第二个活动中检索它后添加 1

于 2012-07-17T06:22:13.143 回答
-1

从您的第一个Activity开始,只需使用 bundle 传递名称、类别和位置。像这样的东西

Intent intent = new Intent(SearchAssetActivity.this,
                        CheckInActivity.class);
Bundle bundle = new Bundle();
bundle.putString("name", xxx);
bundle.putString("sno", asasas);
intent.putExtras(bundle);
startActivity(intent);

第二个Activity你可以在你的onCreate()

String name= getIntent().getExtras().getString("name");

这样您就可以检索任意数量的参数

于 2012-07-17T05:57:11.220 回答