gpt4 book ai didi

android - onItemClick 得到错误的项目

转载 作者:行者123 更新时间:2023-11-30 04:06:20 26 4
gpt4 key购买 nike

我的 OnClickListener 似乎从 ListView 中选择了错误的项目。 Listener 总是选择列表中的第一项。这是我的 Activity,其中 OnClickListener

public class ShowCharts extends ListActivity{
ProgressDialog pDialog;
static String url_read = "PHP DATA LINK";
static String TAG_ID="id",
TAG_CHARTS="charts",
TAG_SUCCESS = "success",
TAG_INTERPRET="interpret",
TAG_TITEL="titel",
TAG_ALBUM="album",
TAG_ALBUMCOVER="albumcover",
TAG_LIKES="likes";

ArrayList<HashMap<String, String>> songslist;

JSONParser jParser = new JSONParser();
JSONArray charts = null;

String content;
ListView list;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.charts);

songslist = new ArrayList<HashMap<String, String>>();

new LoadCharts().execute();
list = getListView();
list.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// getting values from selected ListItem
String cid = ((TextView)findViewById(R.id._id)).getText().toString();

// Starting new intent
Intent in = new Intent(getApplicationContext(),EditSong.class);
// sending id to next activity
in.putExtra(TAG_ID, cid);

// starting new activity and expecting some response back
startActivityForResult(in, 100);
}
});

那是下一个 Activity,我之前通过 Acitvity 获取数据:

public class EditSong extends Activity {

EditText txtInterpret;
EditText txtTitel;
EditText txtAlbum;
EditText txtAlbumcover;
Button btnSpeichern;
Button btnLoeschen;

String cid;
// Progress Dialog
private ProgressDialog pDialog;

// JSON parser class
JSONParser jsonParser = new JSONParser();

// single product url
private static final String url_details = "PHP DETAILS LINK";

// url to update product
private static final String url_update = "PHP UPDATE LINK";

// url to delete product
private static final String url_delete = "PHP DELTE LINK";

// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_CHARTS = "charts";
private static final String TAG_ID = "id";
private static final String TAG_INTERPRET = "interpret";
private static final String TAG_TITEL = "titel";
private static final String TAG_ALBUMCOVER = "albumcover";
private static final String TAG_ALBUM = "album";

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.editsong);

// save button
btnSpeichern = (Button) findViewById(R.id._speichernButton);
btnLoeschen = (Button) findViewById(R.id._loeschen);

// getting product details from intent
Intent i = getIntent();

// getting product id (id) from intent
cid = i.getStringExtra(TAG_ID);

这是我的适配器

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class LazyAdapter extends BaseAdapter {

private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;

public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader = new ImageLoader(activity.getApplicationContext());
}

public int getCount() {
return data.size();
}

public Object getItem(int position) {
return position;
}

public long getItemId(int position) {
return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.list_row, null);

TextView id = (TextView)vi.findViewById(R.id._id); //id
TextView title = (TextView)vi.findViewById(R.id.title); // title
TextView artist = (TextView)vi.findViewById(R.id.artist); // artist name
TextView likes = (TextView)vi.findViewById(R.id.likes); // likes
ImageView thumb_image = (ImageView)vi.findViewById(R.id.list_image); // thumb image

HashMap<String, String> song = new HashMap<String, String>();
song = data.get(position);

// Setting all values in listview
id.setText(song.get(ShowCharts.TAG_ID));
title.setText(song.get(ShowCharts.TAG_TITEL));
artist.setText(song.get(ShowCharts.TAG_INTERPRET));
likes.setText(song.get(ShowCharts.TAG_LIKES));
imageLoader.DisplayImage(song.get(ShowCharts.TAG_ALBUMCOVER), thumb_image);
return vi;
}
}

最佳答案

您需要将 findViewById() 的范围限定到当前行,否则它将始终获取第一次出现的 R.id._id(它应该始终是第一行):

String cid = ((TextView) v.findViewById(R.id._id)).getText().toString();
// add this ^

此外,由于您正在扩展 ListActivity,因此您应该使用 onListItemClick listener已经提供:

@Override
protected void onListItemClick(ListView parent, View view, int position, long id) {
// getting values from selected ListItem
String cid = ((TextView) view.findViewById(R.id._id)).getText().toString();

// Starting new intent
Intent in = new Intent(getApplicationContext(),EditSong.class);
// sending id to next activity
in.putExtra(TAG_ID, cid);

// starting new activity and expecting some response back
startActivityForResult(in, 100);
}

关于android - onItemClick 得到错误的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11593902/

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