我的json输出:
[{"FirstName":"childname2"},{"FirstName":"childname4"}]
我试图用json数据填充我的listview,这是我用post方法得到的,
通过这样做,我在postexecute中得到了我的总json [{"FirstName":"childname2"},{"FirstName":"childname4"}]
输出。当我试图通过使用for循环传递列表中的这个值它没有显示任何错误,,,以及没有填充我的列表, ,
建议,,,,
我的java:
private ListView lv=(ListView) findViewById(R.id.listView1);
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
Toast.makeText(Certify_Child_Count.this,result, Toast.LENGTH_LONG).show();
if (result.equals("[]"))
{
Toast.makeText(Certify_Child_Count.this,
"Wrong Username or Password", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(Certify_Child_Count.this,result, Toast.LENGTH_LONG).show();
JSONArray arr=new JSONArray();
JSONObject obj = null;
//ArrayAdapter<String> adapter=new ArrayAdapter<String>(Certify_Child_Count.this, android.R.layout.simple_list_item_1, list);
//lv.setAdapter(adapter);
for(int i=0;i<arr.length();i++)
{
Toast.makeText(Certify_Child_Count.this,"going into for loop", Toast.LENGTH_LONG).show();
try {
obj=arr.getJSONObject(i);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
list.add(obj.getString("FirstName"));
Toast.makeText(Certify_Child_Count.this,obj.getString("FirstName"), Toast.LENGTH_LONG).show();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
答案 0 :(得分:1)
您需要使用适配器来填充ListView。
您需要创建一个类来实现ArraryAdapters<>
例如:
public class ListAdapters extends ArraryAdapters<string>
此类实现getView()
方法。在这里,您需要填充列表视图。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = context.getLayoutInflater();
convertView = inflater.inflate(R.layout.list_rows, null);
holder = new ViewHolder();
holder.tectview = (TextView) convertView
.findViewById(R.id.questionary_name);
convertView.setTag(holder);
}
holder = (ViewHolder) convertView.getTag();
String s = items[position];
holder.tectview.setText(s);
return convertView;
}
答案 1 :(得分:0)
你的适配器在哪里?您需要一个适配器来显示列表的数据。如果是单个文本,您可以使用基本适配器,并在每次完成请求并解析数据时通知它。
答案 2 :(得分:0)
你应该改变
JSONArray arr=new JSONArray();
到
JSONArray arr=new JSONArray(result);
您正在创建一个空JSONArray
而不添加结果。
答案 3 :(得分:0)
你很幸运,我只是完成了与你几乎一样的演示。希望它可以帮助你解决问题。
package com.ecnu.vendingmachine;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListFragment;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import com.ecnu.vendingmachine.widgets.JSONParser;
public class VendingFragment extends ListFragment {
private String Tag = "VendingFragment";
private static final String TAG_SUCCESS = "success";
private ListView listView;
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jsonParser = new JSONParser();
JSONArray vendingmachine = null;
ArrayList<HashMap<String, String>> vendinglist;
// url to get all products list
MainActivity main = new MainActivity();
private String url_all_vendingmachine = main.getIP()
+ "vendingmachine/get_all_vendingmachine.php";
// products JSONArray
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.vending_main, container, false);
}
@Override
public void onViewCreated (View view, Bundle savedInstanceState) {
vendinglist = new ArrayList<HashMap<String, String>>();
//listView = (ListView) view.findViewById(R.id.list);
listView = getListView();
new get_all_vendingmachine().execute();
}
class get_all_vendingmachine extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Creating Product..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
Log.i(Tag, "pDialog");
}
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
Log.i(Tag, url_all_vendingmachine);
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(
url_all_vendingmachine, "GET", params);
// check log cat for response
Log.i(Tag, json.toString());
// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// Ada record Data (SUCCESS = 1)
// Getting Array of vendingmachine
vendingmachine = json.getJSONArray("vendings");
// looping through All vendingmachine
for (int i = 0; i < vendingmachine.length(); i++) {
JSONObject c = vendingmachine.getJSONObject(i);
// Storing each json item in variable
String id = c.getString("VMid");
String name = c.getString("Name");
String address = c.getString("Address");
Log.i(Tag, id);
Log.i(Tag, name);
Log.i(Tag, address);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put("VMid", id);
map.put("Name", name);
map.put("Address", address);
// adding HashList to ArrayList
vendinglist.add(map);
}
} else {
// failed to create product
getActivity().finish();
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
// updating listview
// HashMap<String, String>中的key
String[] from = { "Name", "Address", "VMid" };
// list_item.xml中对应的控件ID
int[] to = { R.id.vending_name, R.id.vending_address,
R.id.vending_id };
Log.i(Tag, from[0]);
SimpleAdapter adapter = new SimpleAdapter(getActivity(),
vendinglist, R.layout.vending_list, from, to);
listView.setAdapter(adapter);
}
});
}
}
}
vending_main_xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/whiteColor" >
<include
android:id="@+id/layout_buy_title_actionbar"
layout="@layout/headbar_title" />
<ListView
android:id="@id/android:list"
android:layout_below="@id/layout_buy_title_actionbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cacheColorHint="@android:color/transparent"
android:divider="@drawable/reader_item_divider"
android:listSelector="@android:color/transparent" >
</ListView>
</RelativeLayout>
vending_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="@dimen/selectVmListCell_Height"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="@dimen/selectVmListCell_Height"
android:layout_gravity="center_vertical"
android:background="@drawable/bg_listview"
android:gravity="center_vertical"
android:paddingLeft="@dimen/layout_Margin_10" >
<LinearLayout
android:id="@+id/imageTypeLayout"
android:layout_width="@dimen/smallImage_HW_45"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:orientation="vertical" >
<ImageView
android:id="@+id/vendingImage"
style="@style/smallImageStyle"
android:src="@drawable/buy_main_not_shop" />
<TextView
android:id="@+id/distanceText"
style="@style/smallTxtStyle"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:singleLine="true"
android:textColor="@color/lightOrangeColor" />
</LinearLayout>
<RelativeLayout
android:id="@+id/vendingSave"
android:layout_width="47.0dip"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" >
<ImageView
android:id="@+id/vendingSaveImage"
android:layout_width="27.0dip"
android:layout_height="27.0dip"
android:layout_centerInParent="true"
android:layout_centerVertical="true"
android:background="@drawable/bg_favor_btn" />
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerVertical="true"
android:layout_marginLeft="@dimen/layout_Margin_10"
android:layout_toLeftOf="@id/vendingSave"
android:layout_toRightOf="@id/imageTypeLayout"
android:gravity="center_vertical"
android:orientation="vertical" >
<TextView
android:id="@+id/vending_name"
style="@style/midDarkGrayTxtStyle"
android:layout_width="fill_parent" />
<TextView
android:id="@+id/vending_address"
style="@style/smallLightGrayTxtStyle"
android:layout_width="fill_parent"
android:maxLines="2" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
style="@style/smallLightGrayTxtStyle"
android:text="Number:" />
<TextView
android:id="@+id/vending_id"
style="@style/smallLightGrayTxtStyle" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
希望它可以帮助您找到如何将listview放入片段中。 我的listview中的元素很复杂,如果你只想要一行,它比我编码的更简单。