Android:无法选择自定义列表视图

时间:2012-09-17 12:28:48

标签: android listview

我正在使用游标适配器来获取自定义列表视图。我有两个xml文件。

a.xml包含Linearlayout Listview

y.xml有LinearlayoutTextview

当我看到列表视图时,我无法选择。我正在使用setOnItemClickListener从列表视图中选择一个项目。

我试图搜索这个并且发现他们中的许多人都有同样的问题。我尝试将它应用于xml中的textview android:focusable="false并以编程方式。

此问题的解决方法是什么?

A.XML

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scroller"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true" >

     <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
           />

               <TextView
            android:id="@+id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
           />
     </LinearLayout>
    </ScrollView>

y.xml

LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

3 个答案:

答案 0 :(得分:0)

也许这个问题是这样的:button in expandable listview android

试试这个:

mTextView.setFocuseable(false);

在您的.java类中而不是您的xml。

答案 1 :(得分:0)

看看这个,希望它能给你一些指示,遗憾的是,如果没有看到你的整个java,就无法做多少!:

public class CRUDonDB extends ListActivity {

private final String SAMPLE_DB_NAME = "myFriendsDb";
private final String SAMPLE_TABLE_NAME = "friends";

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

    ListView lv = getListView();

    ArrayList<String> results = new ArrayList<String>();
    SQLiteDatabase sampleDB = null;

    try {
        sampleDB =  this.openOrCreateDatabase(SAMPLE_DB_NAME, MODE_PRIVATE, null);

        sampleDB.execSQL("CREATE TABLE IF NOT EXISTS " +
                SAMPLE_TABLE_NAME +
                " (LastName VARCHAR, FirstName VARCHAR," +
                " Country VARCHAR, Age INT(3));");

        sampleDB.execSQL("INSERT INTO " +
                SAMPLE_TABLE_NAME +
                " Values ('Doe','John','UK',22);");
        sampleDB.execSQL("INSERT INTO " +
                SAMPLE_TABLE_NAME +
                " Values ('Smith','Robert','UK',20);");
        sampleDB.execSQL("INSERT INTO " +
                SAMPLE_TABLE_NAME +
                " Values ('Walton','Henry','UK',10);");

        Cursor c = sampleDB.rawQuery("SELECT FirstName, LastName, Country, Age FROM " +
                SAMPLE_TABLE_NAME +
                " where Age >= 10 LIMIT 5", null);

        if (c != null ) {
            if  (c.moveToFirst()) {
                do {
                    String firstName = c.getString(c.getColumnIndex("FirstName"));
                    String lastName = c.getString(c.getColumnIndex("LastName"));
                    String country = c.getString(c.getColumnIndex("Country"));
                    int age = c.getInt(c.getColumnIndex("Age"));
                    results.add("" + firstName + " " + lastName + " " + country + " " +age);
                }while (c.moveToNext());
            } 
        }

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

        lv
        .setOnItemClickListener(mOnItemListViewClickListener);


    } catch (SQLiteException se ) {
        Log.e(getClass().getSimpleName(), "Could not create or Open the database");
    } finally {
        if (sampleDB != null) 
            sampleDB.execSQL("DELETE FROM " + SAMPLE_TABLE_NAME);
            sampleDB.close();
    }
}

private final OnItemClickListener mOnItemListViewClickListener = new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {

        String item = (String) parent.getItemAtPosition(position);
        Toast.makeText(getBaseContext(), "You have chosen " +item, Toast.LENGTH_LONG).show();
};
};

}

答案 2 :(得分:0)

删除了<scrollview>,它就像一个魅力。