【发布时间】:2015-06-08 07:32:32
【问题描述】:
我正在尝试使用 ListView 将一些项目从 PHP 填充到 Android
public class ChooseCategory extends ListActivity {
private ListView lv;
ArrayAdapter<FoodStores> arrayAdapter;
private static final String TAG = MainActivity.class.getSimpleName();
private ArrayList<FoodStores> storefoodList;
// JSON parser class
JSONParser jsonParser = new JSONParser();
//ids
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
private String URL_STORES = "http://www.123.com/get_stores.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
//lv = (ListView) findViewById(R.id.list_item);
lv = (ListView)findViewById(android.R.id.list);
storefoodList = new ArrayList<FoodStores>();
new GetFoodStores().execute();
arrayAdapter = new ArrayAdapter<FoodStores> (this,R.layout.restaurant_list,storefoodList );
private class GetFoodStores extends AsyncTask<Void,Void,Void> {
@Override
protected void onPreExecute(){
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... arg0) {
ServiceHandlerFood jsonParserFood = new ServiceHandlerFood();
String json = jsonParserFood.makeServiceCall(URL_STORES, ServiceHandlerFood.GET);
Log.e("Response: ", " > " + json);
if(json != null){
try{
JSONObject jsonObj = new JSONObject(json);
if(jsonObj != null){
JSONArray storeListFood = jsonObj.getJSONArray("storelistfood");
for(int i = 0; i < storeListFood.length(); i++){
JSONObject storeFoodObj = (JSONObject) storeListFood.get(i);
FoodStores foodStores = new FoodStores(storeFoodObj.getInt("id"),storeFoodObj.getString("STORENAME"));
storefoodList.add(foodStores);
}
}
}catch(JSONException e){
e.printStackTrace();
}
}else{
Log.e("JSON Data", "No data received from server");
}
return null;
}
@Override
protected void onPostExecute(Void result){
super.onPostExecute(result);
lv.setAdapter(arrayAdapter);
}
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="{relativePackage}.${activityClass}" >
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
restaurant_list.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:src="@drawable/nasilemak2" />
<TextView
android:id="@+id/Itemname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:paddingTop="5dp"/>
</LinearLayout>
我得到的错误是它只显示空白屏幕并在一分钟后崩溃。我想使用 PHP 中的 ArrayList 填充 ListView。
【问题讨论】:
-
发布崩溃日志。
-
我认为应该是 webservice 而不是 php,或者你可以说是用 PHP 实现的 web services。
-
我认为您必须通过使用
ArrayAdapter<FoodStores>扩展类来为您的问题制作自定义适配器。 -
如果以下任一答案满足您的查询,请将其标记为已接受。