如何从Edittext输入中显示listviewitem

时间:2015-07-31 06:41:26

标签: android

当我在Edittext中输入文本时如何显示listviewitem。我已经从网上搜索过,但我对如何做到这一点并不知情。

这是代码 MainActivity.java

public class MainActivity extends ListActivity {

private ProgressDialog pDialog;

private EditText et,search;

private static final String URL = "http://192.168.254.101/productlist.php";

JSONParser jsonParser = new JSONParser();

private static final String Success ="success";

private static final String TAG_POSTS = "message";

private static final String TAG_BRAND = "Brand";

private static final String TAG_CATEGORY = "Category";

private static final String TAG_DESCRIPTION = "Description";

private static final String TAG_CODE = "Code";

private static final String TAG_QUANTITY = "Quantity";

private static final String TAG_UNIT = "Unit";

private static final String TAG_UNITPRICE = "Unitprice";

private JSONArray mComments = null;

//manages all of our comments in a list.

private ArrayList<HashMap<String, String>> mCommentList;

@Override

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    search = (EditText)findViewById(R.id.EditText01);

    setContentView(R.layout.activity_main);

}


@Override

protected void onResume() {

    // TODO Auto-generated method stub

    super.onResume();

    //loading the comments via AsyncTask

    new LoadComments().execute();

}


public void updateJSONdata() {

    int success;

    String searchdesc = search.getText().toString();


    mCommentList = new ArrayList<HashMap<String, String>>();


    try {

        List<NameValuePair> params = new ArrayList<NameValuePair>();

        params.add(new BasicNameValuePair("username", searchdesc));

        JSONObject json = jsonParser.makeHttpRequest(
                URL, "POST", params);

        success = json.getInt(Success);



        mComments = json.getJSONArray(TAG_POSTS);

        for (int i = 0; i < mComments.length(); i++) {

            JSONObject c = mComments.getJSONObject(i);

            //gets the content of each tag

            String brand = c.getString(TAG_BRAND);

            String category = c.getString(TAG_CATEGORY);

            String description = c.getString(TAG_DESCRIPTION);

            String code = c.getString(TAG_CODE);

            String quantity = c.getString(TAG_QUANTITY);

            String unit = c.getString(TAG_UNIT);

            String unitprice = c.getString(TAG_UNITPRICE);


            HashMap<String, String> map = new HashMap<String, String>();

            map.put(TAG_BRAND, brand);

            map.put(TAG_CATEGORY, category);

            map.put(TAG_DESCRIPTION, description);

            map.put(TAG_CODE, code);

            map.put(TAG_QUANTITY, quantity);

            map.put(TAG_UNIT, unit);

            map.put(TAG_UNITPRICE, unitprice);

            mCommentList.add(map);

        }

    } catch (JSONException e) {

        e.printStackTrace();

    }

}

private void updateList() {

    ListAdapter adapter = new SimpleAdapter(this, mCommentList,

            R.layout.single_post, new String[] { TAG_BRAND, TAG_CATEGORY,

            TAG_DESCRIPTION, TAG_CODE, TAG_QUANTITY, TAG_UNIT, TAG_UNITPRICE}, new int[]{ R.id.Brand, R.id.Category,

            R.id.Description, R.id.Code, R.id.Quantity, R.id.Unit, R.id.Price 

    });

    setListAdapter(adapter);

    ListView lv = getListView();

    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override

        public void onItemClick(AdapterView<?> parent, View view,

                                int position, long id) {
        }

    });

  }

public class LoadComments extends AsyncTask<Void, Void, Boolean> {

    @Override

    protected void onPreExecute() {

        super.onPreExecute();

        pDialog = new ProgressDialog(MainActivity.this);

        pDialog.setMessage("Loading Products...");

        pDialog.setIndeterminate(false);

        pDialog.setCancelable(true);

        pDialog.show();

    }

    @Override

    protected Boolean doInBackground(Void... arg0) {

        updateJSONdata();

        return null;

    }

    @Override

    protected void onPostExecute(Boolean result) {

        super.onPostExecute(result);

        pDialog.dismiss();

        updateList();

    }

}

}

activity_main.xml中

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#fff" >

<LinearLayout

    android:id="@+id/top_layover"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:layout_alignParentLeft="true"

    android:layout_alignParentTop="true"

    android:orientation="vertical" >

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/EditText01"
        android:layout_gravity="center_horizontal"
        android:hint="Search.." />

</LinearLayout>

