2011-01-17 33 views
19

Tôi có một tuỳ chỉnh ListView. Điều này ListView chứa 1 Hình ảnh và 6 TextView s. Để truy xuất giá trị tôi đã tạo setOnItemClickListener(...). Bất cứ khi nào tôi nhấp vào ListView làm cách nào để tôi có thể truy xuất tất cả dữ liệu từ 6 TextView s?setOnItemClickListener trên ListView tùy chỉnh

Trả lời

2

Nếu trong trình nghe bạn có bố cục gốc của mục (giả sử itemLayout) và bạn đã đưa một số id vào các bản xem trước văn bản, bạn có thể nhận chúng bằng một cái gì đó như itemLayout.findViewById(R.id.textView1).

+0

Cảm ơn bạn rất nhiều bigstones. – kangalert

+0

@kangalert bạn được chào đón.nhớ đánh dấu các câu trả lời được chấp nhận (nhấp vào dấu kiểm bên cạnh câu trả lời), để cho mọi người biết đó là giải pháp làm việc. nó cũng làm tăng danh tiếng của bạn và của người trả lời :) – bigstones

52

Sample Code:

ListView list = (ListView) findViewById(R.id.listview); 
list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
     Object listItem = list.getItemAtPosition(position); 
    } 
}); 

Trong đoạn code mẫu trên, listItem nên chứa các dữ liệu được chọn cho textView.

+3

Tôi không thể cảm nhận được cảm ứng ngay cả khi tôi viết một bánh mì nướng bên trong onItemNhấp vào nó không hiển thị – abhishek

+0

@abhishek, cùng một vấn đề với tôi cũng –

10

tôi cũng đã có vấn đề tương tự .. Nếu chúng ta suy nghĩ logic một chút chúng ta có thể nhận được câu trả lời .. Nó làm việc cho tôi rất tốt .. Tôi hy vọng u sẽ nhận được nó ..

  1. listviewdemo.xml

    <ListView 
        android:id="@+id/listview" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:paddingBottom="30dp" 
        android:paddingLeft="10dp" 
        android:paddingRight="10dp" /> 
    

  2. listviewcontent.xml - lưu ý rằng TextView - android:id="@+id/txtLstItem"

    <LinearLayout 
        android:id="@+id/listviewcontentlayout" 
        android:layout_width="0dp" 
        android:layout_height="fill_parent" 
        android:layout_weight="1" 
        android:orientation="horizontal"> 
    
        <ImageView 
         android:id="@+id/img1" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_marginRight="6dp" /> 
    
        <LinearLayout 
         android:layout_width="0dp" 
         android:layout_height="fill_parent" 
         android:layout_weight="1" 
         android:orientation="vertical"> 
    
         <TextView 
          android:id="@+id/txtLstItem" 
          android:layout_width="match_parent" 
          android:layout_height="wrap_content" 
          android:gravity="left" 
          android:shadowColor="@android:color/black" 
          android:shadowRadius="5" 
          android:textColor="@android:color/white" /> 
    
        </LinearLayout> 
    
        <ImageView 
         android:id="@+id/img2" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_marginRight="6dp" /> 
    </LinearLayout> 
    

  3. ListViewActivity.java - Lưu ý rằngview.findViewById(R.id.txtLstItem) - như chúng ta thiết lập các giá trị cho TextView bởi setText() phương pháp chúng tôi nhận được văn bản từ TextView by View đối tượng được trả về theo phương thức onItemClick. OnItemClick() trả về chế độ xem hiện tại.

    TextView v=(TextView) view.findViewById(R.id.txtLstItem); 
    Toast.makeText(getApplicationContext(), "selected Item Name is "+v.getText(), Toast.LENGTH_LONG).show();** 
    

    Sử dụng logic đơn giản này, chúng tôi có thể nhận được các giá trị khác như CheckBox, RadioButton, ImageView, vv

    ListView List = (ListView) findViewById(R.id.listview); 
    cursor = cr.query(CONTENT_URI,projection,null,null,null); 
    adapter = new ListViewCursorAdapter(ListViewActivity.this, R.layout.listviewcontent, cursor, from, to); 
    
    cursor.moveToFirst(); 
    
    // Let activity manage the cursor 
    startManagingCursor(cursor); 
    
    List.setAdapter(adapter); 
    List.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
         @Override 
         public void onItemClick (AdapterView <?> adapter, View view,int position, long arg){ 
          // TODO Auto-generated method stub 
          TextView v = (TextView) view.findViewById(R.id.txtLstItem); 
          Toast.makeText(getApplicationContext(), "selected Item Name is " + v.getText(), Toast.LENGTH_LONG).show(); 
         } 
        } 
    ); 
    
+0

Tôi có cùng mã nhưng nó không hoạt động cho tôi –

2

Nếu nó giúp mọi người, tôi thấy rằng vấn đề đã được tôi đã có một android : sự kiện onClick trong tệp bố cục của tôi (mà tôi đã tăng cao cho các hàng ListView). Điều này đã thay thế sự kiện onItemClick.

1

Nếu câu trả lời ở trên không hoạt động có thể bạn không thêm giá trị trả lại vào phương thức getItem trong bộ điều hợp tùy chỉnh, xem this câu hỏi và xem câu trả lời đầu tiên.

Các vấn đề liên quan