我讀了很多關於這個問題在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>
顯示您的'getView(...)'代碼,您是否有clicklistener for imagebutton? – Xcihnegn 2015-04-01 15:41:23
我沒有clicklistener爲imagebutton,因爲我使用6個圖像,並將使用更多的圖像,以及clicklistener如何定義哪個imagebutton被點擊爲原因使用listview。 – Foxstrot 2015-04-01 15:50:54
類應以大寫字母開頭。使您的成員變量保密。不要使用6個圖像的列表視圖。使用ScrollView,在其內部是線性佈局,在線性佈局內部添加圖像。通過findViewById訪問onCreate中的按鈕。歡呼聲 – JacksOnF1re 2015-04-01 16:29:31