我无法在列表视图中添加搜索功能

时间:2015-02-28 17:14:29

标签: java android listview

我的应用程序中有一个列表视图,当我将搜索添加到我的列表视图时,它根本不起作用。 当我将这些代码添加到我的项目中时,getfilter无法解析。

enter code here
public class MyActivity extends Activity {



InputStream in;
BufferedReader reader;
String line = "1";
public ListView listView;
VehicleAdapter adapter;
ArrayList<String> dataItems = new ArrayList<String>();
public int star = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    G.currentActivity = this;
    setContentView(R.layout.main);
    // Adad.setTestMode(true);
    ReadText();
    // String[] dataArray = dataItems.toArray(new String[0]);
    //String[] dataArray = getResources().getStringArray(R.array.listdata);
    //List<String> dataTemp = Arrays.asList(dataArray);
    // dataItems.addAll(dataTemp);
    listView = (ListView) findViewById(R.id.mainList);
    EditText ed = (EditText) findViewById(R.id.editText1);


    ArrayList<Bitmap> arr_bitmaps = new ArrayList<Bitmap>(4);
    adapter = new VehicleAdapter(MyActivity.this, arr_bitmaps, dataItems);
    listView.setAdapter(adapter);
    listView.setTextFilterEnabled(true);
    ed.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                                  int arg3) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1,
                                      int arg2, int arg3) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable arg0) {
            // vaghti kar bar harfi vared kard josteju mikone :
            MyActivity.this.adapter.getFilter().filter(arg0);
        }
    });

}

public class VehicleAdapter extends BaseAdapter {
    ArrayList<String> arr_calllog_name = new ArrayList<String>();
    public Activity context;
    ArrayList<Bitmap> imageId;
    public LayoutInflater inflater;

    public VehicleAdapter(Activity context, ArrayList<Bitmap> arr_bitmaps, ArrayList<String> arr_calllog_name) {
        super();
        this.imageId = arr_bitmaps;
        this.context = context;
        this.arr_calllog_name = arr_calllog_name;
        this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return arr_calllog_name.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    public class ViewHolder {

        TextView txtName;
        ImageButton btn;


    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.child_listview, null);
            holder.txtName = (TextView) convertView.findViewById(R.id.childTextView);
            holder.btn = (ImageButton) convertView.findViewById(R.id.childButton);
            convertView.setTag(holder);
        } else
            holder = (ViewHolder) convertView.getTag();


        holder.txtName.setText(PersianReshape.reshape(arr_calllog_name.get(position)));

        Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/B Morvarid_YasDL.com.ttf");
        //holder.txtName.setTypeface(G.defaultFont);
        holder.txtName.setTypeface(custom_font);


        holder.btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent sharingIntent = new Intent(Intent.ACTION_SEND);
                sharingIntent.setType("text/plain");
                String shareBody = arr_calllog_name.get(position);
                sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject Here");
                sharingIntent.putExtra(Intent.EXTRA_TEXT, shareBody);
                startActivity(Intent.createChooser(sharingIntent, "Share via"));
            }
        });
        return convertView;
    }

}

public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.menu, menu);
    return super.onCreateOptionsMenu(menu);
}

public boolean onOptionsItemSelected(MenuItem menuItem) {

    if (menuItem.getItemId() == R.id.contact) {
        Intent intent = new Intent(MyActivity.this, ContactUs.class);
        startActivity(intent);
    }
    return super.onOptionsItemSelected(menuItem);
}

private void ReadText() {
    try {
        in = this.getAssets().open("text.txt");
        reader = new BufferedReader(new InputStreamReader(in));
        while (line != null) {
            line = reader.readLine();
            if (line != null)
                dataItems.add(line);
            else
                break;
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

我的问题是这段代码。

enter code here
 MyActivity.this.adapter.getFilter().filter(arg0);

为什么getFilter()无法解决? 它是否适用于我的适配器? 请帮忙

2 个答案:

答案 0 :(得分:0)

  

为什么getFilter()无法解决?

因为Filterable类中没有实现VehicleAdapter接口。

要调用adapter.getFilter().filter for Activity usng Adapter对象需要在Adapter类中实现Filterable接口并覆盖getFilter

请参阅以下示例如何使适配器可过滤:

Android Filter Custom ListView (BaseAdapter)

答案 1 :(得分:0)

在自定义适配器中,它不起作用。这个搜索代码可以在SimpleAdapter中使用。对于适配器,您必须编写自定义代码进行搜索。 按照此链接,它将根据您的需要(自定义adapetr中的搜索过滤器)

Custom Listview Adapter with filter Android