gpt4 book ai didi

java - onItemClickListener 的应用程序不工作

转载 作者:行者123 更新时间:2023-11-29 05:25:14 27 4
gpt4 key购买 nike

我正在申请。我想从网络上的 Json 文件中获取一些信息。这行得通。现在我想要一个 onItemClickListener 到带有内容的 ListView 的项目。 onItemClickListener 必须将点击列表的信息发送到我的新 Activity 。

我的 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com