onClickListener for listview的onItemClickListener内的按钮在单击项目之前不起作用

dil*_*ram 2 android listview

我有一个listview在android中显示来自数据库的人员信息.将listview被赋予一个OnItemClickListener() .在OnItemClickListener()中,我还onClickListener()为listview项中的按钮添加了一些内容.在运行应用程序时,列表视图中的按钮不可单击,或者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();
                }
            });

        }
    });
Run Code Online (Sandbox Code Playgroud)

我正在使用的适配器是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);
Run Code Online (Sandbox Code Playgroud)

Muh*_*nna 5

行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>
Run Code Online (Sandbox Code Playgroud)