2015-02-08 76 views
0
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()); 
     } 
    } 
} 

我想發送年份,姓名和年齡到另一個活動,我能夠獲取位置的意圖,但不是其他值。請幫忙...我想發送listview數據到android中的另一個活動

我能夠在列表中看到這些值,但無法在意向段調用。

您的幫助將不勝感激..

回答

0
  1. 首先聲明你的JSONArray json爲類變量。
  2. 當您獲取數據new JSONArray(data);時,將其初始化。
  3. 當你獲得列表項目的位置,然後從該json數組訪問該對象。
  4. 從對象中獲取屬性並將其傳遞給另一個活動。
+0

我是新來的機器人,請你告訴我怎麼做,我不知道先生。請幫助我.., – 2015-02-09 04:34:56

0

請不要在真實項目中這樣做。

首先你需要分離網絡請求邏輯,響應處理邏輯和UI。

我建議將所有列表存儲在SQLite表中,因此在此之後您不需要將其發送到其他活動。

你可以嘗試http://www.activeandroid.com/

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

使用此代碼,你可以從一個活動的數據發送到另一個

+0

先生,獲取這些行的錯誤 view_det.putExtra(「name」,obj.getString(name)); view_det.putExtra(「name」,obj.getString(year)); view_det.putExtra(「name」,obj.getString(age)); 無法引用在不同方法中定義的內部類中的非最終變量名稱 – 2015-02-09 12:16:51

+0

在類 – thestrongenough 2015-02-09 14:18:53

+0

中聲明這些變量先生,我已經聲明,但是如何在此代碼中使用該值。 ? – 2015-02-23 08:48:15

相關問題