onItemClick得到错误的项目

时间:2012-07-21 16:28:49

标签: android listview

我的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);
            }
        });

这是下一个活动,我之前通过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;
}
}

2 个答案:

答案 0 :(得分:1)

你需要将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);
}

答案 1 :(得分:0)

是的,这是因为您没有完全正确地实现BaseAdapter。

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

基本上onItemClick调用getItem函数。您没有在此处引用您的数据。你应该做这样的事情

public Object getItem(int position) {
    return data.get(position);
}