【问题标题】:setOnItemClick isnt getting recognized(Android)设置项目点击未被识别(Android)
【发布时间】:2016-03-23 23:25:38
【问题描述】:

我正在显示记录数据的列表视图,当用户单击特定列表项时,它会触发意图并将用户带到该选定用户的配置文件。我第一次运行我的项目时工作过,但从那以后它就无法工作了。提前谢谢你。

public class CowListView extends ListActivity {

    RegisterCowAdapter cowAdapter;
    private DataBaseHelper databaseHelper;
    public ListView listView;
    TextView student_Id;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cow_list_item);
        cowAdapter = new RegisterCowAdapter(this);
        cowAdapter.open();
        updateCowUI();
        listView = (ListView) findViewById(android.R.id.list);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                student_Id = (TextView) view.findViewById(R.id.cowtagnolist);
                String studentId = student_Id.getText().toString();
                Intent objIndent = new Intent(getApplicationContext(), CowProfile.class);
                Toast.makeText(CowListView.this, "Clicked", Toast.LENGTH_SHORT).show();
                objIndent.putExtra("student_Id", Integer.parseInt(studentId));
                startActivity(objIndent);
            }
        });

    }


    private void updateCowUI() {

        Cursor cursor = cowAdapter.getAllCowData();
        startManagingCursor(cursor);
        String[] from = new String[]{RegisterCowAdapter.COWTAGNO, RegisterCowAdapter.COWDOB};
        int[] to = new int[]{R.id.cowtagnolist, R.id.cowdoblist};
        SimpleCursorAdapter reminders = new SimpleCursorAdapter(
                this,
                R.layout.cow_list_item,
                cursor,
                from,
                to
        );
        this.setListAdapter(reminders);
    }
}


    private void updateCowUI() {

         Cursor cursor =cowAdapter.getAllCowData();
        startManagingCursor(cursor);
        String[] from = new String[]{RegisterCowAdapter.COWTAGNO, RegisterCowAdapter.COWDOB};
        int[] to = new int[]{R.id.cowtagnolist, R.id.cowdoblist};
        SimpleCursorAdapter reminders  = new SimpleCursorAdapter(
                this,
                R.layout.cow_list_item,
                cursor,
                from,
                to
        );
        this.setListAdapter(reminders);
    }
   }

我的 XML

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:longClickable="true">
    <ListView
        android:id="@android:id/list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"


        >
    </ListView>
    <TextView
        android:id="@+id/cowtagnolist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@color/black"
        android:focusableInTouchMode="false"
        android:clickable="false"
        android:focusable="false"


        android:descendantFocusability="blocksDescendants"
        >
    </TextView>

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

        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="17dp"
        android:focusableInTouchMode="false"
        android:clickable="false"
        android:focusable="false"
        android:descendantFocusability="blocksDescendants"
        android:textColor="@color/black">

    </TextView>
    </LinearLayout>

【问题讨论】:

  • 你应该尝试调试。
  • 为什么在 public void onClick(View view) { 中包含 ListView 代码?
  • 首先将 onClick() 函数中的代码移动到 onCreate() 中,您还必须只定义一个不在 onCreate 方法中的 ListView 对象。

标签: android listview onitemclick


【解决方案1】:

可能是两件事之一。 您已经实现了OnClickListener,但您还没有设置 ListView 来监听那个特定的类,也没有在 onCreate 中调用任何方法来设置 ListView 的监听器到那个类。

其次,我认为你可能需要实现OnItemClickListener 而不是onCLickListener,因为这就是ListView 上的内容,一个Item。

我很困惑为什么你有onClick 然后你在这个方法中设置监听器?如果视图没有从一开始即在 onCreate 中或在 onCreate 中调用的方法中进行侦听,它如何首先知道该视图已被单击?你的逻辑似乎有点不对劲

【讨论】:

  • 简而言之,实现 OnItemClickListener 然后在 onCreate 中将 ListView 的 onItemClickListener 设置为 'this'。
猜你喜欢
  • 2021-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多