我有一个 ListView
,它通过从 mysql 数据库获取数据来填充... ListView
包含来自数据库的三个值...
- 姓名
- 类别
- 职位
我想将这些值传递给下一个 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/