我有一个自定义适配器,用于将项目放在listview中。为了自定义它,我有一个listitem.xml文件,我在其中设置组成listitem的元素。
他们如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="50dp" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:scaleType="fitXY" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView1"
android:layout_marginLeft="23dp"
android:layout_marginTop="5dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="" />
<ToggleButton
android:id="@+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_marginRight="5dp"
android:text=""/>
</RelativeLayout>
我有一个使用以下布局settings.xml的classe Settings.java:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayoutSettings"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#666666"
android:orientation="vertical" >
<ListView
android:id="@+id/listViewSettings"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</RelativeLayout>
Class Settings.java是这样的:
public class Settings extends Activity {
private SensorAdapter sensorAdapter;
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.settings);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.title_bar);
context = getApplicationContext();
sensorAdapter = new SensorAdapter();
ListView sensorListview = (ListView) findViewById(R.id.listViewSettings);
sensorListview.setAdapter(sensorAdapter);
sensorListview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
sensorItem sensor = sensorAdapter.getCodeLearnChapter(arg2);
}
});
}
public class sensorItem {
String sensorName;
String sensorDescription;
Drawable sensorImage;
}
public List<sensorItem> getDataForListView() {
List<sensorItem> sensorsList = new ArrayList<sensorItem>();
sensorItem sensorGps = new sensorItem();
sensorGps.sensorName = "Location";
sensorGps.sensorDescription = "Collect location";
sensorGps.sensorImage = getResources().getDrawable(R.drawable.location);
sensorItem sensorPhoto = new sensorItem();
sensorPhoto.sensorName = "Photos";
sensorPhoto.sensorDescription = "Collect photos";
sensorPhoto.sensorImage = getResources().getDrawable(R.drawable.photos);
sensorItem sensorAmplitude = new sensorItem();
sensorAmplitude.sensorName = "Sound";
sensorAmplitude.sensorDescription = "Collect amplitude";
sensorAmplitude.sensorImage = getResources().getDrawable(
R.drawable.amplitude);
sensorItem sensorOrientation = new sensorItem();
sensorOrientation.sensorName = "Orientation";
sensorOrientation.sensorDescription = "Collect orientation";
sensorOrientation.sensorImage = getResources().getDrawable(
R.drawable.compass);
sensorItem sensorSms = new sensorItem();
sensorSms.sensorName = "Messages";
sensorSms.sensorDescription = "Collect messages";
sensorSms.sensorImage = getResources().getDrawable(R.drawable.sms);
sensorItem sensorBattery = new sensorItem();
sensorBattery.sensorName = "Battery";
sensorBattery.sensorDescription = "Collect battery";
sensorBattery.sensorImage = getResources().getDrawable(
R.drawable.battery);
sensorItem sensorCalendar = new sensorItem();
sensorCalendar.sensorName = "Calendar";
sensorCalendar.sensorDescription = "Collect calendar";
sensorCalendar.sensorImage = getResources().getDrawable(
R.drawable.calendar);
sensorItem sensorAccelerometer = new sensorItem();
sensorAccelerometer.sensorName = "Accelerometer";
sensorAccelerometer.sensorDescription = "Collect accelerometer";
sensorAccelerometer.sensorImage = getResources().getDrawable(
R.drawable.accelerometer);
sensorItem sensorLight = new sensorItem();
sensorLight.sensorName = "Light";
sensorLight.sensorDescription = "Collect luminosity";
sensorLight.sensorImage = getResources().getDrawable(R.drawable.light);
sensorItem sensorContacts = new sensorItem();
sensorContacts.sensorName = "Contacts";
sensorContacts.sensorDescription = "Collect contact";
sensorContacts.sensorImage = getResources().getDrawable(
R.drawable.contacts);
sensorsList.add(sensorGps);
sensorsList.add(sensorPhoto);
sensorsList.add(sensorAmplitude);
sensorsList.add(sensorOrientation);
sensorsList.add(sensorSms);
sensorsList.add(sensorBattery);
sensorsList.add(sensorCalendar);
sensorsList.add(sensorAccelerometer);
sensorsList.add(sensorLight);
sensorsList.add(sensorContacts);
return sensorsList;
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public class SensorAdapter extends BaseAdapter {
List<sensorItem> sensorList = getDataForListView();
private ToggleButton locationToggle, photosToggle, soundToggle,
orientationToggle, messagesToggle, batteryToggle, calendarToggle,
accelerometerToggle, lightToggle, contactsToggle;
@Override
public int getCount() {
return sensorList.size();
}
@Override
public sensorItem getItem(int arg0) {
return sensorList.get(arg0);
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
if (arg1 == null) {
LayoutInflater inflater = (LayoutInflater) Settings.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
arg1 = inflater.inflate(R.layout.listitem, arg2, false);
}
TextView sensorDescription = (TextView) arg1
.findViewById(R.id.textView2);
ImageView sensorImage = (ImageView) arg1
.findViewById(R.id.imageView1);
locationToggle = (ToggleButton) arg1.findViewById(R.id.toggleButton1);
locationToggle.setChecked(true);
locationToggle.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "CARREGOU", Toast.LENGTH_LONG).show();
}
});
sensorItem sensor = sensorList.get(arg0);
sensorDescription.setText(sensor.sensorDescription);
sensorImage.setBackground(sensor.sensorImage);
return arg1;
}
public sensorItem getCodeLearnChapter(int position) {
return sensorList.get(position);
}
}
}
这个问题是现在listitem.xml中的每个元素都具有完全相同的ID。因此每个切换按钮都具有相同的ID。我不知道用户按下了哪个togglebutton。
我怎么知道用户按下哪个togglebutton?我是否必须改变填充列表视图的方式?
非常感谢。编辑:
这就是它现在的样子,问题是我必须检查一行中的toggleButton是否被点击的ArrayList,当我点击另一行时似乎都将每个元素都设置为true。
实施例: 我有
是的是的|真的
我点击第三个按钮,然后我
是的假|真
但如果我点击第一个按钮,例如我得到
false |是的|真
当我应该得到
false |假|真。
这是代码适配器,所以有人可以看看。
public class SensorAdapter extends BaseAdapter {
List<sensorItem> sensorList = getDataForListView();
private ToggleButton settingsToggle;
private ArrayList<Boolean> checkedSensors;
@Override
public int getCount() {
return sensorList.size();
}
@Override
public sensorItem getItem(int arg0) {
return sensorList.get(arg0);
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
if (arg1 == null) {
LayoutInflater inflater = (LayoutInflater) Settings.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
arg1 = inflater.inflate(R.layout.listitem, arg2, false);
}
// TextView sensorName = (TextView)
// arg1.findViewById(R.id.textView1);
TextView sensorDescription = (TextView) arg1
.findViewById(R.id.textView2);
ImageView sensorImage = (ImageView) arg1
.findViewById(R.id.imageView1);
settingsToggle = (ToggleButton) arg1.findViewById(R.id.toggleButton1);
settingsToggle.setChecked(true);
checkedSensors = new ArrayList<Boolean>();
int i = 0;
while(i < 10){
checkedSensors.add(i,true);
i++;
}
settingsToggle.setTag(new Long(getItemId(arg0)));
settingsToggle.setOnClickListener(mOnToggleListener);
/*settingsToggle.setOnCheckedChangeListener(new OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
if(isChecked){
Toast.makeText(Settings.this, "on "+buttonView.getId(), Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(Settings.this, "off "+buttonView.getId(), Toast.LENGTH_LONG).show();
}
}
});*/
sensorItem sensor = sensorList.get(arg0);
// sensorName.setText(sensor.sensorName);
sensorDescription.setText(sensor.sensorDescription);
sensorImage.setBackground(sensor.sensorImage);
return arg1;
}
public OnClickListener mOnToggleListener = new OnClickListener(){
@Override
public void onClick(View v){
long id = (Long) v.getTag();
if (checkedSensors.get((int)id)){
//enableSensor(id);
checkedSensors.add((int)id, false);
}else{
//disableSensor(id);
checkedSensors.add((int)id, true);
}
for(int i = 0; i<10 ; i++){
System.out.println(i + " " + checkedSensors.get(i));
}
}
};
public sensorItem getSensor(int position) {
return sensorList.get(position);
}
}
}
答案 0 :(得分:1)
您可以在切换按钮的标签中设置索引,以便onClick事件知道点击了哪个索引。
在设置onClick侦听器之前,请添加以下行:
locationToggle.setTag(arg0);
在你的onClick监听器中:
int position = (Integer)v.getTag();
答案 1 :(得分:0)
您可以像这样创建getView的侦听器:
public OnClickListener mOnToggleListener = new OnClickListener() {
@Override
public void onClick(View v) {
//get id of your item from view
//v is locationToggleView
long id = (Long)v.getTag();
}
};
并在getView方法中进行这些更改:
//set id as view tag when create or reuse view
//arg0 is position
locationToggle.setTag(new Long(getItemId(arg0)));
locationToggle.setOnClickListener(mOnToggleListener);
通过这种方式,您可以在单击locationToggle时获取与视图相关的项目ID。