0
안녕하세요, 내 listview 내 사용자 정의 arrayadapter 사용하고 있습니다. vongella의 코드를 사용하고 있습니다.
여러 번 호출 listview
public class CategoriesRowDetails {
private String name;
private String resName;
private boolean selected;
public CategoriesRowDetails(String name, boolean isSelected){
setName(name);
setResName(name);
setSelected(isSelected);
}
private void setName(String name) {
this.name = name;
}
public String getName(){
return name;
}
private synchronized void setResName(String name) {
name = name.toLowerCase();
name = name.replace(" ", "_");
this.resName = name;
}
public synchronized String getResName(){
return resName;
}
public boolean isSelected(){
return selected;
}
public void setSelected(boolean selected){
this.selected = selected;
}
}
문제는 그 (5)의 목록을 전달한다는 것이다 :
categoriesRowDetails 다음이다
public class CategoriesAdapter extends ArrayAdapter<CategoriesRowDetails>{
private final Activity context;
private final List<CategoriesRowDetails> categories;
public CategoriesAdapter(Activity context,
List<CategoriesRowDetails> categories) {
super(context, R.layout.categories_row, R.id.textViewCategory, categories);
this.context = context;
this.categories = categories;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if(rowView == null){
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.categories_row, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.imageView = (ImageView) rowView.findViewById(R.id.imageViewCategory);
viewHolder.textView = (TextView) rowView.findViewById(R.id.textViewCategory);
viewHolder.checkBox = (CheckBox) rowView.findViewById(R.id.checkBoxCategory);
viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
CategoriesRowDetails element = (CategoriesRowDetails) viewHolder.checkBox.getTag();
element.setSelected(buttonView.isChecked());
}
});
rowView.setTag(viewHolder);
viewHolder.checkBox.setTag(categories.get(position));
}else{
((ViewHolder) rowView.getTag()).checkBox.setTag(categories.get(position));
}
ViewHolder holder = (ViewHolder) rowView.getTag();
holder.textView.setText(categories.get(position).getName());
int resResult = context.getResources().getIdentifier(categories.get(position).getResName(), "drawable",
".my_tour_guide");
if(resResult == 0){
holder.imageView.setImageResource(R.drawable.stop);
}else{
holder.imageView.setImageResource(resResult);
}
Log.i("CategoriesAdapter", String.valueOf(categories.get(position).isSelected()) + "position = " + String.valueOf(position));
holder.checkBox.setSelected(categories.get(position).isSelected());
return rowView;
}
private static class ViewHolder{
public ImageView imageView;
public TextView textView;
public CheckBox checkBox;
}
} 어댑터에 대한 코드는 다음과 같다 항목은 모두 isSelected = true로 설정되고 목록보기가 표시 될 때 항목이 선택되지 않고 logcat에 다음 메시지가 표시됩니다. getview는 모든 항목에 대해 두 번 실행됩니다.
02-25 20:43:51.160: I/CategoriesAdapter(14185): trueposition = 0
02-25 20:43:51.190: I/CategoriesAdapter(14185): trueposition = 1
02-25 20:43:51.200: I/CategoriesAdapter(14185): trueposition = 2
02-25 20:43:51.210: I/CategoriesAdapter(14185): trueposition = 3
02-25 20:43:51.220: I/CategoriesAdapter(14185): trueposition = 4
02-25 20:43:51.660: I/CategoriesAdapter(14185): trueposition = 0
02-25 20:43:51.660: I/CategoriesAdapter(14185): trueposition = 1
02-25 20:43:51.670: I/CategoriesAdapter(14185): trueposition = 2
02-25 20:43:51.670: I/CategoriesAdapter(14185): trueposition = 3
02-25 20:43:51.670: I/CategoriesAdapter(14185): trueposition = 4
편집 레이아웃을 추가
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageViewCategory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:paddingLeft="10dp" />
<TextView
android:id="@+id/textViewCategory"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/checkBoxCategory"
android:layout_toRightOf="@+id/imageViewCategory"
android:lines="1"
android:paddingLeft="5dp"
android:scrollHorizontally="true"
android:textSize="24sp" />
<CheckBox
android:id="@+id/checkBoxCategory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:paddingLeft="5dp"
android:paddingRight="5dp" />
레이아웃 모양은 어떻습니까? 'getView'가 두 번 실행되는 것은'fill_parent'로 설정되지 않은 경우 너비와 높이를 여러 번 계산했기 때문일 수 있습니다. –
나는 방금 내 질문을 편집했다. 이건 내 레이아웃이야. – user2065529
나는 무엇이 잘못 될지 모르지만, 많은 항목을 추가하고 스크롤 한 후 사라지는 처음 두 가지를 체크 할 때, 나는 체크 박스가 바뀌었을 때 스크롤을합니다! checkboxsettag()를 사용하여 문제가 발생했습니다. – user2065529