我正在申请。我想从Web上的Json文件中获取一些信息。这可行。现在我想拥有一个onItemClickListener到带有内容的listview的项目。 onItemClickListener必须将单击列表的信息发送到我的新活动。
我的MainActiviy.java代码是:
package com.jitesh.androidjsonparser;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class MainActivity extends ListActivity {
private Context context;
private static String url = "http://docs.blackberry.com/sampledata.json"; // URL VAN HET JSON BESTAND
private static final String TAG_VTYPE = "vehicleType"; //VELDEN UIT DE JSON HALEN
private static final String TAG_VCOLOR = "vehicleColor";
private static final String TAG_FUEL = "fuel";
private static final String TAG_TREAD = "treadType";
private static final String TAG_OPERATOR = "approvedOperators";
private static final String TAG_NAME = "name";
private static final String TAG_POINTS = "experiencePoints";
ArrayList<HashMap<String, String>> jsonlist = new ArrayList<HashMap<String, String>>();
ListView lv ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new ProgressTask(MainActivity.this).execute();
}
private class ProgressTask extends AsyncTask<String, Void, Boolean> {
private ProgressDialog dialog;
private ListActivity activity;
// private List<Message> messages;
public ProgressTask(ListActivity activity) {
this.activity = activity;
context = activity;
dialog = new ProgressDialog(context);
}
/** progress dialog to show user that the backup is processing. */
/** application context. */
private Context context;
protected void onPreExecute() {
this.dialog.setMessage("Progress start");
this.dialog.show();
}
@Override
protected void onPostExecute(final Boolean success) {
if (dialog.isShowing()) {
dialog.dismiss();
}
ListAdapter adapter = new SimpleAdapter(context, jsonlist,
R.layout.list_item, new String[] { TAG_VTYPE, TAG_VCOLOR,
TAG_FUEL, TAG_TREAD }, new int[] {
R.id.vehicleType, R.id.vehicleColor, R.id.fuel,
R.id.treadType });
setListAdapter(adapter);
// selecting single ListView item
lv = getListView();
}
protected Boolean doInBackground(final String... args) {
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONArray json = jParser.getJSONFromUrl(url);
for (int i = 0; i < json.length(); i++) {
try {
JSONObject c = json.getJSONObject(i);
String vtype = c.getString(TAG_VTYPE);
String vcolor = c.getString(TAG_VCOLOR);
String vfuel = c.getString(TAG_FUEL);
String vtread = c.getString(TAG_TREAD);
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_VTYPE, vtype);
map.put(TAG_VCOLOR, vcolor);
map.put(TAG_FUEL, vfuel);
map.put(TAG_TREAD, vtread);
jsonlist.add(map);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
}
}
有人可以通过onItemClickListener帮助我吗,我必须在哪个位置设置此onItemClickListener?
@Override protected void onListItemClick(ListView lv, View v, int position, long id) {
Intent i = new Intent(getApplicationContext(), single_list_item_view.class);
String vehicleType = ((TextView) v).getText().toString();
i.putExtra("TAG_VTYPE", vehicleType);
startActivity(i);
}
最佳答案
您正在扩展ListActivity
,因此不需要onItemClickListener
。您可以直接覆盖onListItemClick(ListView l, View v, int position, long id)
。当选择列表中的项目时,将调用此方法。
与onCreate一样,您可以对onListItemClick
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Intent i = new Intent(this, single_list_item_view.class);
HashMap<String, String> itemPressed = (HashMap<String, String>)l.getItemAtPosition(position);
String vehicleType = itemPressed.get(TAG_VTYPE);
i.putExtra("TAG_VTYPE", vehicleType);
startActivity(i);
}
关于java - onItemClickListener的应用程序无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22913082/