Android:Listview onClick无法正常工作?

时间:2011-01-31 11:19:20

标签: android

我有Listview,其数据来自website.data。它在Listview中正确显示,但其onListItemClick未正确触发。

public class OutKrys extends ListActivity   {
private ProgressDialog progress;
ListView listView1;
private ArrayList<Post> listItems = new ArrayList<Post>();
private PostAdapter post_Adapter;
public static DefaultHttpClient httpclient = new DefaultHttpClient();
private class PostAdapter extends ArrayAdapter<Post> {

    private ArrayList<Post> items = new ArrayList<Post>();

    public PostAdapter(Context context, int textViewResourceId,
            ArrayList<Post> items) {
        super(context, textViewResourceId, items);
        this.items = items;
    }

     @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        try {
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.outkrys, null);
            }
            Post o = items.get(position);
            if (o != null) {
                TextView tt = (TextView) v.findViewById(R.id.Story);
                TextView bt = (TextView) v.findViewById(R.id.userName);
                TextView tm = (TextView) v.findViewById(R.id.Time);
                ImageView iv=(ImageView)v.findViewById(R.id.OutKryImageView01);
                if (tt != null) {
                    tt.setText(o.getStory());
                }
                if (bt != null) {
                    bt.setText(o.getUserName());
                }
                if (tm != null) {
                    tm.setText(o.getTime());
                    if(iv!=null)
                    {
                          Drawable drawable = LoadImageFromWebOperations("abc.com/"+o.getImagePath());
                            iv.setImageDrawable(drawable);

                    }

                }
            }
            return v;
        } catch (Exception e) {
            progress.dismiss();
            return null;
        }
    }
}
private Drawable LoadImageFromWebOperations(String url)

{
    try
    {

        InputStream is = (InputStream) new URL(url).getContent();
        Drawable d = Drawable.createFromStream(is, "src name");
        return d;
    }catch (Exception e) {
        System.out.println("Exc="+e);
        return null;
    }
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    // Get the item that was clicked
    Object o = this.getListAdapter().getItem(position);
    String keyword = o.toString();
    Toast.makeText(this, "You selected: " + position, Toast.LENGTH_LONG)
            .show();

}
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.listitems);
    //listView1 = (ListView) findViewById(R.id.ListView01);
    try {
        progress = ProgressDialog.show(OutKrys.this, "Please wait...",
                "Loading list", true);
        Thread t = new Thread() {
            public void run() {
                listItems=getList();
                pHandler.post(mUpdateResults);
            }
            };
        t.start();



    } catch (Exception e) {
        //progress.dismiss();
        Toast.makeText(OutKrys.this, "dsadsadsa"+e, Toast.LENGTH_LONG).show();
    }
}
final Handler pHandler = new Handler();

// Create runnable for posting
final Runnable mUpdateResults = new Runnable() {
    public void run() {

        updateTabData();


    }
};
private void updateTabData() {
    try {

        this.post_Adapter = new PostAdapter(this, android.R.layout.simple_list_item_checked,
                listItems);
        this.setListAdapter( new PostAdapter(this, R.layout.outkrys,
                listItems));

        progress.dismiss();






    } catch (Exception e) {
        //progress.dismiss();

    }
}


private ArrayList<Post> getList() {
    try {

        String loginURI = "abcd.com";
        HttpPost httpost = new HttpPost(loginURI);
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("authenticate", "authenticate"));
        nvps.add(new BasicNameValuePair("tab", "Recent"));
        httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
        HttpResponse response = null;
        response = KryAbout.httpclient.execute(httpost);
        HttpEntity entity = response.getEntity();

        Post p = new Post();
        String line = "";
        if (entity != null) {
            InputStream resContent = response.getEntity().getContent();
            line = KryAbout.ReadInputStream(resContent).toString().trim();
        } else {
        }
        listItems.removeAll(listItems);
        JSONArray ja = new JSONArray(line);
        for (int i = 0; i < ja.length(); i++) {
            p = new Post();
            JSONObject jo = (JSONObject) ja.get(i);
            p.setStory(jo.getString("story"));
            p.setFavorite(false);
            p.setTime(jo.getString("time"));
            p.setPid(Integer.parseInt(jo.getString("pid")));
            p.setUserName(jo.getString("username"));
            p.setImagePath(jo.getString("profilepicture"));
            listItems.add(p);
        }
    } catch (Exception e) {
        progress.dismiss();
    }
    return listItems;
}



    }

1 个答案:

答案 0 :(得分:4)

我认为你应该使用

@Override
protected void onListItemClick(ListView l, View v, int position, long id) 
{
  .....
}

而不是

 @Override
 public void onItemClick(AdapterView<?> a, View v, int position, long id)
 {
    .....
 }