0

我讀了很多關於這個問題在stackoverflow的文章,但我沒有解決我的問題。我有ListView有行,每行有3個圖像按鈕,當我點擊「setOnItemClickListener」中的圖像按鈕代碼不起作用。如何從包含可點擊項目(imagebuttons)的ListView中獲取行項目的標識(每行有3個項目)?

我需要點擊行中的哪個項目(imagebutton)。在我的ListView中會有很多行。

我聽說關於simpleCursorAdapter,fragmentlist,cursorloader,但我最小需要的SDK是10

在這段代碼中,我使用了6個圖像,但是我將使用更多的圖像(100)。

我的Java代碼:

package foxstrot.ghp; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ListView; 
import android.widget.SimpleAdapter; 
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.Map; 

public class images extends Activity { 

    int[] images = {R.raw.a2, R.raw.a3, R.raw.im2, R.raw.a4, R.raw.a5, R.raw.im6,}; 

    ListView lv; 

    Intent i = new Intent(this, king.class); 

    final String LOG_TAG = "L"; 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.images); 



     lv = (ListView) findViewById(R.id.lv); 

     ArrayList<Map <String, Object>> data = new ArrayList<Map<String, Object>>(images.length); 
     Map<String, Object> m; 

     for(int i = 0; i < images.length; i++){ 


      m = new HashMap<String, Object>(); 
      m.put("image1", images[i]); 
      m.put("image2", images[i+1]); 
      m.put("image3", images[i+2]); 
      i = i + 2; 
      data.add(m); 

     } 

     String[] from = {"image1","image2","image3"}; 
     int[] to = {R.id.ib1, R.id.ib2, R.id.ib3}; 

     SimpleAdapter sAdapter = new SimpleAdapter(this, data, R.layout.a_, from, to); 

     lv.setAdapter(sAdapter); 


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

       i.putExtra("id", id); 
       startActivity(i); 
       Log.d(LOG_TAG, "itemClick: position = " + position + ", id = " 
         + id); 
      } 
     }); 






    } 












} 

我的XML代碼(CONTAINER其中用適配器)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="125dp" 
    > 



     <ImageButton 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:id="@+id/ib1" 
      android:layout_weight="1" 
      android:scaleType="fitXY" 

      /> 

     <ImageButton 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:id="@+id/ib2" 
      android:layout_weight="1" 
      android:scaleType="fitXY" 
      /> 

     <ImageButton 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:id="@+id/ib3" 
      android:layout_weight="1" 
      android:scaleType="fitXY" 
      /> 



</LinearLayout> 
+0

顯示您的'getView(...)'代碼,您是否有clicklistener for imagebutton? – Xcihnegn 2015-04-01 15:41:23

+0

我沒有clicklistener爲imagebutton,因爲我使用6個圖像,並將使用更多的圖像,以及clicklistener如何定義哪個imagebutton被點擊爲原因使用listview。 – Foxstrot 2015-04-01 15:50:54

+0

類應以大寫字母開頭。使您的成員變量保密。不要使用6個圖像的列表視圖。使用ScrollView,在其內部是線性佈局,在線性佈局內部添加圖像。通過findViewById訪問onCreate中的按鈕。歡呼聲 – JacksOnF1re 2015-04-01 16:29:31

回答

0

setOnItemClickListener()用於檢測在列表項(行),而不是你可能對這些項目(行)的特定按鈕的點擊。

對於xml上的每一行按鈕,您將需要添加一個onClick屬性和函數來處理點擊:android:onClick="myFunction"

將你的活動,你將不得不創建一個函數:

public void myFunction(View v) {...}

如果您需要通過具體的信息關於哪一行被點擊,你可以在你的適配器的getView()按鈕的視圖中添加一個標籤方法。有關此檢查的更多信息,請點擊此處:How do I get the row ID of the row from a ListView, which contains a clickable item?

+0

我需要點擊行中的哪個項目(圖像按鈕)。在我的ListView中會有很多行。 – Foxstrot 2015-04-01 16:00:39

+0

我不明白你的問題,你在每個圖像按鈕上放上一個onClick函數(如果你需要3個不同的函數),並且如果你需要跟蹤每個按鈕所屬的listview行,請查看我發佈的關於如何使用settag ) 爲了達成這個。 – 2015-04-02 08:12:11

+0

Evripidis Drakos非常感謝,myAdapter中的settag()解決了問題。 – Foxstrot 2015-04-02 15:52:00

0

在你images.xml佈局文件,請將您的ImageButtonsclickablefocusable屬性到false。按鈕上的水龍頭現在會通過並由ListView處理。

<ImageButton 
    android:id="@+id/ib1" 
    ... 
    android:clickable="false" 
    android:focusable="false" /> 
+0

感謝您的回答,但這對我來說不起作用。 – Foxstrot 2015-04-01 15:57:43

+0

這沒有效果,因爲你問我點擊我的圖像按鈕時,「setOnItemClickListener」中的代碼在以前不工作。現在,你改變了你的問題! – 2015-04-02 02:42:06

相關問題