天天看點

android ListView 顯示 查詢結果

// ListView 顯示 查詢結果

List<HashMap<String, Object>> numbers = new ArrayList<HashMap<String, Object>>();

        //callLog query

        Cursor cc = getContentResolver().query(Calls.CONTENT_URI, null, null, null, null);

        while (cc.moveToNext()){

             HashMap<String, Object> number = new HashMap<String, Object>();

            number.put("number", cc.getString(cc.getColumnIndex(Calls.NUMBER)));

            number.put("name", cc.getString(cc.getColumnIndex(Calls.CACHED_NAME)));

            number.put("image", R.drawable.ic_btn_round_plus);

            numbers.add(number);

        }

        //contacts query

        Cursor c = getContentResolver().query(Contacts.Phones.CONTENT_URI, null, null, null, null);

        while (c.moveToNext()){

            HashMap<String, Object> number = new HashMap<String, Object>();

            number.put("number", c.getString(c.getColumnIndex(Contacts.Phones.NUMBER)));

            number.put("name", c.getString(c.getColumnIndex(Contacts.People.NAME)));

            number.put("image", R.drawable.ic_btn_round_more);

            numbers.add(number);

        }

        SimpleAdapter adapter = new SimpleAdapter(this, numbers, R.layout.number_list,

                                        new String[]{"name", "number", "image"}, new int[]{R.id.nameText, R.id.numberText, R.id.image});

        listView1.setAdapter(adapter);

        cc.close();

        c.close();

//number_list.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="horizontal"

>

    <LinearLayout

         android:layout_width="160dip"

            android:layout_height="wrap_content"

            android:layout_marginRight="100dip"

            android:orientation="vertical">

        <TextView android:id="@+id/nameText"

           android:layout_width="match_parent"

           android:layout_height="wrap_content"

         />

       <TextView android:id="@+id/numberText"

           android:layout_width="match_parent"

           android:layout_height="wrap_content"

         />

    </LinearLayout>

    <ImageButton

         android:id="@+id/image"

         android:layout_width="45dip"

         android:layout_height="45dip"    

        />

</LinearLayout>

// 在listView 中過濾  相當 于 模糊 搜尋

protected void  queryFuzzy(){

        //this list would be putted the new values which are from the result of the queryAction

        List<HashMap<String, Object>> filternumbers = new ArrayList<HashMap<String, Object>>();

        String keyValue = mDigits.getText().toString();

        if(keyValue.indexOf("-") != -1){

            keyValue = keyValue.replace("-", "");

        }

        for(int i=0; i<numbers.size(); i++){

          HashMap<String , Object> map = numbers.get(i);

          String theNumber = (String) map.get("number");

          if(theNumber.indexOf("-") != -1){

              theNumber = theNumber.replace("-", "");

          }

          if(theNumber.contains(keyValue)){

              filternumbers.add( map);

          }

        }

        SimpleAdapter adapter = new SimpleAdapter(this, filternumbers, R.layout.number_list,

                new String[]{"name", "number", "image"}, new int[]{R.id.nameText, R.id.numberText, R.id.image});

        listView1.setAdapter(adapter);

    }