在自定义列表视图筛选后单选状态选择更新 [英] Radiobutton state updation after filtering in custom listview

查看:237
本文介绍了在自定义列表视图筛选后单选状态选择更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有显示图像,以及TextView的单选按钮,自定义列表视图。我需要一次只选择一个项目(即单选按钮)。我连着一个textwatcher这个列表视图,一切工作正常。

I have a custom listview which display an image, textview and radio button. I need to select only one item (i.e RadioButton)at a time. I attached a textwatcher to this listview and everything working fine.

问题放在这里


  1. 让我们假设,在列表视图中,我选择了一个单选按钮。

  2. 我在执行盒子的EditText表明,它过滤列表视图项搜索选项
    过滤列表项/项目将在第一个位置显示。

  3. 在这里,在第一当前位置的filterd项目也越来越检查(因为它是前选择)

是不是需要保存单选按钮的状态过滤过吗?

Is it require to save the state of radio button before filtering ?

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可检查的方法如下:

And i implemented LinearLayout checkable method as follows

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);
         }

}}

任何帮助是极大AP preciated ..

Any help is greatly appreciated..

推荐答案

您必须设法使单选按钮,通过使用适配器的getView()方法的位置参数进行检查。相反,尝试设置单选托运基于一些标识。

You must be trying to make radiobutton checked by using position parameter of the getView() method of your adapter. Instead try to set radiobutton checked based on some Id.

编辑:

从你的code的研究中,我才知道,你已经实现了这个例子:

From your code study, I came to know that you have implemented this example:

http://tokudu.com/2010/android-checkable-linear-layout /

和我早先假设,单选按钮检查状态取决于在列表视图行的位置,而不是实际的项目在其位置上的意思过滤器来改变列表视图。
我已经做了一些改动在code和现在的ListView项检查是不是基于项目的立场,但(通过匹配联系人的名字)项目本身。

and as I had assumed earlier, the radio button check state is dependent on position of the row in listview but not actual item in the listview whose position is meant to be changed on filter. I have done couple of changes in your code and now the listview item check is not based on position of the item, but item itself(by matching the name of contact).

在code变动如下:

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);
        }
    }

这篇关于在自定义列表视图筛选后单选状态选择更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