我在使此代码正常工作时遇到问题。
基本上我从一个列表活动转到另一个,并通过活动的意图将列表项中的文本传递到新列表视图,然后在新列表活动中检索该文本,然后根据该列表的值执行 http 请求物品。
原木猫
04-05 17:47:32.370: E/AndroidRuntime(30135): FATAL EXCEPTION: main
04-05 17:47:32.370: E/AndroidRuntime(30135): java.lang.ClassCastException:android.widget.LinearLayout
04-05 17:47:32.370: E/AndroidRuntime(30135): at com.thickcrustdesigns.ufood.CatogPage$1.onItemClick(CatogPage.java:66)
04-05 17:47:32.370: E/AndroidRuntime(30135): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
04-05 17:47:32.370: E/AndroidRuntime(30135): at android.widget.ListView.performItemClick(ListView.java:3731)
04-05 17:47:32.370: E/AndroidRuntime(30135): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1959)
04-05 17:47:32.370: E/AndroidRuntime(30135): at android.os.Handler.handleCallback(Handler.java:587)
04-05 17:47:32.370: E/AndroidRuntime(30135): at android.os.Handler.dispatchMessage(Handler.java:92)
04-05 17:47:32.370: E/AndroidRuntime(30135): at android.os.Looper.loop(Looper.java:130)
04-05 17:47:32.370: E/AndroidRuntime(30135): at android.app.ActivityThread.main(ActivityThread.java:3691)
04-05 17:47:32.370: E/AndroidRuntime(30135): at java.lang.reflect.Method.invokeNative(Native Method)
04-05 17:47:32.370: E/AndroidRuntime(30135): at java.lang.reflect.Method.invoke(Method.java:507)
04-05 17:47:32.370: E/AndroidRuntime(30135): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
04-05 17:47:32.370: E/AndroidRuntime(30135): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
04-05 17:47:32.370: E/AndroidRuntime(30135): at dalvik.system.NativeStart.main(Native Method)
列表活动 1
package com.thickcrustdesigns.ufood;
import java.util.ArrayList;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
public class CatogPage extends ListActivity {
ListView listView1;
Button btn_bk;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.definition_main);
btn_bk = (Button) findViewById(R.id.btn_bk);
listView1 = (ListView) findViewById(android.R.id.list);
ArrayList<NameValuePair> nvp = new ArrayList<NameValuePair>();
nvp.add(new BasicNameValuePair("request", "categories"));
ArrayList<JSONObject> jsondefs = Request.fetchData(this, nvp);
String[] defs = new String[jsondefs.size()];
for (int i = 0; i < jsondefs.size(); i++) {
try {
defs[i] = jsondefs.get(i).getString("Name");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
uFoodAdapter adapter = new uFoodAdapter(this, R.layout.definition_list,
defs);
listView1.setAdapter(adapter);
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
TextView tv = (TextView) view;
String p = tv.getText().toString();
Intent i = new Intent(getApplicationContext(), Results.class);
i.putExtra("category", p);
startActivity(i);
}
});
btn_bk.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent i = new Intent(getApplicationContext(),
UFoodAppActivity.class);
startActivity(i);
}
});
}
}
**ListActivity 2**
package com.thickcrustdesigns.ufood;
import java.util.ArrayList;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ListView;
public class Results extends ListActivity {
ListView listView1;
enum Category {
Chicken, Beef, Chinese, Cocktails, Curry, Deserts, Fish, ForOne {
public String toString() {
return "For One";
}
},
Lamb, LightBites {
public String toString() {
return "Light Bites";
}
},
Pasta, Pork, Vegetarian
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.definition_main);
listView1 = (ListView) findViewById(android.R.id.list);
Bundle data = getIntent().getExtras();
String category = data.getString("category");
Category cat = Category.valueOf(category);
String value = null;
switch (cat) {
case Chicken:
value = "Chicken";
break;
case Beef:
value = "Beef";
break;
case Chinese:
value = "Chinese";
break;
case Cocktails:
value = "Cocktails";
break;
case Curry:
value = "Curry";
break;
case Deserts:
value = "Deserts";
break;
case Fish:
value = "Fish";
break;
case ForOne:
value = "ForOne";
break;
case Lamb:
value = "Lamb";
break;
case LightBites:
value = "LightBites";
break;
case Pasta:
value = "Pasta";
break;
case Pork:
value = "Pork";
break;
case Vegetarian:
value = "Vegetarian";
}
ArrayList<NameValuePair> nvp = new ArrayList<NameValuePair>();
nvp.add(new BasicNameValuePair("request", "category"));
nvp.add(new BasicNameValuePair("cat", value));
ArrayList<JSONObject> jsondefs = Request.fetchData(this, nvp);
String[] defs = new String[jsondefs.size()];
for (int i = 0; i < jsondefs.size(); i++) {
try {
defs[i] = jsondefs.get(i).getString("Name");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
uFoodAdapter adapter = new uFoodAdapter(this, R.layout.definition_list,
defs);
listView1.setAdapter(adapter);
}
}
要求
package com.thickcrustdesigns.ufood;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;
public class Request {
@SuppressWarnings("null")
public static ArrayList<JSONObject> fetchData(Context context,
ArrayList<NameValuePair> nvp) {
ArrayList<JSONObject> listItems = new ArrayList<JSONObject>();
InputStream is = null;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://co350-11d.projects02.glos.ac.uk/php/database.php");
httppost.setEntity(new UrlEncodedFormEntity(nvp));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection" + e.toString());
}
// convert response to string
String result = "";
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
InputStream stream = null;
StringBuilder sb = null;
while ((result = reader.readLine()) != null) {
sb.append(result + "\n");
}
stream.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
try {
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
JSONObject jo = jArray.getJSONObject(i);
listItems.add(jo);
}
} catch (Exception e) {
Toast.makeText(context.getApplicationContext(), "None Found!",
Toast.LENGTH_LONG).show();
}
return listItems;
}
}
package com.thickcrustdesigns.ufood;
import java.util.ArrayList;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ListView;
public class Results extends ListActivity {
ListView listView1;
enum Category {
Chicken, Beef, Chinese, Cocktails, Curry, Deserts, Fish, ForOne {
public String toString() {
return "For One";
}
},
Lamb, LightBites {
public String toString() {
return "Light Bites";
}
},
Pasta, Pork, Vegetarian
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.definition_main);
listView1 = (ListView) findViewById(android.R.id.list);
Bundle data = getIntent().getExtras();
String category = data.getString("category");
Category cat = Category.valueOf(category);
String value = null;
switch (cat) {
case Chicken:
value = "Chicken";
break;
case Beef:
value = "Beef";
break;
case Chinese:
value = "Chinese";
break;
case Cocktails:
value = "Cocktails";
break;
case Curry:
value = "Curry";
break;
case Deserts:
value = "Deserts";
break;
case Fish:
value = "Fish";
break;
case ForOne:
value = "ForOne";
break;
case Lamb:
value = "Lamb";
break;
case LightBites:
value = "LightBites";
break;
case Pasta:
value = "Pasta";
break;
case Pork:
value = "Pork";
break;
case Vegetarian:
value = "Vegetarian";
}
ArrayList<NameValuePair> nvp = new ArrayList<NameValuePair>();
nvp.add(new BasicNameValuePair("request", "category"));
nvp.add(new BasicNameValuePair("cat", value));
ArrayList<JSONObject> jsondefs = Request.fetchData(this, nvp);
String[] defs = new String[jsondefs.size()];
for (int i = 0; i < jsondefs.size(); i++) {
try {
defs[i] = jsondefs.get(i).getString("Name");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
uFoodAdapter adapter = new uFoodAdapter(this, R.layout.definition_list,
defs);
listView1.setAdapter(adapter);
}
}