使用Spinner过滤Listview

时间:2014-12-11 16:39:30

标签: android sqlite listview filter spinner

我有一个包含列表视图和微调器的类。列表视图包含多个文本项。我想通过选择列表视图中包含一个项目的微调器项来筛选此列表视图。我试过这个,但是在选择微调项时,我只在列表视图中获得了一个项目。我的编码如下。

费用等级

private SQLiteDatabase db;
private AndroidSQLiteData sdbData;

Spinner spinCat;
ArrayAdapter<String> adapter;
private NewDatabaseHandler ndbHandler;
ArrayAdapter<String> sAdaptr;

String catString;
String[] data;
List<String> sData;

public String theCategory="";

AllDataClass allData;
ListView exList;
private ArrayList<String> kId = new ArrayList<String>();
private ArrayList<String> t_category = new ArrayList<String>();
private ArrayList<String> t_date = new ArrayList<String>();
private ArrayList<String> t_amount=new ArrayList<String>();
private ArrayList<String> t_moneytype=new ArrayList<String>();
private ArrayList<String> t_description=new ArrayList<String>();

ArrayList<String> list=new ArrayList<String>();

@Override
public void onCreate(Bundle savedInstanceState) 
{
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.expense_tab_list);
    exList=(ListView)findViewById(R.id.listEx);

    spinCat=(Spinner)findViewById(R.id.spinnerCategories);

    sdbData=new AndroidSQLiteData(this);
    sdbData.open();

    ndbHandler=new NewDatabaseHandler(getApplicationContext());
    ndbHandler.open();

}

@Override
protected void onResume()
{
    displayData(theCategory);
    super.onResume();
}

private void displayData(final String theCategory)
{

    db=sdbData.getWritableDatabase();

    Cursor mCursor = db.rawQuery("SELECT * FROM "
            +AndroidSQLiteData.TABLE_NAME, null);


    kId.clear();
    t_category.clear();
    t_amount.clear();
    t_moneytype.clear();
    t_date.clear();
    t_description.clear();

    if(mCursor.moveToFirst())
    {
        do
        {
            kId.add(mCursor.getString(mCursor.getColumnIndex(AndroidSQLiteData.KEY_ID)));
            t_category.add(mCursor.getString(mCursor.getColumnIndex(AndroidSQLiteData.KEY_CATEGORY)));
            t_date.add(mCursor.getString(mCursor.getColumnIndex(AndroidSQLiteData.KEY_DATE)));
            t_amount.add(mCursor.getString(mCursor.getColumnIndex(AndroidSQLiteData.KEY_AMOUNT)));
            t_moneytype.add(mCursor.getString(mCursor.getColumnIndex(AndroidSQLiteData.KEY_MONEY)));
            t_description.add(mCursor.getString(mCursor.getColumnIndex(AndroidSQLiteData.KEY_DESCRIPTION)));

            catString= mCursor.getString(mCursor.getColumnIndex(AndroidSQLiteData.KEY_CATEGORY));

            if ( catString.length() > 0 && ! catString.equals(theCategory) )
                  continue;

        }

        while(mCursor.moveToNext());
    }


    allData=new AllDataClass(ExpenseTabList.this,kId, t_category, t_date, t_moneytype, t_amount, t_description);

    exList.setAdapter(allData);
    exList.setTextFilterEnabled(true);


        data = ndbHandler.getAllLabels();


        for(int i=0;i<data.length;i++)
        {
            Log.i(this.toString(), data[i]);
        }

        adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, data);

        // Drop down layout style - list view with radio button
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        // attaching data adapter to spinner

        spinCat.setAdapter(adapter);


        spinCat.setOnItemSelectedListener(new OnItemSelectedListener()
        {


            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long arg3) 
            {

                ExpenseTabList.this.theCategory =spinCat.getSelectedItem().toString();

                displayData(theCategory);

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0)
            {
                // TODO Auto-generated method stub

            }

        });


    mCursor.close();
}


}

SQLite数据库类

public class AndroidSQLiteData extends SQLiteOpenHelper
{

private static final int DATABASE_VERSION = 1;


public static final String DATABASE_NAME = "expensedata";


public static final String TABLE_NAME = "expense";

// Labels Table Columns names
public static final String KEY_ID = "id";
public static final String KEY_CATEGORY = "category";
public static final String KEY_MONEY="moneytype";
public static final String KEY_DATE="date";
public static final String KEY_AMOUNT="amount";
public static final String KEY_DESCRIPTION="description";


private SQLiteDatabase db=null;

public AndroidSQLiteData(Context context)
{
    super(context,DATABASE_NAME,null,DATABASE_VERSION);
    // TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase sdb) 
{
    String CREATE_CATEGORIES_TABLE = "CREATE TABLE " + TABLE_NAME + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_CATEGORY + " TEXT,"+KEY_DATE+ " TEXT," +KEY_MONEY+" TEXT," +KEY_AMOUNT+ " TEXT," +KEY_DESCRIPTION+ " TEXT)";
    sdb.execSQL(CREATE_CATEGORIES_TABLE);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    onCreate(db);       

}

public void insertData(String category, String date, String amount, String moneytype, String description)
{
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_CATEGORY, category);
    values.put(KEY_DATE, date);
    values.put(KEY_MONEY, moneytype);
    values.put(KEY_AMOUNT, amount); 
    values.put(KEY_DESCRIPTION, description);

    db.insert(TABLE_NAME, null, values);
    db.close();


}

public List<String> getAllData(){
    List<String> data = new ArrayList<String>();

    String selectQuery = "SELECT * FROM " + TABLE_NAME;

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst())

