2011-09-28 40 views
15

Tôi đang sử dụng android 2.3.3. Tôi thiết lập một bố cục như thế nào,Android OnItemClickListener không hoạt động

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <ListView android:id="@+id/mainList" android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 
</LinearLayout> 

Và tôi thao tác nó với

package org.dewsworld.ui; 

import org.dewsworld.core.NBConfig; 

import android.app.Activity; 
import android.app.ListActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.ListAdapter; 
import android.widget.ListView; 

public class NewsBotActivity extends ListActivity { 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     setListAdapter(new ArrayAdapter<String>(this, 
              android.R.layout.simple_list_item_1, 
              NBConfig.topics)); 

     ListView listView = (ListView) findViewById(R.id.mainList) ; 
     listView.setOnItemClickListener(new OnItemClickListener() { 

     }); 
    } 
} 

Sử dụng eclise IDE, khi tôi setthe OnItemClickListener nó mang lại cho tôi những lỗi

The method setOnItemClickListener(AdapterView.OnItemClickListener) in the type AdapterView<ListAdapter> is not applicable for the arguments (new 
    OnItemClickListener(){}) 

tôi có thể không sửa lỗi này. [Tôi đã thêm một hình ảnh với các lỗi] imgimg

+1

Bạn đang nhập khẩu AdapterView.OnItemClickListener? Nếu không, hãy thử sử dụng 'mới AdapterView.OnItemClickListener() {' –

+1

'Ctrl + 1' có thể có ích trong nhật thực;) – Gio

+1

nhập android.widget.AdapterView.OnItemClickListener; –

Trả lời

45

Có vẻ như bạn đã nhập OnItemClickListener sai, hãy thử một này để thay thế, và loại bỏ nhập khẩu android.view.View.OnClickListener

import android.widget.AdapterView.OnItemClickListener; 
+0

Wow! Nó hoạt động! Nhưng làm cách nào tôi có thể đặt Trình nghe nào cần nhập? Caz, đây là tự động được tạo bởi nhật thực – Dewsworld

+1

Nhập hoàn tất trong Eclipse, sorta như ctl-shift-o, thường cung cấp cho bạn danh sách các nhập khẩu xung đột để chọn. – Pedantic

5

cách về điền trong cơ thể của đối tượng mới của mình bằng cách xác định onItemClick() chức năng:

public void onItemClick(AdapterView parent, View v, int position, long id) 
{ 
    // Display a messagebox. 
    Toast.makeText(this,"Your Listener Works!",Toast.LENGTH_SHORT).show(); 
} 

thử sử dụng ctrl + shift + o trong nhật thực để tổ chức tất cả hàng nhập khẩu tự động ...

1

Chỉ cần triển khai OnItemClickListener vào lớp học của bạn.

Giống như này:

public class ClassName extends Activity implements OnItemClickListener{}

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