在列表视图中搜索数据存储在db中的位置

时间:2013-01-25 07:05:02

标签: android android-listview android-arrayadapter android-sqlite android-adapter

我有一个listview个不同的数据存储在数据库中,当我点击特定数据时,它会显示该细节,我也使用了适配器类通过listview的位置,它获取要显示的数据的详细信息。

我需要添加listview的搜索,并从结果listview添加,如果我点击数据,它应该显示详细信息。请,任何人都可以为此提供代码。

公共类CustomerAdapter扩展了BaseAdapter {

public CustomerAdapter(Context context) {    
    this.context = context;
    this.layoutInflater = LayoutInflater.from(context);
    reload(0, TAKE);
}


private void reload(int skip, int take)
{
    totalSize = customers.size();
    System.out.println("Total Size:   "+totalSize);
    loadedCustomer = loadCustomers(skip, take);
}

private ArrayList<HashMap<String, Object>> loadCustomers(int skip, int take)
{
    ArrayList<HashMap<String, Object>> arrayList = new ArrayList<HashMap<String,Object>>();  

    Query query = new Query();
    query.select("x.fname, x.lname, x.surrogateKey, x.id");
    query.from("Customer", "x");
    query.orderBy("id", SortOrder.ASCENDING);
    query.setTake(take);
    query.setSkip(skip);

    lowIndex = skip;
    highIndex = lowIndex;

    QueryResultSet rs = SUP101.SUP101DB.executeQuery(query);
    while(rs.next())
    {
        String fname = rs.getString(1);
        String lname = rs.getString(2);
        long sk = rs.getLong(3);

        HashMap<String, Object> tempHashMap = new HashMap<String, Object>();
        tempHashMap.put(NAME, " " + fname + " " + lname);  
        tempHashMap.put("sk", sk);
        arrayList.add(tempHashMap);

        highIndex++;
    }

    return arrayList;  
}



public int getCount() {
    return totalSize;
}


public Object getItem(int position) {
    reloadIfNeeded(position);
    return loadedCustomer.get(position - lowIndex);
}

@SuppressWarnings("unchecked")
public String getSK(int position)
{
    HashMap<String, Object> map = (HashMap<String, Object>)getItem(position);
    return map.get("sk").toString();
}


private void reloadIfNeeded(int newPosition)
{
    if(newPosition < lowIndex || newPosition >= highIndex)
    {
        int lowIndex = (newPosition);
        reload(lowIndex, TAKE);
    }
}

public void refreshUI(boolean force)
{
    if(force)
    {
        reload(lowIndex, TAKE);
        ((Activity)context).runOnUiThread(
                new Runnable()
                {
                    public void run()
                    {
                        CustomerAdapter.this.notifyDataSetChanged();    
                    }                        
                }
           );
    }
}

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


public View getView(int position, View convertView, ViewGroup parent) {

    TextView tv = null;
    if(convertView==null){
        convertView = layoutInflater.inflate(R.layout.customer, null);
    }
    tv = (TextView)convertView.findViewById(R.id.textView1);        
    reloadIfNeeded(position);        
    tv.setText(loadedCustomer.get(position - lowIndex).get(NAME).toString());
return convertView;
}

// mainmenu class

        Mainmenu.this.runOnUiThread(new Runnable()
                    {
                        public void run()
                        {
                            adapter1 = new CustomerAdapter(Mainmenu.this);
                            listView.setAdapter(adapter1);

                            array_sort = new ArrayList<String>();
                            adapter1 =  new CustomerAdapter(Mainmenu.this);
                            listView.setAdapter(adapter1);
                            inputSearch.addTextChangedListener(new TextWatcher() {

                                @Override
                                public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                                    // When user changed the Text
                                    Mainmenu.this.adapter1.getFilter().filter(cs);  
                                }

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

                                }

                                @Override
                                public void afterTextChanged(Editable arg0) {
                                    // TODO Auto-generated method stub                          
                                }
                            });
                            listView.setOnItemClickListener(new OnItemClickListener()
                            {
                                public void onItemClick(AdapterView<?> a, View v, int position, long id)
                                {
                                    Intent intent = new Intent(Mainmenu.this, Detailview.class);
                                    intent.putExtra("sk", adapter1.getSK(position));
                                    Mainmenu.this.startActivityForResult(intent, REQUEST_DETAIL);
                                }

                            });
                        }

                    });
                }
            }).start();

2 个答案:

答案 0 :(得分:0)

您可以使用FilterQueryAdapter界面,如下所示:

  DataBaseHelper myDbHelper = new DataBaseHelper(this);
  FilterQueryProvider fqp = new myFilterQueryProvider(myDbHelper);
        <YourCursorAdapter>.setFilterQueryProvider(fqp);
        <YourListView>.setTextFilterEnabled(true); 
        <YourEditTextSearchbox>.addTextChangedListener(new TextWatcher()
        {
            @Override
            public void onTextChanged(CharSequence s, int start,
                    int before, int count) {
                // TODO Auto-generated method stub

            }
            @Override
            public void beforeTextChanged(CharSequence s, int start,
                    int count, int after) {
                // TODO Auto-generated method stub

            }
            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
                this.adaptr.getFilter().filter(s);
            }               
        });

  public class myFilterQueryProvider implements FilterQueryProvider {
    DataBaseHelper myDbHelper;
    public myFilterQueryProvider(DataBaseHelper mdb) {
        myDbHelper = mdb;
    }
    public Cursor runQuery(CharSequence constraint) 
    { 

        try {
            myDbHelper.openDataBase();
        }catch(SQLException sqle){
            throw sqle;
        }

        sql =""; //Your DB Query here
        Cursor cursor = myDbHelper.getLobbyView(sql);
        return cursor; 
    }
}

答案 1 :(得分:0)

如果您正在使用数据库

  • 只需获取一个Edittext textwatcher。
  • 每个textchanged调用db“LIKE”查询以显示结果