如何根据每个用户ID通过onclick监听器显示列表视图

时间:2017-05-13 18:53:47

标签: java android sqlite

我希望在点击列表视图上实现的每个用户ID后显示列表视图。一旦我点击了用户ID,应用就会立即停止。任何人都有任何想法来解决问题

这是我的第一个列表视图java

public void search() {
    String str1 = editText.getText().toString();
    // Find ListView to populate
    listView = (ListView) findViewById(R.id.list_view1);
    //LoginDataBaseAdapter loginDataBaseAdapter = new LoginDataBaseAdapter(getApplicationContext());
    List<String> labels = loginDataBaseAdapter.searchData(str1);
    ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(this,R.layout.activity_list,R.id.tvsid, labels);
    listView.setAdapter(dataAdapter1);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                long arg3) {
            // TODO Auto-generated method stub
            Bundle passdata = new Bundle();
            Cursor listCursor = (Cursor) arg0.getItemAtPosition(arg2);
            String studId = listCursor.getString(listCursor
                    .getColumnIndex("ATTENDANCE.STUDENT_ID"));
            // Toast.makeText(getApplicationContext(),
            // Integer.toString(nameId), 500).show();
            passdata.putString("keyid", studId);
            Intent passIntent = new Intent(LecturerSearch.this,
                    LecturerResult.class);
            passIntent.putExtras(passdata);
            startActivity(passIntent);
        }
    });

} 

这是第二个列表视图java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_lecturer_result);
    listView = (ListView)findViewById(R.id.list_view);
    listDataAdapter = new ListDataAdapter(getApplicationContext(),R.layout.attendance_row);
    listView.setAdapter(listDataAdapter);
    Bundle showData = getIntent().getExtras();
    rowId = showData.getString("keyid");
    loginDataBaseAdapter = new LoginDataBaseAdapter(getApplicationContext());
    cursor = loginDataBaseAdapter.getInformation(db, rowId);
    if(cursor.moveToFirst())
    {
        do{

            String location, date;
            location=cursor.getString(0);
            date=cursor.getString(1);
            DataAttendance dataAttendance =new DataAttendance(location,date);
            listDataAdapter.add(dataAttendance);

        } while(cursor.moveToNext());
    }


}

这是第一个列表视图中的函数

 public ArrayList<String> searchData(String subject) {

    ArrayList<String> view = new ArrayList<String>();
    //String [] columns = {"ATTENDANCE.STUDENT_ID"};
    String selectQuery = "SELECT ATTENDANCE.STUDENT_ID FROM ATTENDANCE WHERE SUBJECT=\""+ subject+ "\" GROUP BY ATTENDANCE.STUDENT_ID" ;

    try {
        //Cursor mCursor = db.query("ATTENDANCE",columns,"SUBJECT ='"+subject+"'",null,null,null,null);
        Cursor mCursor = db.rawQuery(selectQuery,null);

        if (mCursor != null) {
            mCursor.moveToFirst();
            for (int i = 0; i < mCursor.getCount(); i++) {
                view.add(mCursor.getString(0));
                mCursor.moveToNext();
            }
            mCursor.close();
            return view;
        }
        return view;
    } catch (Exception e) {
        e.printStackTrace();
    }

    return view;
}



this is the function of second listview

 public Cursor getInformation(SQLiteDatabase db, String id){
    Cursor cursor;
    String[] projections = {"LOCATION","DATE"};
    db = dbHelper.getReadableDatabase();
    cursor = db.query("ATTENDANCE",projections,"ATTENDANCE.STUDENT_ID ='"+id+"'",null,null,null,null,null);
    /*cursor = db.query("SELECT ATTENDANCE.LOCATION,ATTENDANCE.DATE," +
            "ATTENDANCE.SUBJECT,STUDENT.ID FROM STUDENT JOIN ATTENDANCE ON STUDENT.ID=ATTENDANCE.STUDENT_ID",null,null,null,null,null,null,null);*/

    return cursor;
}

这是logcat

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.azharwa.studentattendance, PID: 2209
              java.lang.ClassCastException: java.lang.String cannot be cast to android.database.Cursor
                  at com.example.azharwa.studentattendance.LecturerSearch$2.onItemClick(LecturerSearch.java:69)
                  at android.widget.AdapterView.performItemClick(AdapterView.java:300)
                  at android.widget.AbsListView.performItemClick(AbsListView.java:1143)
                  at android.widget.AbsListView$PerformClick.run(AbsListView.java:3044)
                  at android.widget.AbsListView.onTouchUp(AbsListView.java:3845)
                  at android.widget.AbsListView.onTouchEvent(AbsListView.java:3617)
                  at android.view.View.dispatchTouchEvent(View.java:8388)
                  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2424)
                  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2158)
                  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
                  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
                  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
                  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
                  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
                  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
                  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
                  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
                  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
                  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
                  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
                  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2172)
                  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2314)
                  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1692)
                  at android.app.Activity.dispatchTouchEvent(Activity.java:2739)
                  at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:71)
                  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2275)
                  at android.view.View.dispatchPointerEvent(View.java:8578)
                  at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4021)
                  at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3887)
                  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449)
                  at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502)
                  at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3468)
                  at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3578)
                  at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3476)
                  at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3635)
                  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449)
                  at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502)
                  at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3468)
                  at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3476)
                  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449)
                  at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5701)
                  at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5675)
                  at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5646)
                  at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5791)
                  at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
                  at android.os.MessageQueue.nativePollOnce(Native Method)
                  at android.os.MessageQueue.next(MessageQueue.java:143)
                  at android.os.Looper.loop(Looper.java:122)
                  at android.app.ActivityThread.main(ActivityThread.java:5221)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:372)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                at com.android.internal.os.ZygoteInit.main(Zy

1 个答案:

答案 0 :(得分:0)

  

java.lang.ClassCastException:java.lang.String无法转换为   android.database.Cursor

您将获得ClassCastException。要解决此错误,请更新onItemClick()方法,如下所示:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        // Student Id 
        String studId = labels.get(position);

        Intent passIntent = new Intent(LecturerSearch.this, LecturerResult.class);
        passIntent.putExtra("keyid", studId);
        startActivity(passIntent);
    }
});

仅供参考,,因为您要将学生ids存储到labels数组,并将其传递给adapter以填充listview。因此,您可以从clicked方法获取position项目onItemClick()并轻松使用此position,您可以从id获取学生labels } array。

希望这会有所帮助〜