我想将listview数据发送到android中的另一个活动

时间:2015-02-08 23:05:59

标签: android android-intent android-listview

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());
        } 
    }
}

我想将年份,姓名和年龄发送到另一个活动,我能够获得意图但不是其他值的位置。请帮忙......

我能够在列表中看到这些值,但无法在意图部分调用。

您的帮助将不胜感激。

3 个答案:

答案 0 :(得分:0)

  1. 首先将您的JSONArray json声明为类变量。
  2. 获取数据new JSONArray(data);时初始化它。
  3. 获取列表项位置后,从该json数组中访问该对象。
  4. 从对象获取属性并将意图传递给另一个活动。

答案 1 :(得分:0)

请不要在实际项目中做这样的事情。

首先,您需要分离网络请求逻辑,响应处理逻辑和UI。

我建议将所有列表存储在SQLite表中,之后您就不需要将其发送到其他活动。

您可以尝试http://www.activeandroid.com/

答案 2 :(得分:0)

是的,你可以这样做

    public class MainActivity extends Activity {
       TextView text;
       JSONArray json;
       @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 {

   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) {
            JSONObject obj=json.getJSONObject(arg2);
            // 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);
            view_det.putExtra("name", obj.getString(name));
            view_det.putExtra("name", obj.getString(year));
            view_det.putExtra("name", obj.getString(age));
            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());
    } 
}}

使用此代码,您可以将数据从一个活动发送到另一个活动