SOF*_*234 -6 java android listview
我试图使用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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
我得到的错误是它只显示空白屏幕并在一分钟后崩溃.我想使用PHP中的ArrayList填充ListView.
你没有提供所有课程,所以我提供了一些课程.这个给你:

ChooseCategory.java:
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class ChooseCategory extends ListActivity {
private ListView lv;
ArrayAdapter<FoodStores> arrayAdapter;
private static final String TAG = "XXX";
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://echo.jsontest.com/id/1/STORENAME/Perogi";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(android.R.id.list);
storefoodList = new ArrayList<FoodStores>();
new GetFoodStores().execute();
arrayAdapter = new ArrayAdapter<FoodStores>(this,
R.layout.restaurant_list, R.id.Itemname, storefoodList);
lv.setAdapter(arrayAdapter);
}
private class GetFoodStores extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... arg0) {
ServiceHandlerFood jsonParserFood = new ServiceHandlerFood();
String json = jsonParserFood.makeServiceCall(URL_STORES,
"TEST");
//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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
Foodstores.java:
public class FoodStores {
int id;
String name;
public FoodStores(int id, String string) {
this.id=id;
name=string;
}
@Override
public String toString(){
return name;
}
}
Run Code Online (Sandbox Code Playgroud)
ServiceHandlerFood.java:
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
public class ServiceHandlerFood {
public static final String GET = "GET";
public String makeServiceCall(String uRL_STORES, String get2) {
if ("TEST".equals(get2))
return "{'storelistfood':[{'id':1, 'STORENAME':'Arbys'},{'id':2, 'STORENAME':'Perogi'},{'id':3, 'STORENAME':'McDonalds'}]}";
DefaultHttpClient httpclient = new DefaultHttpClient(
new BasicHttpParams());
HttpPost httppost = new HttpPost(uRL_STORES);
httppost.setHeader("Content-type", "application/json");
InputStream inputStream = null;
String result = null;
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(
inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
result = sb.toString();
} catch (Exception e) {
// Oops
} finally {
try {
if (inputStream != null)
inputStream.close();
} catch (Exception squish) {
}
}
return result;
}
}
Run Code Online (Sandbox Code Playgroud)
其余文件没有动过.
| 归档时间: |
|
| 查看次数: |
691 次 |
| 最近记录: |