如何在Android上过滤ListView

时间:2016-11-02 20:55:30

标签: java android listview filtering

我想在我的列表视图中搜索,并且我在Stack Overflow中阅读了许多用于过滤列表视图的内容,但我仍然不知道如何过滤我的列表视图。 请帮助我如何过滤我的列表视图。 有代码 我的自定义Listview适配器:

package com.jafari.arman.contacts;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filterable;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.jar.Attributes;
import java.util.logging.Filter;

/**
 * Created by arman on 10/22/16.
 */
public class ListviewAdapter extends BaseAdapter implements Filterable {

    Context mContext;
    LayoutInflater mInflater;
    List<String> Names;
    List<String> Shomare;

    public ListviewAdapter(Context c,List<String> names,List<String> shomareha) {
        mContext = c;
        mInflater = LayoutInflater.from(c);
        Names = names;
        Shomare=shomareha;

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

    @Override
    public Object getItem(int i) {
        return null;
    }

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

    @Override
    public View getView(int position, View arg1, ViewGroup parent) {
        View rowView = mInflater.inflate(R.layout.my_listview_row,parent, false);
        TextView tv = (TextView)rowView.findViewById(R.id.name_lv);
        TextView tv_2=(TextView)rowView.findViewById(R.id.num_lv);
        tv.setText(Names.get(position));
        tv_2.setText(Shomare.get(position));
        return rowView;
    }

    @Override
    public android.widget.Filter getFilter() {
        android.widget.Filter filter = new android.widget.Filter() {

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {


            }

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {


                return null;
            }

        };
        return null;
    }
}

主要活动

package com.jafari.arman.contacts;

import android.app.Dialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import android.app.AlertDialog;

import static android.app.PendingIntent.getActivity;


public class MainActivity extends AppCompatActivity {

    public static SQLiteDatabase myDatabase;
    public static String cnames;
    public static String cshomareha;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        MainActivity.myDatabase=this.openOrCreateDatabase("mydb",SQLiteDatabase.CREATE_IF_NECESSARY,null);
        MainActivity.myDatabase.setVersion(1);
        MainActivity.myDatabase.setLocale(Locale.getDefault());
      //  String creatTable="create table if not exists User(myName Text)";
        myDatabase.execSQL("CREATE TABLE IF  NOT EXISTS "+ "User" +" (NAME TEXT, NUMBER TEXT);");
     //   MainActivity.myDatabase.execSQL(creatTable);
        Button addcontact = (Button) findViewById(R.id.addcontact);
        addcontact.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this, acActivity.class));
            }
        });



    }

    @Override
    protected void onResume() {
        final List<String> names= new ArrayList<>();
        final List<String> shomareha= new ArrayList<>();
        super.onResume();
        String query="SELECT * FROM "+"User";
        Cursor cr=myDatabase.rawQuery(query,null);
        int count=cr.getCount();
        cr.moveToFirst();

        while(!cr.isAfterLast())
        {
            String esm = cr.getString(cr.getColumnIndex("NAME"));
            names.add(esm);
            String esm2=cr.getString(cr.getColumnIndex("NUMBER"));
            shomareha.add(esm2);
            cr.moveToNext();
        }
        final ListView lv = (ListView) findViewById(R.id.lv);
        final ListviewAdapter la = new ListviewAdapter(this,names,shomareha);
        lv.setAdapter(la);
        EditText searchbox = (EditText) findViewById(R.id.tvsearch);
        lv.setTextFilterEnabled(true);

        searchbox.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, final int position, long l) {
                final String[] valuee = new String[]
                        {
                                "Edit","Delete"
                        };
                AlertDialog.Builder alertdialogbuilder = new AlertDialog.Builder(MainActivity.this);
                alertdialogbuilder.setTitle(names.get(position));
                cnames=names.get(position).toString();
                cshomareha=shomareha.get(position).toString();
                alertdialogbuilder.setItems(valuee, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                        if (i==0)
                        {
                            startActivity(new Intent(MainActivity.this,EditActivity.class));
                        }
                        if (i==1)
                        {
                            myDatabase.execSQL("DELETE FROM " + "User" + " WHERE NAME = '"+cnames+"'");
                            names.remove(position);
                            la.notifyDataSetChanged();
                        }

                    }
                });


                AlertDialog dialog = alertdialogbuilder.create();
                dialog.show();
                return false;
            }
        });
    }
}

0 个答案:

没有答案