Question

I am have a small problem in my custom dialog. When the user search for an item in the listview, the list shows the right items, but if the user for example wants to search again, the listview shows the results from the previous search.

The problem: How do I restore the listview after the first search?

My activity with the custom dialog

    edit.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                showDialog();

            }

        });

    }

}

private void showDialog() {
    dialog = new Dialog(this);
    View view = getLayoutInflater().inflate(R.layout.material_list, null);
    searchList = (ListView) view.findViewById(R.id.searchList);
    dialog.setTitle("Välj ny artikel");

    final MaterialAdapter adapter = new MaterialAdapter(
            InformationActivity.this, materialList);

    dialog.setContentView(view);

    searchList.setAdapter(adapter);

    searchList.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {

            Materials itemMat = materialList.get(position);

            resultProduct = itemMat.materialName;
            resultProductNo = itemMat.materialNo;

            result = resultProduct + " " + resultProductNo;
            Toast.makeText(getApplicationContext(), result,
                    Toast.LENGTH_SHORT).show();

        }

    });

    search = (EditText) dialog.findViewById(R.id.search);

    search.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            resultText = search.getText().toString()
                    .toLowerCase(Locale.getDefault());
            adapter.filter(resultText);

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });
    cancel = (Button) dialog.findViewById(R.id.btnCancel);
    cancel.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            dialog.dismiss();

        }
    });

    ok = (Button) dialog.findViewById(R.id.btnOK);
    ok.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            product.setText(resultProduct);
            productNo.setText(resultProductNo);
            dialog.dismiss();

        }
    });

    dialog.show();

}

}

My adapter

public class MaterialAdapter extends BaseAdapter {
LayoutInflater inflater;
Context context;
List<Materials> searchItemList = null;
ArrayList<Materials> materialList = new ArrayList<Materials>();

public MaterialAdapter(Context context, List<Materials> searchItemList) {
    this.context = context;
    inflater = LayoutInflater.from(context);
    this.searchItemList = searchItemList;
    this.materialList = new ArrayList<Materials>();
    this.materialList.addAll(searchItemList);

}

@Override
public int getCount() {
    return searchItemList.size();
}

@Override
public Object getItem(int position) {
    return searchItemList.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

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

        convertView = inflater.inflate(R.layout.d, null);
        viewHolder = new ViewHolder();
        viewHolder.tvMaterialName = (TextView) convertView
                .findViewById(R.id.tvMaterialName);
        viewHolder.tvMaterialNo = (TextView) convertView
                .findViewById(R.id.tvMaterialNo);

        convertView.setTag(viewHolder);

    } else {
        viewHolder = (ViewHolder) convertView.getTag();

    }

    viewHolder.tvMaterialName.setText((searchItemList.get(position))
            .getMaterialName());
    viewHolder.tvMaterialNo.setText((searchItemList.get(position))
            .getMaterialNo());

    return convertView;

}

public class ViewHolder {
    TextView tvMaterialName;
    TextView tvMaterialNo;

}

public void filter(String charText) {
    charText = charText.toLowerCase(Locale.getDefault());
    searchItemList.clear();
    if (charText.length() == 0) {
        searchItemList.addAll(materialList);
    } else {
        for (Materials wp : materialList) {
            if (wp.getMaterialName().toLowerCase(Locale.getDefault())
                    .startsWith(charText)) {
                searchItemList.add(wp);
            }
        }
    }
    notifyDataSetChanged();

}

}

Thanks for the help :)

Was it helpful?

Solution

Try to change search.getText() to s.toString()

    public void onTextChanged(CharSequence s, int start, int before,
            int count) {
        resultText = search.getText().toString()
                .toLowerCase(Locale.getDefault());
        adapter.filter(resultText);

    }

to

    public void onTextChanged(CharSequence s, int start, int before, int count) {
        resultText = s.toString().toLowerCase(Locale.getDefault());
        adapter.filter(resultText);
    }

A secondary thing, make materialList = searchItemList; And searchItemList empty which will be filled with search items (first run will be same elements of materialList.)

P.S Your adapter should implement Filterable in this case (implements Filterable)

OTHER TIPS

@Override
        public void afterTextChanged(Editable s) {
 resultText = search.getText().toString()
                    .toLowerCase(Locale.getDefault());
            adapter.filter(resultText);
        } 

Try this and check whether it helps

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top