setOnItemClick未被识别(Android)

时间:2016-03-23 23:25:39

标签: android listview onitemclick

我正在显示记录数据的Listview,当用户点击特定列表项时,它会触发意图并将用户带到该选定用户的配置文件。当我第一次运行我的项目时,我工作,但是因为它不起作用。提前谢谢。

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>

1 个答案:

答案 0 :(得分:1)

可能是两件事之一。 您已经实现了OnClickListener,但是您没有将ListView设置为侦听该特定类,也没有在onCreate中调用任何将ListView的侦听器设置为该类的方法。
/>其次我认为你可能需要实现OnItemClickListener而不是onCLickListener,因为这就是ListView上的一个项目。

我很困惑,为什么你有{{{ 1}}然后你在这个方法中设置监听器?如果从一开始就没有收听,即在onClickonCreate中调用的方法,那么它如何首先知道该视图已被点击?你的逻辑似乎没有了