我的OnClickListener
似乎从ListView
中选择了错误的项目。侦听器始终选择列表中的第一项。这是我的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);
}
});
多数民众赞成在下一个活动,我之前通过活动获取数据:
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);
}