OnListItemClick事件无法正常工作

时间:2014-06-20 20:47:37

标签: android listactivity

我一直面临一个问题......有五个不同的Java类MainActivity,Application,ApplicationAdapter,FetchData,FetchDataListener ..我在ListView中绑定来自api的mysql数据...但是我试图点击一下ListView上的事件,但我收到错误....这里是代码...

MainActivity.java

 @Override
public void onFetchComplete(List<Applicity> data) {
    // dismiss the progress dialog
    if(dialog != null)  dialog.dismiss();
    // create new adapter
    ApplicationAdapter adapter = new ApplicationAdapter(this, data);
    // set the adapter to list
    setListAdapter(adapter);        
}
public void onListItemClick(ListView l, View v, int position, long id) {
    Applicity app= new Applicity();
    int p =app.getPosition(position);
    Toast.makeText(this,p,Toast.LENGTH_LONG).show();
} 

ApplicationAdapter.java

public class ApplicationAdapter extends ArrayAdapter<Applicity>{
private List<Applicity> items;

public ApplicationAdapter(Context context, List<Applicity> items) {
    super(context, R.layout.app_custom_list, items);
    this.items = items;
}

@Override
public int getCount() {
    return items.size();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;

    if(v == null) {
        LayoutInflater li = LayoutInflater.from(getContext());
        v = li.inflate(R.layout.app_custom_list, null);            
    }

    Applicity app = items.get(position);

    if(app != null) {
        ImageView icon = (ImageView)v.findViewById(R.id.appIcon);
        TextView titleText = (TextView)v.findViewById(R.id.titleTxt);
        LinearLayout ratingCntr = (LinearLayout)v.findViewById(R.id.ratingCntr);
        TextView dlText = (TextView)v.findViewById(R.id.dlTxt);

        if(icon != null) {
            Resources res = getContext().getResources();
            String sIcon = "com.sj.jsondemo:drawable/" + app.getIcon();
            icon.setImageDrawable(res.getDrawable(res.getIdentifier(sIcon, null, null)));
        }

        if(titleText != null) titleText.setText(app.getTitle());

        if(dlText != null) {
            NumberFormat nf = NumberFormat.getNumberInstance();
            dlText.setText(nf.format(app.getTotalDl())+" dl");            
        }

        if(ratingCntr != null && ratingCntr.getChildCount() == 0) {        
            /*
             * max rating: 5
             */
            for(int i=1; i<=5; i++) {
                ImageView iv = new ImageView(getContext());

                if(i <= app.getRating()) {
                    iv.setImageDrawable(getContext().getResources().getDrawable(R.drawable.start_checked));
                }
                else {                
                    iv.setImageDrawable(getContext().getResources().getDrawable(R.drawable.start_unchecked));
                }

                ratingCntr.addView(iv);
            }
        }
    }

    return v;
}

}

Applicity.java

public class Applicity {
private String title;
private long totalDl;
private int rating;
public int position;
private String icon;

public String getTitle() {
    return title;
}
public void setTitle(String title) {
    this.title = title;
}
public long getTotalDl() {
    return totalDl;
}
public void setTotalDl(long totalDl) {
    this.totalDl = totalDl;
}
public int getRating() {
    return rating;
}
public void setRating(int rating) {
    this.rating = rating;
}
public String getIcon() {
    return icon;
}
public void setIcon(String icon) {
    this.icon = icon;
}
public int getPosition(int arg){
    return position;
}    

}

1 个答案:

答案 0 :(得分:0)

使用OnItemClickListener。您正在创建一个新的自定义方法,而不是OnItemClickListener。

lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //Do something here. 
    }
});