<ListView

    android:id="@android:id/list"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:layout_above="@+id/bottom_layover"

    android:layout_below="@+id/top_layover"

    android:background="#fff"

    android:divider="@android:color/transparent"

    android:scrollbars="none" />

<LinearLayout

    android:id="@+id/bottom_layover"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:layout_alignParentBottom="true"

    android:layout_alignParentLeft="true"


    android:orientation="horizontal"

    android:weightSum="2" >

    <LinearLayout

        android:layout_width="0dp"

        android:layout_height="wrap_content"

        android:layout_weight="1"

        android:orientation="vertical" >

    </LinearLayout>

</LinearLayout>

single_post.xml

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:background="#f0f0f0"

android:orientation="vertical" >

<LinearLayout

    android:id="@+id/box"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:layout_margin="2dp"


    android:orientation="horizontal" >


    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:orientation="vertical"

        android:paddingBottom="5dp"

        android:background="#ffffff">

        <LinearLayout

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:orientation="horizontal">

            <TextView

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:gravity="left"

                android:paddingLeft="5dp"

                android:text="Brand: ">

            </TextView>

            <TextView

                android:id="@+id/Brand"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:gravity="left"

                android:textColor="#5d5d5d"

                android:textStyle="bold" >

            </TextView>

        </LinearLayout>

        <LinearLayout

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:orientation="horizontal">


            <TextView

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:gravity="left"

                android:paddingLeft="5dp"

                android:text="Category: " >

            </TextView>

            <TextView

                android:id="@+id/Category"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:gravity="left"

                android:textColor="#5d5d5d"

                android:textStyle="bold" >

            </TextView>

        </LinearLayout>

        <LinearLayout

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:orientation="horizontal">


            <TextView

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:gravity="left"

                android:paddingLeft="5dp"

                android:text="Description: " >

            </TextView>

            <TextView

                android:id="@+id/Description"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:gravity="left"

                android:textColor="#5d5d5d"

                android:textStyle="bold" >

            </TextView>

        </LinearLayout>


        <LinearLayout

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:orientation="horizontal">


            <TextView

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:gravity="left"

                android:paddingLeft="5dp"

                android:text="Code: " >

            </TextView>

            <TextView

                android:id="@+id/Code"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:gravity="left"

                android:textColor="#5d5d5d"

                android:textStyle="bold" >

            </TextView>

        </LinearLayout>



        <LinearLayout

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:orientation="horizontal">


            <TextView

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:gravity="left"

                android:paddingLeft="5dp"

                android:text="Quantity: ">

            </TextView>

            <TextView

                android:id="@+id/Quantity"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:gravity="left"

                android:textColor="#5d5d5d"

                android:textStyle="bold" >

            </TextView>

        </LinearLayout>



        <LinearLayout

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:orientation="horizontal">


            <TextView

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:gravity="left"

                android:paddingLeft="5dp"

                android:text="Unit: " >

            </TextView>

            <TextView

                android:id="@+id/Unit"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:gravity="left"

                android:textColor="#5d5d5d"

                android:textStyle="bold" >

            </TextView>

        </LinearLayout>



        <LinearLayout

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:orientation="horizontal">


            <TextView

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:gravity="left"

                android:paddingLeft="5dp"

                android:text="Price: " >

            </TextView>

            <TextView

                android:id="@+id/Price"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:gravity="left"

                android:textColor="#5d5d5d"

                android:textStyle="bold" >

            </TextView>

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

这是php代码search.php

<?php

try{

$handler= new PDO('mysql:host=localhost;dbname=account','root','');

$handler->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);

}catch(Exception $e){


echo $e->getMessage();

die();
}

$description = $_POST['search'];

 $query =$handler->query( "Select * from tblproducts where Description like '%$description%' ");

 $records= array();

 $records =$query->fetchAll(PDO::FETCH_ASSOC);

 $json["success"]=1;

 $json["message"]=$records;

 echo json_encode($json);

 ?>

1 个答案:

答案 0 :(得分:0)

好的我用你的案子编辑我的答案 请将此行放在onCreate方法中:

setContentView(R.layout.activity_main);

在此行之上

search = (EditText)findViewById(R.id.EditText01);

之后调用此方法

search = (EditText) findViewById(R.id.EditText01);
    txt.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
                updateJSONdata(s.toString());
        }
    });

之后将updateJSONdata方法改为第一行到此

public void updateJSONdata(String searchdesc){

并评论此行

String searchdesc = search.getText().toString();

也许这个帮助