2013-07-12 14 views
0

我遇到問題。在列表視圖按鈕上執行不同的任務點擊

 listview position 1 
     button1 
     listview position 2 
     button2 

這是我設計的listview。我的適配器是遵循

 adapter = new SimpleCursorAdapter(this, R.layout.appareal_listview_text ,cursor, new String[]{"Key_ProductName","Key_SellingPrice"} , new int[] {R.id.title_info_txt_v,R.id.description_info_txt_v}); 

現在,我給我的xml文件appareal_listview_text.xml現在

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:clickable="true" 
    android:background="@drawable/layout1" 
    android:orientation="vertical" > 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/title_info_txt_v" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:layout_marginLeft="3dip" 
     android:layout_marginRight="3dip" 
     android:textColor="#000000" 
     android:textSize="15dp" 
     android:textStyle="bold" /> 

    <TextView 
     android:id="@+id/description_info_txt_v" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="4dip" 
     android:layout_marginLeft="3dip" 
     android:layout_marginRight="3dip" 
     android:layout_marginTop="4dip" 
     android:textColor="#000000" 
     android:textSize="12dp" 
     android:textStyle="bold" /> 

    <Button 
     android:id="@+id/btnBuyNow_id" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="12dp" 
     android:layout_marginTop="15dp" 
     android:background="@drawable/buynow" 
     android:clickable="true" 
     android:onClick="buynowClick" /> 
</LinearLayout> 

我porblem是定義我的XML按鈕,這是適配器和按鈕呼叫是隻定義我只是一次在XML中,我想執行不同的任務,每次點擊每個位置的listview.For我用

@Override 
    public void onListItemClick(ListView parent, View view, int position, 
      long id) { 
     Intent intent = new Intent(this, asasdad.class); 
     Cursor cursor = (Cursor) adapter.getItem(position); 
     intent.putExtra("EMPLOYEE_ID", cursor.getInt(cursor.getColumnIndex("_id"))); 
     startActivity(intent); 
     System.out.println("list is click"); 
    } 

和以及我不能使用此

 listView.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View view,int position, long id) { 

      // When clicked, show a toast with the TextView text 

     } 
}); 

那麼我怎麼會認識到它列表按鈕位置點擊執行不同的任務。

+0

問題解決還是仍然面臨? –

+0

@Chintan ..感謝好奇心在我的問題,,,我仍然是同樣的問題 – DJhon

+1

你應該去選擇項目時從視圖搜索兒童。 –

回答

0

您應該使用以下代碼。

button.setTag("position"); 

當按鈕點擊後,

String strPos = view.getTag(); 

在這裏,您將獲得按鈕的位置。

+0

好吧我試過這個,但我將如何實現這個甚至按鈕,然後單擊是定義XML。 – DJhon

+0

@BlueGreen,你不能因爲你需要在你的Adapter適配器中實現列表視圖。您需要創建一個「單獨的適配器類」,其中「getView」方法將被覆蓋。在你的代碼中,你可以查看位置以及查看它被點擊的位置。如果我弄錯了,請原諒。 –

相關問題