-2

I have edit text & listview at layout, what I want add listview item value on edit texts text change event. Suppose I started typing in edit text that character will shown as list item in listview.

How could I achieve that..?

My Base adapters getView() Code as following.. I'm creating text views programatically,

    TTDemo msg = (TTDemo) this.getItem(position);

    ViewHolder viewHolder = null;
    if(convertView == null)
    {
        viewHolder = new ViewHolder();
        convertView = LayoutInflater.from(context).inflate(R.layout.row, group, false);

        viewHolder.relativeLayout = (RelativeLayout) convertView.findViewById(R.id.root_rl);

        RelativeLayout.LayoutParams rlp =  new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                                                                                                                                 RelativeLayout.LayoutParams.WRAP_CONTENT);;

        rlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        rlp.setMargins(0, 8, 0, 8);

        bg = new TextView(context);
        bg.setGravity(Gravity.CENTER);
        bg.setPadding(2, 2, 2, 2);
        bg.setTextSize(20f);
        bg.setTextColor(Color.RED);
        bg.setBackgroundColor(Color.RED);
        bg.setLayoutParams(rlp);
        viewHolder.relativeLayout.addView(bg);

        textView = new TextView(context);
        textView.setGravity(Gravity.CENTER);
        textView.setTextSize(20f);
        textView.setTextColor(Color.WHITE);
        textView.setBackgroundColor(Color.TRANSPARENT);
        textView.setLayoutParams(rlp);
        viewHolder.relativeLayout.addView(textView);

        Animation animation = AnimationUtils.loadAnimation(context, R.anim.zoom_out);
        bg.setAnimation(animation);

        viewHolder.message = msg;
        convertView.setTag(viewHolder);
    }
    else
        viewHolder = (ViewHolder) convertView.getTag();

    if(msg.getName() != null)
    {
        bg.setText("." + msg.getName() + ".");
        textView.setText("" + msg.getName());
    }
    else
    {
        viewHolder = null;
    }

    return viewHolder.relativeLayout;

And on EditTexts text change event,

et.addTextChangedListener(new new TextWatcher()
{
    @Override
    public void onTextChanged(CharSequence sequence, int start, int before, int count) 
    {
        if(sequence != null && sequence.length() > 0)
        {
           list.add(al);
           adapter.notifyDataSetChanged();
           listView.setSelection(list.size()-1);
        }
    }
});

Model Class, al is an instance of this class ( list.add(al) in textChange method)

public class TTDemo 
{
    public String name;
    public int isFinished;

    public TTDemo(String name, int isFinished) 
    {
       this.name = name;
       this.isFinished = isFinished;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

1 Answers1

0

If I understood the question correctly , your trying to filter a listView. If so, add a EditText field to the layout and add an OnTextChangeListener to your EditText field and inside its callback method apply filter to your listview's adapter. And do remember, if you want to filter a Custom BaseAdapter, make sure you implement a filterable interface for the Custom BaseAdapter.

For additional information, you can refer this question.

Community
  • 1
  • 1
whit3hawks
  • 429
  • 7
  • 14