【发布时间】:2014-04-05 18:01:11
【问题描述】:
我有一个自定义适配器可以将项目放置在列表视图中。为了自定义它,我有一个 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 的类 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。我不知道用户按下了哪个切换按钮。
我如何知道用户按下了哪个切换按钮?我是否必须更改填充列表视图的方式?
非常感谢。
编辑:
这就是它现在的样子,问题是我必须检查是否单击了一行中的切换按钮的 ArrayList,当我单击另一行时,它似乎都将每个元素都设置为 true。
示例: 我有
真 |真实 |真的
我点击第三个按钮,我得到了
真 |假 |真的
但是如果我点击第一个按钮,例如我得到
假 |真实 |真的
什么时候我应该得到代替
假 |假 |真的。
这是代码适配器,所以有人可以看看。
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);
}
}
}
【问题讨论】:
标签: java android xml listview android-listview