使用微调器值作为列表项的Android搜索列表视图

时间:2015-09-07 14:15:54

标签: android android-listview

我创建了一个列表视图,其中包含每个列表项上的微调器和单选按钮,并根据每个列表项上的微调器值创建了编辑文本以搜索列表视图项。

如何使用列表项中的微调器值实现搜索功能。

例如,当在编辑文本中输入1时,将列出包含在微调器中选择的1的所有列表项。

ListAdapter.java

public class ListAdapter extends ArrayAdapter<DataModel>
{
private Activity myContext;
int selectedPos = -1;
int flag = 0;
int pos = 0;

public ListAdapter(Activity context, int textViewResourceId, DataModel[] objects)
{
    super(context, textViewResourceId, objects);
    myContext = context;
}

static class ViewHolder
{
    protected DataModel data;
    protected TextView selectedText;
    protected Spinner spin;
    protected RadioButton radioBtn;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
    View view = convertView;
    final ViewHolder viewHolder;

    if (view == null)
    {
        LayoutInflater inflator = myContext.getLayoutInflater();
        view = inflator.inflate(R.layout.single_list_row, null);

        viewHolder = new ViewHolder();
        viewHolder.data = new DataModel(myContext);

        viewHolder.selectedText = (TextView) view.findViewById(R.id.singleitem);
        viewHolder.spin = (Spinner) view.findViewById(R.id.spinner1);
        viewHolder.spin.setAdapter(viewHolder.data.getAdapter());
        viewHolder.radioBtn = (RadioButton) view.findViewById(R.id.choice);

        view.setTag(viewHolder);
    }
    else
    {
        viewHolder = (ViewHolder) view.getTag();
    }

    viewHolder.spin.setOnItemSelectedListener(new OnItemSelectedListener()
    {
        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)
        {
            viewHolder.data.setSelected(arg2);
            viewHolder.selectedText.setText(viewHolder.data.getText());
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0)
        {
        }
    });

    if(flag != 1)
    {
        if (selectedPos == position)
        {
            viewHolder.radioBtn.setChecked(true);
        }
        else
        {
            viewHolder.radioBtn.setChecked(false);
        }
    }
    else
    {
        viewHolder.radioBtn.setChecked(true);
    }

    viewHolder.radioBtn.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            if(flag != 1)
            {
                if (((RadioButton) view).isChecked())
                {
                    selectedPos = position;
                }
                else
                {
                    selectedPos = -1;
                }
            }
            else
            {
                ((RadioButton) view).setChecked(true);
            }
            notifyDataSetChanged();
        }
    });
    viewHolder.selectedText.setText(viewHolder.data.getText());
    view.setTag(viewHolder);
    return view;
}
}

DataModel.java

public class DataModel
{
private int selected;
private int check;
private ArrayAdapter<CharSequence> adapter;

public DataModel(Context parent)
{
    adapter = ArrayAdapter.createFromResource(parent, R.array.options, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
}

public String getText()
{
    return (String)adapter.getItem(selected);
}

public ArrayAdapter<CharSequence> getAdapter()
{
    return adapter;
}

public int getSelected()
{
    return selected;
}

public void setSelected(int selected)
{
    this.selected = selected;
}

public int getCheck()
{
    return check;
}

public void setCheck(int check)
{
    this.check = check;
}
}

MainActivity.java

public class MainActivity extends Activity
{
int checkFlag = 1;
private ListView lv;
private ListAdapter d;
private EditText inputSearch;
private Button checkAllBtn;
private Button uncheckAllBtn;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    inputSearch = (EditText) findViewById(R.id.inputSearch);
    checkAllBtn = (Button) findViewById(R.id.checkall);
    uncheckAllBtn = (Button) findViewById(R.id.uncheckall);

    lv = (ListView) this.findViewById(R.id.listview1);
    lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    lv.setTextFilterEnabled(true);

    DataModel data = new DataModel(this);
    DataModel data1 = new DataModel(this);
    DataModel data2 = new DataModel(this);
    DataModel data3 = new DataModel(this);
    DataModel data4 = new DataModel(this);
    DataModel data5 = new DataModel(this);
    DataModel data6 = new DataModel(this);
    DataModel data7 = new DataModel(this);
    DataModel data8 = new DataModel(this);
    DataModel data9 = new DataModel(this);
    DataModel data10 = new DataModel(this);

    d = new ListAdapter(this, R.layout.single_list_row, new DataModel[] { data, data1, data2, data3, data4, data5, data6, data7, data8, data9, data10 });
    lv.setAdapter(d);

    inputSearch.addTextChangedListener(new TextWatcher()
    {
        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3)
        {
            d.getFilter().filter(cs);
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3)
        {
        }

        @Override
        public void afterTextChanged(Editable arg0)
        {

        }
    });

    checkAllBtn.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View arg0)
        {
            d.flag = 1;
            d.notifyDataSetChanged();
        }
    });

    uncheckAllBtn.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View arg0)
        {
            d.flag = 0;
            d.notifyDataSetChanged();
        }
    });
}
}

0 个答案:

没有答案