簡體   English   中英

ListView中的onItemClick不起作用

[英]onItemClick in ListView doesn't work

我無法獲取Toast消息,因為從不調用onItemClick Log-cat沒有顯示任何錯誤。 如果我在任何地方出錯,請查看我的代碼並糾正我。 我用過數組適配器。

public class Open extends ListActivity {
    DbAdapter mDb = new DbAdapter(this);

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

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

        ListView listContent = (ListView) findViewById(android.R.id.list);

        mDb.open();
        Cursor cursor =mDb.fetchAllPrograms();
        startManagingCursor(cursor);

        String[] from = new String[] { DbAdapter.KEY_TITLE };
        int[] to = new int[] { R.id.textViewName };

        SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, 
    R.layout.row, cursor, from, to);
        listContent.setAdapter(cursorAdapter);

        //Onclick ListView setlistener
        listContent.setTextFilterEnabled(true);
        listContent=getListView();


            listContent.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View 

    view,int position, long id) {
                    Toast.makeText(getApplicationContext(),
   "Working!", 
                    Toast.LENGTH_LONG).show();    
                }
            });

    }
}

Row.xml如下:

<?xml version="1.0" encoding="utf-8"?>
<TextView
    android:id="@+id/textViewName"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_marginTop="10dp"
     android:layout_marginBottom="10dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="25sp"
    android:textColor="#0000FF"
    android:textIsSelectable="true"
     />

我遇到了類似的問題。 我的onItemClick方法不僅被忽略了,而且我的List Selector也無法正常工作( android:listSelector 。)

原來它是我的TextView行元素上的android:textIsSelectable屬性。 嘗試將其設置為false

在我的情況下,問題是在android:longClickable="true" 從xml中刪除此行后,單擊偵聽器再次開始工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM