2016-03-23 68 views
0

我正在显示记录数据的列表视图,当用户单击特定列表项时,它会触发一个意图并将用户带到该选定用户的配置文件。我工作时,我第一次运行我的项目,但因为它不会工作。先谢谢你。setOnItemClick未被识别(Android)

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> 
+0

你应该尝试调试。 –

+1

为什么你在公共无效onClick(视图视图){? –

+0

首先将onClick()函数中的代码移动到onCreate(),您也必须仅定义一个不在onCreate方法中的ListView对象。 –

回答

1

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

其次,我认为你可能需要执行OnItemClickListener而不是onCLickListener,因为那是什么在ListView,一个项目。

我感到困惑,但为什么你有onClick然后你在这个方法里面设置监听器?首先它将如何知道,如果视图没有从开始处进行侦听,即在onCreate中或者在onCreate中调用的方法中,该视图已被点击?你的逻辑似乎退步

+0

简言之,实现OnItemClickListener,然后在onCreate中将ListView的onItemClickListener设置为'this'。 – Manny265