我有点麻烦。所以我的问题是我想把json_encode的结果放到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);
}
任何人都可以解决这个问题?或者还有其他方法可以简化它? 只是为了简单,我想在listview中显示json。
感谢您的合作
答案 0 :(得分: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步:在您的活动中创建自定义列表适配器&amp;用于绑定和存储已解析的JSON数据的Hashmap
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步:在您的帖子执行方法中,将列表设置为列表视图
@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);
}
答案 1 :(得分:0)
//将它存放在arraylist中
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);