在我的Android应用程序

时间:2015-07-31 04:15:07

标签: android listview

我对android开发很新。我正在尝试在我的应用中实现搜索功能&从DB检索数据并在列表视图中显示它。当我进行下一次搜索时,结果会添加到列表视图中而不是刷新。请查看我的代码并提出可能的解决方案。

    public class MainActivity extends ActionBarActivity {

    ListView lv;
    EditText etSearch;
    ImageButton ibSearch;

    InputStream is = null;
    String line = null;
    String result = null;
    String temp = "";
    String[] arr = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lv = (ListView) findViewById(R.id.lv);
        etSearch = (EditText) findViewById(R.id.etSearch);
        ibSearch = (ImageButton) findViewById(R.id.ibSearch);

        ibSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String input = etSearch.getText().toString();
                retrieveData(input);
            }
        });
    }

    public void retrieveData(String input){
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        ArrayList<NameValuePair> dataToSend = new ArrayList<>();
        dataToSend.add(new BasicNameValuePair("input", input));

        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://www.coderstop.net/android/display.php");
            httpPost.setEntity(new UrlEncodedFormEntity(dataToSend));
            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();

            is = entity.getContent();

        } catch (Exception e) {
            System.out.println("1st Exception caught");
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }

            result = sb.toString();
            is.close();

            System.out.println("----------data------------");
            System.out.println(result);


        } catch (Exception e) {
            System.out.println("2nd Exception caught");
        }


        try {
            JSONArray jsonArray = new JSONArray(result);
            int count = jsonArray.length();

            for(int i=0;i<count;i++){
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                temp += jsonObject.getString("NAME").trim()+":";
            }
            //count = 0;
            arr = null;
            System.out.println(temp);
            arr = temp.split(":");

            ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, arr);
            lv.setAdapter(adapter);
            adapter.notifyDataSetChanged();

        } catch (Exception e) {
            System.out.println("3rd Exception caught");
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

1 个答案:

答案 0 :(得分:0)

在这个for循环中,您将值赋值给temp。

for(int i=0;i<count;i++){
        JSONObject jsonObject = jsonArray.getJSONObject(i);
        temp += jsonObject.getString("NAME").trim()+":";
    }

但是你永远不会将temp的值设置为null或空字符串(“”)。因此,当您再次搜索时,新结果将添加到先前结果列表中。

解决方案是你需要设置use temp =“”;在for for循环之前。

希望这可以帮助你:) 如果您有任何问题,请告诉我 谢谢:))