OnClickItem不适用于Android

时间:2013-07-20 08:36:51

标签: android android-listview

我有一个列表视图,它从服务器获取数据后生成。现在我想让每个项目都能够触摸或点击。但它不起作用。任何帮助都是适当的

这是我的xml文件:

 <ListView
        android:id="@+id/promo_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

我的活动:

public class Promo extends ListActivity implements OnTouchListener
,OnGestureListener {

@Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.promotions);
        gestureScanner = new GestureDetector(this);

        RelativeLayout layMain = (RelativeLayout) findViewById(R.id.main_layout);
        layMain.setOnTouchListener((OnTouchListener) this);
         listView = (ListView) findViewById(R.id.promo_list);

         listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub

                Log.i("---------------------","------------------------------------");

            }





         });


        MyTask obj = new MyTask();
        obj.execute("http:www.somelink.com");
    }






    // ////////////////////////////////////////////////////////////////////
    // //////////Background Thread todo Async work////////////////////////
    // ////////////////////////////////////////////////////////////////////
    private class MyTask extends AsyncTask<String, Integer, String> {

        @Override
        protected void onPostExecute(String result) {


            // JSONObject obj = myList.get(2);

             listAdapter = new MyCustomList(getApplicationContext());
             listView.setAdapter(listAdapter);
            super.onPostExecute(result);
        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            // TODO Auto-generated method stub
            super.onProgressUpdate(values);

        }

        @Override
        protected String doInBackground(String... params) {
            String myRespone = null;
            String url = params[0];
            HttpClient client = new DefaultHttpClient();

            HttpGet Get = new HttpGet(url);

            try {
                HttpResponse response = client.execute(Get);

                HttpEntity entity = response.getEntity();
                myRespone = EntityUtils.toString(entity);

            } catch (ClientProtocolException e) {

                e.printStackTrace();

                Log.e("My webservice Response", "ClientProtocolException");

            } catch (IOException e) {

                Log.e("My webservice Response", "IOException");

                e.printStackTrace();
            }
            JSONObject jsonObj;

            if (myRespone != null) {
                try {

                    jsonObj = new JSONObject(myRespone);
                    JSONArray jsonArray = jsonObj.getJSONArray("Promo");
                    Log.d("Promo Count", "" + jsonArray.length());

                    for (int i = 0; i <= jsonArray.length() - 1; i++) {
                        myList.add(jsonArray.getJSONObject(i));

                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                // do nothing
            }
            return null;
        }

    }

    private static class Promo {
        TextView name1, name2, name3, name4;
    }

    private class MyCustomList extends BaseAdapter implements OnItemClickListener{

        private Context context;
        LayoutInflater inflater;

        public MyCustomList(Context context) {

            this.context = context;
            inflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return myList.size();
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int position, View view, ViewGroup parent) {
            Promo promotion;
            if (view == null) {
                view = inflater.inflate(R.layout.promo_listview, null);
                promotion = new Promo();
                view.setTag(promotion);

            } else {
                promo = (Promo) view.getTag();
            }



            return view;
        }

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub



        }

        }
}

2 个答案:

答案 0 :(得分:1)

您可以按照以下步骤在充气视图上应用onClickListener,然后无需实现OnItemClickListener。

private class MyCustomList extends BaseAdapter{

    private Context context;
    LayoutInflater inflater;

    public MyCustomList(Context context) {

        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return myList.size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(final int position, View view, ViewGroup parent) {
        Promo promotion;
        if (view == null) {
            view = inflater.inflate(R.layout.promo_listview, null);
            promotion = new Promo();
            view.setTag(promotion);

        } else {
            promo = (Promo) view.getTag();
        }

        view..setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                System.out.println("position=="+position);
            }
        });

        return view;
    }


    }`

答案 1 :(得分:0)

试试此代码,

public class Promo extends ListActivity{
     ArrayList<String> myList=null;
     @Override
      protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.promotions);
    listView = (ListView) findViewById(R.id.promo_list);
    myList=new ArrayList<String>();
    MyTask obj = new MyTask();
    obj.execute("http:www.somelink.com");
    listView.setOnItemClickListener(new OnItemClickListener() 
    {
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) 
        {
            System.out.println("Name>>>>>>>"+myList.get(position));
                            //Enter Code Here
        }
    });
}
// ////////////////////////////////////////////////////////////////////
// //////////Background Thread todo Async work////////////////////////
// ////////////////////////////////////////////////////////////////////
private class MyTask extends AsyncTask<String, Integer, String> {
    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
    }
    @Override
    protected void onProgressUpdate(Integer... values) {
        // TODO Auto-generated method stub
        super.onProgressUpdate(values);
    }
    @Override
    protected String doInBackground(String... params) {
        String myRespone = null;
        String url = params[0];
        HttpClient client = new DefaultHttpClient();
        HttpGet Get = new HttpGet(url);
        try {
            HttpResponse response = client.execute(Get);
            HttpEntity entity = response.getEntity();
            myRespone = EntityUtils.toString(entity);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
            Log.e("My webservice Response", "ClientProtocolException");
        } catch (IOException e) {
            Log.e("My webservice Response", "IOException");
            e.printStackTrace();
        }
        JSONObject jsonObj;
        if (myRespone != null) {
            try {
                jsonObj = new JSONObject(myRespone);
                JSONArray jsonArray = jsonObj.getJSONArray("Promo");
                Log.d("Promo Count", "" + jsonArray.length());
                for (int i = 0; i <= jsonArray.length() - 1; i++) {
                    myList.add(jsonArray.getJSONObject(i));
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            // do nothing
        }
        return null;
    }
    @Override
    protected void onPostExecute(String result) {
        // JSONObject obj = myList.get(2);
         listAdapter = new MyCustomList(getApplicationContext(),myList);
         listView.setAdapter(listAdapter);
         super.onPostExecute(result);
    }
}
public class MyCustomList extends BaseAdapter{
private ArrayList<String> myList=null;
private LayoutInflater mInflater=null;

public MyCustomList(Context context, ArrayList<String> results) {
      myList= results;
      mInflater = LayoutInflater.from(context);
}
public int getCount() 
{
    return myList.size();
}
public Object getItem(int position) 
{
    return myList.get(position);
}
public long getItemId(int position) 
{
    return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
    ViewHolder holder;
    if (convertView == null) 
    {
    convertView =mInflater.inflate(R.layout.R.layout.promo_listview, null);
    holder = new ViewHolder();
        holder.Name= (TextView) convertView.findViewById(R.id.txtvName);
   } 
   else 
   {
    holder = (ViewHolder) convertView.getTag();
  }
      holder.Name.setText(myList.get(position));
 }
 static class ViewHolder
 {
      TextView Name=null;
 }
}
}
}