【问题标题】:Android:Not able to select Custom listviewAndroid:无法选择自定义列表视图
【发布时间】:2012-09-17 12:28:47
【问题描述】:

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

a.xml 有一个LinearlayoutListview

y.xml 有 Linearlayout 和两个 Textview

当我看到列表视图时,我无法选择。我正在使用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>

【问题讨论】:

  • 你在哪里添加你的 onClick 监听器?
  • 我们需要查看 XML 和您的 java 代码才能提供帮助,否则只是猜测。
  • @Barak: 已将 xml 更新为我的问题。
  • @fasheikh:我没有使用 onclick 监听器,而是使用 setOnItemClickListener。
  • 好的,你在哪里定义它?如果您可以发布您的java,请

标签: android listview


【解决方案1】:

可能这个问题是这样的:button in expandable listview android

试试这个:

mTextView.setFocuseable(false);

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

【讨论】:

  • :我没有任何按钮,它是 textview。我正在使用 setOnItemClickListener 方法。在我的问题中也更新了我的 xml,仅供参考。
【解决方案2】:

看看这个,希望它能给你一些指导,不幸的是,如果没有看到你的整个 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();
};
};

}

【讨论】:

  • :我正在使用自定义光标适配器,覆盖 newview 和 bindview。
  • 一个带有自定义适配器的示例,在单击时切换复选框
  • :你没有理解我的问题。当我从列表视图中选择一行时,我看不到选择。通常当你从列表视图中单击一行时,你应该看到橙色出现表示该行已被选中。
  • 我已经理解了你的问题,它不一定是橙色的高亮颜色:) 如果没有完整的 java 代码,很难帮助你。因此,我为一个工作示例提供了一个代码,以便您可以将其与您的代码进行比较,并希望发现您的错误。
  • :我没有任何复选框,我只有两个文本视图,我说的是数据库中的值,将每个值放在一个文本视图中。
【解决方案3】:

删除了&lt;scrollview&gt;,它就像一个魅力。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-24
    相关资源
    最近更新 更多