搜索后在对话框中恢复列表视图

时间:2014-03-28 08:43:21

标签: android android-listview android-adapter android-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();

}

}

我的适配器

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

}

}

感谢您的帮助:)

2 个答案:

答案 0 :(得分:0)

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

试试这个并检查是否有帮助

答案 1 :(得分:0)

尝试将search.getText()更改为s.toString()

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

    }

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

第二件事,materialList = searchItemList;并且searchItemList为空将填充搜索项(首次运行将与materialList的元素相同。)

P.S在这种情况下,您的适配器应该实现Filterableimplements Filterable