2013-03-07 118 views
1

工作,我有監聽器工作正常列表視圖,但是當我添加機器人:自動鏈接:「網絡」在row.xml一個的TextView財產清單監聽不行。爲什麼如此呢? 這裏是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(); 
    } 

} 
+2

我們可以有代碼嗎? – 2013-03-07 07:21:48

+0

請發表您的代碼...... – androidgeek 2013-03-07 07:23:11

+0

發佈代碼............... – 2013-03-07 07:26:39

回答

0

做當你有任何可點擊的視圖像Button,ImageButton或類似的行時,onItemClickListene r不起作用。

解決方案:你應該讓你的行的根視圖在你BaseAdapter的getView()方法,並設置onClickListener在rootView。但在onCLickListener你不會得到已經clciked行的位置..爲了獲得位置,你可以在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()); 
       } 
      } 

以下爲整個適配器類

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; 
    } 
} 
1

這是因爲自動鏈接網絡屬性導致的TextView元素竊取單擊事件並阻止它們冒泡到ListView。

0

嘗試設置代碼「機器人:可聚焦=」假「」關於與自動鏈接TextView的,以有重點的ListItem的rootview然後它可以註冊點擊而不是textview。

看到: stackoverflow.com/questions/1121192/android-custom-listview-unable-to-click-on-items