2016-03-23 13 views
0

Kayıtlı verilerin bir Listesini görüntülüyorsam ve kullanıcı belirli bir liste öğesini tıkladığında, bir amacı tetikler ve kullanıcıyı söz konusu kullanıcıya ait profile getirir. Projemi ilk kez çalıştırdığımda, ancak işe yaramayacağından beri çalıştım. Şimdiden teşekkür ederim.setOnItemClick tanınmıyor (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); 
    } 
    } 

My 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

'dan çıkmak üzere görünüyor. Hata ayıklamayı denemelisiniz. –

+1

Neden ListView kodunu public void onClick içine kapattınız (Görünüm görünümü) {? –

+0

İlk olarak onCreate() işlevine kodlarınızı onClick() işlevinde taşıyın, onCreate yönteminde olmayan yalnızca tek bir ListView nesnesi tanımlamanız gerekir. –

cevap

1

iki şeyden biri olabilirdi. OnClickListener'u uyguladınız, ancak ListView'i bu belirli bir sınıfı dinlemek için ayarlamadınız ya da List123'ün dinleyicisini bu sınıfa ayarlayan herhangi bir yöntemi onCreate'da aradınız.

İkinci olarak, OnItemClickListener ve onCLickListener'u uygulamanız gerekmediğini düşünüyorum, çünkü ListView'de bir Öğe var.

Neden kafam karıştı? onClick neden var ve dinleyiciyi bu yöntemin içine koydun? En başta, onCreate'da ya da onCreate'da denen bir yöntemden başlayarak dinlemediğinde, görüntünün tıklandığından nasıl haberdar olacak? Mantığınız

+0

kısa uygulamasında OnItemClickListener sonra ListView'in onItemClickListener öğesini onCreate'da 'this' olarak ayarlayın. – Manny265

İlgili konular