android - onListItemClick() 不适用于 Android 中的 ListView

标签 android xml android-widget

以下不会在点击时注册 Toast。事实上,它甚至似乎没有检测到点击。 CustomListTitle 扩展的父类是 ListActivity,因此应该设置所有内容,但它不起作用。没有错误,只是没有注册点击。

package com.mtp;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class AvoidForeclosure extends CustomListTitle {

 // create variables
 private DbAdapter db;
 private SimpleCursorAdapter adapter;
 private ListView list;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // set to main layout
        setContentView(R.layout.main);

        // set title
        this.title.setText("Avoid Foreclosure");

        // open new DB connection
        db = new DbAdapter(this);
        db.open();

        // Get all the clients and manage the cursor
        Cursor c = db.fetchAllClients();
        startManagingCursor(c);

        String[] from = new String[] { DbAdapter.KEY_NAME };

        int[] to = new int[] { R.id.text1 };

        // Create an array adapter and set it to display using our row
        adapter = new SimpleCursorAdapter(this, R.layout.clientsrow, c, from, to);

        // Set the adapter
        setListAdapter(adapter);

        // get the list and set some data
        list = getListView();

        list.setTextFilterEnabled(true);

        list.setClickable(true);

 }

    @Override
    protected void onListItemClick(ListView list, View v, int position, long id) {
     // TODO Auto-generated method stub

    // get the selection then attempt to make a Toast
     String selection = list.getItemAtPosition(position).toString();
     Toast.makeText(this, selection, Toast.LENGTH_LONG).show();

      // Comment these out for now until I get a working Toast.
      //Intent intent = new Intent();
  //intent.setClass(v.getContext(), CurrentMarketValue.class);
  //intent.putExtra("clientId", clientData.getInt(0));
  //startActivity(intent);

     super.onListItemClick(list, v, position, id);
    }


}

这是我的主要布局:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/widget55"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:descendantFocusability="blocksDescendants"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Choose a current client:"
    android:textStyle="bold"
    android:layout_margin="3dip"
    />


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

   <TextView android:id="@android:id/empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="There are no Clients yet."
        />


</LinearLayout>

这是我的 ListView 用来填充的 clientsrow 布局:

  <?xml version="1.0" encoding="utf-8"?>
<TextView android:id="@+id/text1" xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#FF0081D3"
    android:padding="10dip"
    android:textStyle="bold"
    android:inputType="textCapWords"
    android:capitalize="words"
    android:focusable="false"
    />

有什么想法吗?谢谢你!我在最后期限前陷入困境。

最佳答案

尝试摆脱 android:descendantFocusability="blocksDescendants"(两次都出现)和 android:focusable="false",这两者都不适合您的结构已经成立。另外,我不知道您为什么要调用 list.setClickable(true);,因为这不是必需的。

关于android - onListItemClick() 不适用于 Android 中的 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3451947/

相关文章:

c# - 十六进制值 0x00,是无效字符 : still encounter problem after replace\0 with ""

c# - Linq 性能解析 XML 文件

java - 如何处理 Org.Apache.Axiom.Om.OmException : Com. Ctc.Wstx.Exc.Wstxunexpectedcharexception : Illegal Character ((Ctrl-Char, 代码 25))

android - 小部件上每个按钮的单独待定 Intent

Android 向进度条或其他内容报告媒体播放器当前位置

android - ?attr/selectableItemBackgroundBorderless 在工具栏 Android Studio 内的 ImageView 中不起作用

android - 没有字典提示的 AutoCompleteTextView

android - 更新小部件 ListView

android - 使用密码锁定应用程序

android - Google 应用内结算 : Removing ads with a subscription