OnListItemClick事件不起作用

时间:2014-06-20 16:32:55

标签: android listview listactivity

我遇到了一个问题......有五种不同的Java类MainActivityApplicationApplicationAdapterFetchDataFetchDataListener .. 我在ListView中绑定来自api的mysql数据...但是我试图在ListView上触发点击事件但我收到错误....这里是代码......

MainActivity.Java

@Override
public void onFetchComplete(List<Application> data) {
    if(dialog != null)  dialog.dismiss();
    ApplicationAdapter adapter = new ApplicationAdapter(this, data);
    setListAdapter(adapter);              
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    Application app= new Application();
    int p =app.getPosition(position);

    Toast.makeText(this,p,Toast.LENGTH_LONG).show();
}

ApplicationAdapter.Java

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

    public ApplicationAdapter(Context context, List<Application> 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);            
        }
        Application 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) {        
                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;
    }
}

Application.java

public class Application {
    private String title;
    private long totalDl;
    private int rating;
    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;
    }
}

得到错误......

 06-20 14:28:18.937: I/Choreographer(3740): Skipped 45 frames!  The application may be doing too much work on its main thread.
06-20 14:28:20.267: W/ResourceType(3740): No package identifier when getting value for resource number 0x00000000
06-20 14:28:20.277: D/AndroidRuntime(3740): Shutting down VM
06-20 14:28:20.277: W/dalvikvm(3740): threadid=1: thread exiting with uncaught exception (group=0xb2a31ba8)
06-20 14:28:20.367: E/AndroidRuntime(3740): FATAL EXCEPTION: main
06-20 14:28:20.367: E/AndroidRuntime(3740): Process: com.sj.jsondemo, PID: 3740
06-20 14:28:20.367: E/AndroidRuntime(3740): android.content.res.Resources$NotFoundException: String resource ID #0x0
06-20 14:28:20.367: E/AndroidRuntime(3740):     at android.content.res.Resources.getText(Resources.java:244)
06-20 14:28:20.367: E/AndroidRuntime(3740):     at android.widget.Toast.makeText(Toast.java:268)
06-20 14:28:20.367: E/AndroidRuntime(3740):     at com.sj.jsondemo.MainActivity.onListItemClick(MainActivity.java:41)
06-20 14:28:20.367: E/AndroidRuntime(3740):     at android.app.ListActivity$2.onItemClick(ListActivity.java:319)
06-20 14:28:20.367: E/AndroidRuntime(3740):     at android.widget.AdapterView.performItemClick(AdapterView.java:299)
06-20 14:28:20.367: E/AndroidRuntime(3740):     at android.widget.AbsListView.performItemClick(AbsListView.java:1113)
06-20 14:28:20.367: E/AndroidRuntime(3740):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2904)
06-20 14:28:20.367: E/AndroidRuntime(3740):     at android.widget.AbsListView$3.run(AbsListView.java:3638)
06-20 14:28:20.367: E/AndroidRuntime(3740):     at android.os.Handler.handleCallback(Handler.java:733)
06-20 14:28:20.367: E/AndroidRuntime(3740):     at android.os.Handler.dispatchMessage(Handler.java:95)
06-20 14:28:20.367: E/AndroidRuntime(3740):     at android.os.Looper.loop(Looper.java:136)
06-20 14:28:20.367: E/AndroidRuntime(3740):     at android.app.ActivityThread.main(ActivityThread.java:5017)
06-20 14:28:20.367: E/AndroidRuntime(3740):     at java.lang.reflect.Method.invokeNative(Native Method)
06-20 14:28:20.367: E/AndroidRuntime(3740):     at java.lang.reflect.Method.invoke(Method.java:515)
06-20 14:28:20.367: E/AndroidRuntime(3740):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-20 14:28:20.367: E/AndroidRuntime(3740):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-20 14:28:20.367: E/AndroidRuntime(3740):     at dalvik.system.NativeStart.main(Native Method)
06-20 14:28:23.067: I/Process(3740): Sending signal. PID: 3740 SIG: 9

0 个答案:

没有答案