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());
}
}
}
我想将年份,姓名和年龄发送到另一个活动,我能够获得意图但不是其他值的位置。请帮忙......
我能够在列表中看到这些值,但无法在意图部分调用。
您的帮助将不胜感激。
答案 0 :(得分:0)
这可能会有所帮助
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());
}
}
}