    {
        do 
        {               
            data.add(cursor.getString(1));
        }

        while (cursor.moveToNext());
    }

    cursor.close();
    db.close();

    return data;
}

public void open()
{
    if(this.db==null)
    {
        this.db=this.getWritableDatabase();


    }

}

}

持有人类

public class AllDataClass extends BaseAdapter implements Filterable
{

private Context mContext;
private ArrayList<String> id;
private ArrayList<String> icategory;
private ArrayList<String> idate;
private ArrayList<String> iamount;
private ArrayList<String> imoneytype;
private ArrayList<String> idescription;

private View view;

public AllDataClass(Context mc,ArrayList<String> Id,ArrayList<String> icat,ArrayList<String> ida,ArrayList<String> iamt,
        ArrayList<String> imoney, ArrayList<String> ides)
{
    this.mContext=mc;
    this.id=Id;
    this.icategory=icat;
    this.idate=ida;
    this.iamount=iamt;
    this.imoneytype=imoney;
    this.idescription=ides;

}

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

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

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

@SuppressLint("InflateParams")
@Override
public View getView(int pos, View child, ViewGroup parent)
{
    view=child;
    Holder holder;

    LayoutInflater layoutInflater;

    if (view == null) 
    {
        layoutInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = layoutInflater.inflate(R.layout.expense_list_item, null);
        holder = new Holder();
        holder.txtCat=(TextView)view.findViewById(R.id.textCat);
        holder.txtDate=(TextView)view.findViewById(R.id.textDate);
        holder.txtMoneyTyp=(TextView)view.findViewById(R.id.textType);
        holder.txtDesc=(TextView)view.findViewById(R.id.textDesc);
        holder.txtAmount=(TextView)view.findViewById(R.id.textAmt);
        view.setTag(holder);

    }

    else
    {
        holder=(Holder)view.getTag();

    }


    holder.txtDate.setText("Date: "+idate.get(pos));
    holder.txtMoneyTyp.setText(iamount.get(pos));
    holder.txtCat.setText(icategory.get(pos));
    holder.txtDesc.setText(idescription.get(pos));
    holder.txtAmount.setText("Rs. "+imoneytype.get(pos));
    holder.txtAmount.setTextColor(Color.GREEN);


    return view;
}

public class Holder
{
    TextView txtCat;
    TextView txtDate;
    TextView txtMoneyTyp;
    TextView txtAmount;
    TextView txtDesc;
    TextView txtid;

}

@Override
public Filter getFilter() 
{

    return null;
}

}

如何在spinner中使用一个项目过滤整个列表项..请帮助..

2 个答案:

答案 0 :(得分:0)

我看了你的代码。我会给你一个想法。希望它会对你有所帮助。

在微调器项目上单击:

sp.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parentView, View view, int position,
                    long longPosition) {

                    yourAdapter.updateList(filteredList);
            }
        })

filteredList是List或您的数据。

最后在适配器中添加updateList函数。

public void updateList (ArrayList<Object> filteredList) {
        this.filteredList= filteredList;

        //and call notifyDataSetChanged
        notifyDataSetChanged();
    }

希望它会对你有所帮助。!!!

答案 1 :(得分:0)

只需稍作修改即可获得所需内容。变化

@Override
protected void onResume()
{
displayData();
super.onResume();
}

String theCategory = "Travel"; // if you want to start with just Travel
String theCategory = "";  // if you want to start with all categories

@Override
protected void onResume()
{
displayData(theCategory);
super.onResume();
}

因此您必须将该参数添加到displayData()&#39;。 然后在do块中首先检查类别,并仅在满足条件时添加到列表中。

添加为第一个陈述:

String category = mCursor.getString(mCursor.getColumnIndex(AndroidSQLiteData.KEY_CATEGORY));
if ( category.length > 0 && ! category.equals(theCategory) )
  continue;

在微调器事件处理程序中,您只需要以下代码:

theCategory =spinCat.getSelectedItem().toString();

displayData(theCategory);