android - OnClickItem 不工作 Android

标签 android android-listview

我有一个 ListView ,它是在从服务器获取数据后生成的。现在我想让每个项目都可以触摸或点击。但它不工作。任何帮助将不胜感激

这是我的 xml 文件:

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

我的 Activity :

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



        }

        }
}

最佳答案

您可以按照以下方法在膨胀 View 上应用 onClickListner,然后无需实现 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;
    }


    }`

关于android - OnClickItem 不工作 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17760389/

相关文章:

android - OpenGL ES Matrix/Vector-Transform 不同于桌面 OpenGL

android - 无法评估模块 FacebookLib : Configuration with name 'debug' not found

android - 绘图引用

android - 应用 LayoutParams 隐藏 View

java - 由自定义 ArrayAdapter 中按钮的事件创建的 DialogFragment 始终具有相同的位置

android - ListView 内容不会使用 SimpleCursorAdapter 显示,除非我单击/点击列表项

android - 在 Samsung Galaxy 设备上隐藏 "NFC Tag type not supported"错误

java - 如何使用if条件Android更改listview项目背景颜色

java - Textview文字大小应为editText文字大小并显示在listview中

Android CursorAdapter 在错误的列表项中绘制内容