【问题标题】:onClickListener for buttons inside the onItemClickListener of listview not working before item click列表视图的 onItemClickListener 内的按钮的 onClickListener 在项目单击之前不起作用
【发布时间】:2015-02-02 19:52:03
【问题描述】:

我在 android 中有一个 listview,它显示来自数据库的人员信息。 listview 被赋予 OnItemClickListener() 。在 OnItemClickListener() 内部,我还为列表视图项中的按钮添加了一些 onClickListener()。在运行应用程序时,列表视图中的按钮不可点击或分配给按钮的OnClickListener() 不会被触发。 一旦单击列表视图中的项目,此问题将不再出现。此后所有按钮单击侦听器都在工作。 有人可以给我一个解决方案吗?我正在使用的代码如下。

                    listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            int listrowposition = arg2;
            HashMap<String, String> item = (HashMap<String, String>) arg0
                    .getAdapter().getItem(arg2);
            final String id = item.get("ID");
            Button vd = (Button) arg1.findViewById(R.id.viewDetails);
            // this onclick listener is not working bfore the onitemclick
            // listener
            vd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View arg0) {

                    Toast.makeText(getApplicationContext(),
                            "view details clicked  Id  " + id,
                            Toast.LENGTH_LONG).show();
                }
            });
            Button gd = (Button) arg1.findViewById(R.id.getDirections);
            // this onclick listener is not working bfore the onitemclick
            // listener
            gd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View arg0) {

                    Toast.makeText(getApplicationContext(),
                            "getDirections clicked  Id  " + id,
                            Toast.LENGTH_LONG).show();
                }
            });
            Button ra = (Button) arg1.findViewById(R.id.reqAppointment);
            // this onclick listener is not working bfore the onitemclick
            // listener
            ra.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View arg0) {

                    Toast.makeText(getApplicationContext(),
                            "reqAppointment clicked  Id  " + id,
                            Toast.LENGTH_LONG).show();
                }
            });

        }
    });

我使用的适配器是 SimpleAdapter。适配器详细信息如下。

       String[] from = { "flag", "txt", "cur", "viewDet", "getDirect",
            "reqAppnmnt" };

    // Ids of views in listview_layout
    int[] to = { R.id.flag, R.id.name, R.id.specialization,
            R.id.viewDetails, R.id.getDirections, R.id.reqAppointment };

    // Instantiating an adapter to store each items
    // R.layout.listview_layout defines the layout of each item
    adapter = new SimpleAdapter(getBaseContext(), aList,
            R.layout.listview_layout, from, to);
    new CallToServer(adapter, aList).execute("");
    // Getting a reference to listview of main.xml layout file
    ListView listView = (ListView) findViewById(R.id.listview);
    // Setting the adapter to the listView
    listView.setAdapter(adapter);
    listView.setOnScrollListener(this);

【问题讨论】:

  • 添加您的适配器代码,最好在适配器代码中编写,这样它才能工作
  • @AndoMasahashi 我已经用适配器详细信息编辑了问题
  • 创建自定义适配器。
  • @Rohit5k2 为什么需要这样做。以及 buttonclick 与此的关系。
  • 因为只有当您单击列表视图项时,您的侦听器才会分配给按钮。如果您想一直处于活动状态,则需要制作自定义适配器。

标签: android listview


【解决方案1】:

行 xml 必须有这一行 android:descendantFocusability="blocksDescendants"

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:baselineAligned="false"
    android:descendantFocusability="blocksDescendants"
    android:gravity="center_vertical" >

    // All your widgets here

</LinearLayout>

【讨论】:

    【解决方案2】:

    首先创建一个自定义适配器。这可能会帮助你Custom Adapter for List View

    然后在你的适配器中添加这些方法

    private OnClickListener vdClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            final int position = mListView.getPositionForView((View) v.getParent());
            Log.v(TAG, "vd clicked, row %d", position);
        }
    };
    
    private OnClickListener gdClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            final int position = mListView.getPositionForView((View) v.getParent());
            Log.v(TAG, "gd clicked, row %d", position);
        }
    };
    
    private OnClickListener ravClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            final int position = mListView.getPositionForView((View) v.getParent());
            Log.v(TAG, "ra clicked, row %d", position);
        }
    };
    

    mListView 是您正在使用的列表视图。

    在你的适配器 getView 方法中做这样的事情

    Button vd = (Button) convertView.findViewById(R.id.viewDetails);
    Button gd = (Button) convertView.findViewById(R.id.getDirections);
    Button ra = (Button) convertView.findViewById(R.id.reqAppointment);
    vd.setOnClickListener(vdClickListener);
    gd.setOnClickListener(gdClickListener);
    ra.setOnClickListener(raClickListener);
    

    编辑

    制作自定义简单适配器的通用代码是。您需要在getVeiw() 方法中进行更改。

    public class MyAdapter extends SimpleAdapter{
        HashMap<String, String> map = new HashMap<String, String>();
        public Adapter(Context context, List<? extends Map<String, String>> data,
            int resource, String[] from, int[] to) {
            super(context, data, resource, from, to);
    
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent){
            View row = convertView;
            if (row == null) {
                LayoutInflater mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                row = mInflater.inflate(R.layout.mylistlayout, parent, false);
            }
    
            TextView rw1 = (TextView)findViewById(R.id.row1);
            rw1.setText("my text");
            return row;
        }
    }
    

    【讨论】:

    • 您能否详细解释一下如何使用自定义适配器开发一个包含 2 个文本视图和按钮的列表视图。我仍然没有清楚如何实现自定义适配器
    • 你是如何创建aList的?
    • 我现在使用的是 SimpleAdapter,最初我只是为适配器设置了一个空白列表。然后项目被添加到它稍后和。 notifyDataSetChanged() 被调用。
    • 我已经编辑了我的答案。您需要根据数据加载视图并添加侦听器。
    • 您需要为getView方法中的所有按钮设置点击监听器,并从onItemClick(您之前使用的位置)中删除所有监听器
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-29
    相关资源
    最近更新 更多