2015-07-21 46 views
0

我對此有些麻煩。所以我的問題是我想把json_encode的結果放到listview中。如果沒有長時間閒聊我會告訴你:將Array2d填充到ListView中

這是JSON的結果:

{ 
"length": 
[ 
    { 
     "kode_schedule":"sch0001", 
     "kode_matkul":"TIB01", 
     "title":"Basis Data", 
     "ruang":"501", 
     "latitude":"-6.18861653446272", 
     "longitude":"106.801122526546", 
     "lantai":"5", 
     "hari":"Selasa", 
     "jam":"17:45:00" 
    },   
    { 
     "kode_schedule":"sch0001", 
     "kode_matkul":"TIB02", 
     "title":"Pemrograman Berorientasi Objek", 
     "ruang":"LABB", 
     "latitude":"-6.18864706134991", 
     "longitude":"106.801161122636", 
     "lantai":"5", 
     "hari":"Selasa", 
     "jam":"19:30:00" 
    } 
] 
} 

這是我到目前爲止的代碼:

class GetLength extends AsyncTask<String, String, String> { 
JSONParser jParser = new JSONParser(); 
protected String doInBackground(String... params) { 
      String title; 
      String ruang; 
      String lantai; 
      String hari; 
      String jam; 
      String latitude, longitude; 
      try { 
       // Building Parameters 
       List<NameValuePair> param = new ArrayList<NameValuePair>(); 
       param.add(new BasicNameValuePair("username", txtNim.getText().toString())); 
       JSONObject json = jParser.makeHttpRequest("http://studentstracking.hol.es/installation.php", "POST", param); 
       JSONArray array = json.getJSONArray("length"); 
       for (int i = 0; i < array.length(); i++) { 
        JSONObject row = array.getJSONObject(i); 
        kode_matkul = row.getString("kode_matkul"); 
        title = row.getString("title"); 
        ruang = row.getString("ruang"); 
        latitude = row.getString("latitude"); 
        longitude = row.getString("longitude"); 
        lantai = row.getString("lantai"); 
        hari = row.getString("hari"); 
        jam = row.getString("jam"); 
        kode_matkul_list.add(kode_matkul); 
        title_list.add(title); 
        ruang_list.add(ruang); 
        latitude_list.add(latitude); 
        longitude_list.add(longitude); 
        lantai_list.add(lantai); 
        hari_list.add(hari); 
        jam_list.add(jam); 
        array2d[] array2ds = new array2d[i]; 
        array2ds[i] = new array2d(kode_matkul, title, ruang, longitude, latitude, lantai, hari, jam); 
       } 

@Override 
     protected void onPostExecute(String s) { 
      super.onPostExecute(s); 
} 

這是另一個類:

class array2d { 
     String kode_matkuls; 
     String titles; 
     String ruangs; 
     String lantais; 
     String haris; 
     String jams; 
     String latitudes, longitudes; 

     public array2d(String kode_matkuls, String titles, String ruangs, String longitudes, String latitudes, String lantais, String haris, String jams) { 
      this.kode_matkuls = kode_matkuls; 
      this.titles = titles; 
      this.ruangs = ruangs; 
      this.lantais = lantais; 
      this.haris = haris; 
      this.jams = jams; 
      this.latitudes = latitudes; 
      this.longitudes = longitudes; 
     } 
    } 

這是方法:

private void populate(ArrayList<String> array) { 
     ListView showList = (ListView) findViewById(R.id.listView2); 
     ArrayAdapter<String> shows = new ArrayAdapter<String>(
       getApplicationContext(), 
       android.R.layout.simple_list_item_1, array); 
     showList.setAdapter(shows); 
    } 

任何人都可以解決這個問題?或者也許有其他方法可以簡化它? 只是爲了簡單,我想在列表視圖中顯示json。

感謝您在ArrayList中合作

+1

使用自定義列表視圖.... :) – koutuk

+0

看到http://stackoverflow.com/questions/28192815/how-to-add-定製佈局到陣列適配器/ 28192966#28192966 –

+0

你解決了你的問題嗎?\ –

回答

0

//店呢
ArrayList<String> my_array = new ArrayList<String>();
my_array.add("your json content");
//然後設置addapter到的ListView
ArrayAdapter my_Adapter = new ArrayAdapter(this, R.layout.list,my_array);
listview.setAdapter(my_Adapter);

1

你可以試試下面的辦法。這將使解決方案變得更容易。

步驟1:創建CustomListViewAdapter類應該是這個樣子,

public class CustomListViewAdapter extends BaseAdapter { 

    // Declare Variables 
    Context context; 
    LayoutInflater inflater; 
    ArrayList<HashMap<String, String>> data; 
    HashMap<String, String> resultp = new HashMap<String, String>(); 

    public ListViewAdapter(Context context, 
      ArrayList<HashMap<String, String>> arraylist) { 
     this.context = context; 
     data = arraylist; 
    } 

    @Override 
    public int getCount() { 
     return data.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return null; 
    } 

    @Override 
    public long getItemId(int position) { 
     return 0; 
    } 

    public View getView(final int position, View convertView, ViewGroup parent) { 
     // Declare Variables 
     TextView kode_matkul; 
     TextView title; 

     inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     View itemView = inflater.inflate(R.layout.listview_item, parent, false); 
     // Get the position 
     resultp = data.get(position); 

     // Locate the TextViews in listview_item.xml 
     kode_matkul = (TextView) itemView.findViewById(R.id.kode_matkul); 
     title = (TextView) itemView.findViewById(R.id.title); 

     // Capture position and set results to the TextViews 
     kode_matkul .setText(resultp.get("kode_matkul")); 
     title.setText(resultp.get("title")); 

     return itemView; 
    } 
} 

第2步:爲您定製適配器,你需要指定一個佈局(例如,在這裏我使用listview_item.xml )這將是列表中的視圖。佈局可能是這個樣子,

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

     <TextView 
      android:id="@+id/kode_matkul" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"/> 
     <TextView 
      android:id="@+id/title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"/> 

</LinearLayout> 

第3步:在您的活動創建自定義列表適配器& HashMap來每次你分析時綁定並存儲解析JSON數據

CustomListViewAdapter adapter; 
ArrayList<HashMap<String, String>> arraylist; 

現在一個JSON值把解析的數據HashMap的

 for (int i = 0; i < array.length(); i++) { 
    JSONObject row = array.getJSONObject(i); 
    HashMap<String, String> map = new HashMap<String, String>(); 
    // Retrive JSON String 
    map.put("kode_matkul ", row.getString("kode_matkul")); 
    map.put("title ", row.getString("title ")); 
    ........................................... 
    // Set the JSON Objects into the array 
    arraylist.add(map); 
    } 

科技教育裏面第4頁:在您的文章execute方法設置列表列表視圖

@Override 
     protected void onPostExecute(Void args) { 
      // Locate the listview in listview_main.xml 
      listview = (ListView) findViewById(R.id.listview); 
      // Pass the results into ListViewAdapter.java 
      adapter = new CustomListViewAdapter(MainActivity.this, arraylist); 
      // Set the adapter to the ListView 
      listview.setAdapter(adapter); 
     } 
+0

我在哪裏將第3步放在我的活動中?它在doInBackground裏面嗎? –

+0

在Activity類的開頭聲明這兩個。 –

+0

它給出了一些錯誤。這裏是:試圖調用虛擬方法'int java.util.ArrayList。大小()'空對象引用 在skripsi.ubm.studenttracking.CustomListViewAdapter.getCount(CustomListViewAdapter.java:32) –