我有一个自定义列表视图,显示图像,文本视图和单选按钮。我需要一次只选择一个项目(即RadioButton)。我在这个列表视图中添加了一个textwatcher,一切正常。
问题出在这里
是否需要在过滤前保存单选按钮的状态?
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if ( row == null) {
// Log.d(tag,"Starting XML Row Inflation");
LayoutInflater inflater = (LayoutInflater) this.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.custom_row_view_user, parent, false);
Log.d(tag,"Successfully completed XML Row Inflation for positon"+position);
}
// Get item
final Contact contact = getItem(position);
contactIcon = (ImageView) row.findViewById(R.id.image_icon);
name = (TextView) row.findViewById(R.id.textviewname);
name.setText(contact.name);
rb = (RadioButton) row.findViewById(R.id.radiobutton1);
rb.setEnabled(true);
String imgFilePath = DIR + contact.imageId;
try {
Bitmap bitmap = BitmapFactory.decodeStream(this.context.getResources().getAssets().open(imgFilePath));
contactIcon.setImageBitmap(bitmap);
} catch(IOException e) {
e.printStackTrace();
}
return row;
}
// Filter function
@Override
public Filter getFilter() {
if (filter == null) {
filter = new ContactFilter();
}
return filter;
}
private class ContactFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence prefix) {
FilterResults results = new FilterResults();
if( prefix == null || prefix.length() == 0) {
synchronized (this) {
results.values = originalContacts;
results.count = originalContacts.size();
}
} else {
synchronized (this) {
String prefixString = prefix.toString().toLowerCase();
final ArrayList<Contact> filteredItems = new ArrayList<Contact>();
final ArrayList<Contact> localItems = new ArrayList<Contact>();
localItems.addAll(originalContacts);
final int count = localItems.size();
for ( int i=0; i<count; i++) {
final Contact contact = localItems.get(i);
final String contactName = contact.name.toString().toLowerCase();
if ( contactName.startsWith(prefixString) ) {
filteredItems.add(contact);
} else {
}
}
results.values = filteredItems;
results.count = filteredItems.size();
} // end of synchronized.
}
return results;
}
@Override
protected void publishResults(CharSequence prefix,
FilterResults results) {
synchronized (this) {
@SuppressWarnings("unchecked")
final ArrayList<Contact> localItems = (ArrayList<Contact>)results.values;
notifyDataSetChanged();
clear();
for( Iterator<Contact> iterator = localItems.iterator(); iterator.hasNext();) {
Contact index = (Contact) iterator.next();
add(index);
}
} // end of syncronized
}
}
联系人类型
public class Contact {
public String name;
public String imageId;
public String type;
public boolean isSelected;
public boolean useDefaultKey;
public boolean flag;
public Contact() {
}
public Contact(String name, String type, String resouceFilePath) {
this.name = name;
this.type = type;
this.imageId = resouceFilePath;
this.isSelected = false;
this.useDefaultKey = true;
this.flag = false;
}
@Override
public String toString() {
return this.name;
}
public boolean getCheckeBoxStatus() {
return isSelected;
}
}
我实现了LinearLayout可检查方法如下
public class CheckableLinearLayout extends LinearLayout implements Checkable{
private RadioButton rb;
public CheckableLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
// find checked text view
int childCount = getChildCount();
for (int i = 0; i < childCount; ++i) {
View v = getChildAt(i);
if (v instanceof RadioButton) {
rb = (RadioButton)v;
}
}
}
@Override
public boolean isChecked() {
return rb != null ? rb.isChecked() : false;
}
@Override
public void setChecked(boolean checked) {
if (rb != null) {
// Toast.makeText(getContext(), "Clicked radio buttton", Toast.LENGTH_LONG).show();
rb.setChecked(checked);
}
}
@Override
public void toggle() {
if (rb != null) {
rb.toggle();
rb.setChecked(false);
}
}}
非常感谢任何帮助..
答案 0 :(得分:1)
您必须尝试使用适配器的getView()方法的position参数来检查radiobutton。而是尝试根据一些Id来设置radiobutton。
编辑:
通过代码研究,我发现您已经实现了这个示例:
http://tokudu.com/2010/android-checkable-linear-layout/
并且正如我之前所假设的那样,单选按钮检查状态取决于listview中行的位置,但不依赖于listview中的实际项目,其位置应在过滤器上更改。 我在您的代码中做了几处更改,现在listview项目检查不是基于项目的位置,而是基于项目本身(通过匹配联系人的姓名)。
代码的变化如下:
<强> HelloListViewActivity:强>
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> av, View view, int position,
long id) {
o = av.getItemAtPosition(position);
String name = ((Contact) o).name;
selectedName = name;
ContactAdapter:
static boolean isContactChecked = false;
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
final Contact contact = getItem(position);
if(contact.name.equalsIgnoreCase(HelloListViewActivity.selectedName))
isContactChecked = true;
else
isContactChecked= false;
CheckableLinearLayout:
@Override
public void setChecked(boolean checked) {
if (rb != null) {
// rb.setChecked(checked);
rb.setChecked(ContactAdapter.isContactChecked);
}
}