监听器无法在列表视图上工作

时间:2013-03-07 07:21:27

标签: android android-layout android-listview

我的监听器列表视图工作正常,但当我在 textview 中添加 android:autoLink:" web" 属性时> row.xml ,List Listener不起作用。为什么会如此呢? 这是row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginTop="10dp" 
   >

    <TextView
        android:id="@+id/ID"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:text="First" />

    <TextView
        android:id="@+id/Name"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="56dp"
        android:text="Second" />

    <TextView

          android:autoLink="web"
        android:id="@+id/Link"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/ID"
        android:layout_marginTop="18dp"
        android:text="Third" />

</RelativeLayout>

这里是listview.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="4dp"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Favourtes"
        android:textSize="25sp"
        android:textStyle="bold" >
    </TextView>

    <ListView
        android:id="@+id/listforplace"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scrollbars="vertical" >
    </ListView>

</LinearLayout>

这里.java类:

public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.bookmark);
        // loadSharedPreferencesforplacelist();
        list = (ListView) findViewById(R.id.listforplace);
        myAdapter = new DbManager(this);
        cursor = myAdapter.getAllfav();
        // updateList();
        String[] from = new String[] {/* DbManager.PlaceID, */DbManager.ID,
                DbManager.name, DbManager.referance };
        int[] to = new int[] { R.id.ID, R.id.Name, R.id.Link };
        cursorAdapter = new SimpleCursorAdapter(this, R.layout.bookmark_row,
                cursor, from, to);

        list.setAdapter(cursorAdapter);
        // cursorAdapter.setViewBinder(new ScoreHistoryBinder());
        list.setOnItemClickListener(listOnItemClickListener);

    }

    public ListView.OnItemClickListener listOnItemClickListener = new ListView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            Cursor cursor = (Cursor) parent.getItemAtPosition(position);
            /* final int */placeidfordelete = cursor.getInt(cursor
                    .getColumnIndex(DbManager.ID));
            // int placename =
            // cursor.getString(cursor.getColumnIndex(DbManager.ID));
            placelongi = cursor
                    .getString(cursor.getColumnIndex(DbManager.name));
            placelati = cursor.getString(cursor
                    .getColumnIndex(DbManager.referance));
            placealti = cursor.getString(cursor.getColumnIndex(DbManager.des));

            AlertDialog.Builder builder = new AlertDialog.Builder(viewmark.this);
            builder.setTitle("Delete");
            builder.setIcon(android.R.drawable.ic_dialog_info);

            builder.setMessage("Delete marks");
            builder.setPositiveButton("Delete",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface arg0, int arg1) {

                            myAdapter.delete_byID_place(placeidfordelete);
                            updateList();

                        }
                    });
            builder.setNegativeButton("All delete",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface arg0, int arg1) {
                        }

                    });

            AlertDialog diag = builder.create();
            diag.show();

        }
    };

    @Override
    protected void onDestroy() { // TODO Auto-generated method stub
        super.onDestroy();
        cursor.close();
        myAdapter.close();

    }

    @SuppressWarnings({ "deprecation" })
    private void updateList() {
        cursor.requery();
        cursor.close();
        // //emptyFormFields();
    }

}

4 个答案:

答案 0 :(得分:1)

这是因为autoLink web属性导致TextView元素窃取点击事件并阻止它们冒泡到ListView。

答案 1 :(得分:1)

这只是建议:

<TextView
    android:id="@+id/txtViewWeb"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Web: www.pareshnmayani.wordpress.com"
    android:autoLink="web"
    android:textSize="16sp"
    android:layout_margin="5dip">
</TextView>

检查一下:http://www.technotalkative.com/android-textview-autolink-attribute/

根据@Berdon的回答它没有用,那么你必须用CustomAdapter

来做

https://stackoverflow.com/a/12813906/1168654

答案 2 :(得分:0)

如果您有任何可点击的视图,如Button,ImageButton或其他类似的视图,onItemClickListener将无效。

解决方案:您应该在BaseAdapter的getView()方法中获取行的根视图,并在rootView上设置onClickListener。但是在onCLickListener中,您将无法获得行的位置已经克隆了..对于获取位置,你可以在getView()中引用onClick()方法。

            ViewGroup listView = (ViewGroup) v.getParent();

            for(int i=1;i<listView.getChildCount();i++){

                listView.getChildAt(i).equals(v);

                if(listView.getChildAt(i).equals(v)){
                    //"clicked position" = i
                    toAttachmentViewer.putExtra("Name", attachments.get(i-1).getName());
                    toAttachmentViewer.putExtra("MasterId", attachments.get(i-1).getMastedId());
                }
            }

以下是整个Adapter类的代码

private class AttachmentsListViewAdapter extends BaseAdapter {

    List<Attachment> attachments;
    TextView txtHeader;
    TextView txtPdfName;

    public AttachmentsListViewAdapter(List<Attachment> attachments) {

        this.attachments = attachments;
        parentview=new View[this.attachments.size()+1];
    }

    @Override
    public int getCount() {

        return attachments.size()+1;
    }

    @Override
    public Object getItem(int arg0) {

        return null;
    }

    @Override
    public long getItemId(int arg0) {

        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        if (position==0) {

            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.order_detail_item_header, null);
            txtHeader=(TextView) convertView.findViewById(R.id.header_textView);
            txtHeader.setText(getResources().getString(R.string.attachments));

        } else if (position>0) {

            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.order_attachments_item, null);
            txtPdfName=(TextView) convertView.findViewById(R.id.pdf_textView);
            txtPdfName.setText(attachments.get(position-1).getName());              
        }

        convertView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                String name,url;
                Intent toAttachmentViewer = new Intent(OrderDetailActivity.this,AttachmentViewerActivity.class);

                if(attachments.get(0).getName()!=null) {

                    name=attachments.get(0).getName();


                } else {

                    name="";
                }

                if(attachments.get(0).getUri()!=null) {

                    url=attachments.get(position-1).getUri();
                } else {

                    url="";
                }

                toAttachmentViewer.putExtra("Name",name);
                toAttachmentViewer.putExtra("URL", url);
                ViewGroup listView = (ViewGroup) v.getParent();

                for(int i=1;i<listView.getChildCount();i++){

                    listView.getChildAt(i).equals(v);

                    if(listView.getChildAt(i).equals(v)){

                        toAttachmentViewer.putExtra("Name", attachments.get(i-1).getName());
                        toAttachmentViewer.putExtra("MasterId", attachments.get(i-1).getMastedId());
                    }
                }
                startActivity(toAttachmentViewer);
            }
        });
        return convertView;
    }
}

答案 3 :(得分:0)

尝试使用autoLink在TextView上设置“android:focusable =”false“”,以便将焦点放在listitem的rootview上,然后它可以注册click而不是textview。

请参阅: stackoverflow.com/questions/1121192/android-custom-listview-unable-to-click-on-items