想将listview数据发送到android中的另一个活动
Posted
技术标签:
【中文标题】想将listview数据发送到android中的另一个活动【英文标题】:want to send listview data to another activity in android 【发布时间】:2015-04-09 00:31:21 【问题描述】:package com.example.selflistview;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity
TextView text;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
connect();
private void connect()
String data;
List<String> r = new ArrayList<String>();
ArrayAdapter<String>adapter=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,r);
ListView list=(ListView)findViewById(R.id.listView1);
try
DefaultHttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("http://localhost/listview.php");
HttpResponse response = client.execute(request);
HttpEntity entity=response.getEntity();
data=EntityUtils.toString(entity);
Log.e("STRING", data);
try
JSONArray json=new JSONArray(data);
for(int i=0;i<json.length(); i++)
JSONObject obj=json.getJSONObject(i);
String name=obj.getString("name");
String year=obj.getString("year");
String age=obj.getString("age");
Log.e("STRING", name);
r.add(name);
//r.add(year);
//r.add(age);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3)
// TODO Auto-generated method stub
//final String item = (String) arg0.getItemAtPosition(arg2);
Toast.makeText(getBaseContext(), "arg2 : "+arg2, Toast.LENGTH_SHORT).show();
Intent view_det = new Intent(getBaseContext(),ViewDetail.class);
view_det.putExtra("position",arg2);
startActivity(view_det);
);
catch (JSONException e)
// TODO Auto-generated catch block
e.printStackTrace();
catch (ClientProtocolException e)
Log.d("HTTPCLIENT", e.getLocalizedMessage());
catch (IOException e)
Log.d("HTTPCLIENT", e.getLocalizedMessage());
我想将年份、姓名和年龄发送到另一个活动,我能够获得意向职位,但不能获得其他值。请帮忙...
我可以在列表中看到这些值,但无法在意图部分调用。
您的帮助将不胜感激..
【问题讨论】:
你可以从你的适配器那里得到这个。并且不要使用 list.setAdapter(adapter);在每次迭代中。设置一次适配器并使用 notifyDataSetChanged(); 先生,您能帮帮我吗..,您可以帮我修改代码吗..?我是安卓新手 【参考方案1】:这可能会有所帮助
Public class MainActivity extends Activity
TextView text;
List<String> r; //define list here
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
connect();
private void connect()
String data;
r = new ArrayList<String>();
try
DefaultHttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("http://localhost/listview.php");
HttpResponse response = client.execute(request);
HttpEntity entity=response.getEntity();
data=EntityUtils.toString(entity);
Log.e("STRING", data);
try
JSONArray json=new JSONArray(data);
for(int i=0;i<json.length(); i++)
JSONObject obj=json.getJSONObject(i);
String name=obj.getString("name");
String year=obj.getString("year");
String age=obj.getString("age");
Log.e("STRING", name);
r.add(name);
//r.add(year);
//r.add(age);
ArrayAdapter<String>adapter=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,r);
ListView list=(ListView)findViewById(R.id.listView1);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3)
// TODO Auto-generated method stub
//final String item = (String) arg0.getItemAtPosition(arg2);
Toast.makeText(getBaseContext(), "arg2 : "+arg2, Toast.LENGTH_SHORT).show();
Intent view_det = new Intent(getBaseContext(),ViewDetail.class);
view_det.putExtra("position",arg2);
String name = r.get(arg2);//add this line to get name from the arraylist of that position
JSONObject obj=json.getJSONObject(arg2); //you can get other two values from json object
String year=obj.getString("year");
String age=obj.getString("age");
view_det.putExtra("name",name); //add this
view_det.putExtra("year",year); //add this
view_det.putExtra("age",age); //add this
startActivity(view_det);
);
catch (JSONException e)
// TODO Auto-generated catch block
e.printStackTrace();
catch (ClientProtocolException e)
Log.d("HTTPCLIENT", e.getLocalizedMessage());
catch (IOException e)
Log.d("HTTPCLIENT", e.getLocalizedMessage());
【讨论】:
他也想得到名字和年份,也许你可以修改你的代码从JSON数组中得到这些? 使用位置从json数组中获取json对象,并得到最终编辑中所做的年份和年龄以上是关于想将listview数据发送到android中的另一个活动的主要内容,如果未能解决你的问题,请参考以下文章
Android java:如何正确将排序数据发送到listView?