Listview没有显示任何内容按下键盘后退键

时间:2014-12-11 04:47:19

标签: android json listview keyboard

如果用户点击“提交”按钮并关闭键盘选项,则在用户输入数据并且不关闭键盘选项的情况下,在客户名称选项菜单搜索中有尚未解决的错误,在这种情况下我们从服务器获取数据。在另一种情况下,如果用户在搜索字段中输入数据并立即关闭键盘选项,然后单击“提交”按钮,在这种情况下,数据不会显示在列表视图的弹出框中。

活动:

public class SearchDetails extends Activity {
 ListView lv;
 ArrayList<ProjectVO> list;

SearchAdapter dataAdapter;
EditText searchBox,searchBox1 ;
String custSearch,flatSearch;
ArrayList<ProjectVO> searchList;
ArrayList<ProjectVO> searchList1;
ArrayList<ProjectVO> seachLis,seachLis1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_layout);
    searchList = new ArrayList<ProjectVO>();
    searchList1= new ArrayList<ProjectVO>();

    searchBox = (EditText) findViewById(R.id.editText1);

    searchBox.requestFocus();

    searchBox.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            //seachLis.clear();
            // TODO Auto-generated method stub
            searchList.clear();
            searchBox.setText("");
            searchBox.setFocusable(true);
            dataAdapter.notifyDataSetChanged();
        }
    });
    Button btn=(Button)findViewById(R.id.button1);
    Button btn1=(Button)findViewById(R.id.button2);
    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            custSearch= searchBox.getText().toString(); 
            searchBox.setFocusable(false);
            //getCustomerDetails();
            String url = "http://198.2.2.2:8080/CRMApps/CRMControllerServlet?action=searchCustomerName";

            Log.v("JSON Data Activity", url);
            new GrabURL1().execute(url);


        }
    });

    ListView lv = (ListView) findViewById(R.id.listView1);
    lv.setFocusable(false);

    dataAdapter = new SearchAdapter(this,R.layout.list_of_search, searchList);
    //if(dataAdapter)
    dataAdapter.notifyDataSetChanged();
    lv.setAdapter(dataAdapter);
    searchBox.setFocusable(true);



    btn1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            searchList.clear();


        }
    });


}

private void displayData1(String response)
{
    JSONObject responseObj = null; 

    try
    {
        Gson gson = new Gson();
        responseObj = new JSONObject(response); 
        Log.d("TestTag", "Response from server displaydata:"+response);

        JSONArray ListObj = responseObj.getJSONArray("customerSearchList");

        //list = new ArrayList<ProjectVO>();
        for (int i=0; i<ListObj.length(); i++)
        {


            String info = ListObj.getJSONObject(i).toString();

            ProjectVO list1 = gson.fromJson(info, ProjectVO.class);


            searchList.add(list1);
            Log.d("TestTag","searchList"+searchList.toString());
        }

    }
    catch (JSONException e) {
        e.printStackTrace();
    }

}


class GrabURL1 extends AsyncTask<String, Void, String> {
    private static final int REGISTRATION_TIMEOUT = 3 * 1000;
    private static final int WAIT_TIMEOUT = 30 * 1000;
    private final HttpClient httpclient = new DefaultHttpClient();
    final HttpParams params = httpclient.getParams();
    HttpResponse response;
    private String content =  null;
    private boolean error = false;
    private ProgressDialog dialog = new ProgressDialog(SearchDetails.this);

    protected void onPreExecute() {

        dialog.setMessage("Getting your data... Please wait...");
        /*new Handler().postDelayed(new Runnable() {
            public void run() {                
                dialog.dismiss();         
            }
        }, 300);*/ 
        dialog.show();

    }

    protected String doInBackground(String... urls) {


        String URL = null;

        try {
            URL = urls[0];
            HttpConnectionParams.setConnectionTimeout(params, REGISTRATION_TIMEOUT);
            HttpConnectionParams.setSoTimeout(params, WAIT_TIMEOUT);
            ConnManagerParams.setTimeout(params, WAIT_TIMEOUT);
            HttpPost httpPost = new HttpPost(URL);
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
            nameValuePairs.add(new BasicNameValuePair("customerName", custSearch.toString()));
            Log.d("TestTag","CustomerNmae custSearch"+custSearch);

            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

            response = httpclient.execute(httpPost);

            StatusLine statusLine = response.getStatusLine();

            if(statusLine.getStatusCode() == HttpStatus.SC_OK){

                ByteArrayOutputStream out = new ByteArrayOutputStream();

                response.getEntity().writeTo(out);
 out.close();

                content = out.toString();

            } else{
                //Closes the connection.
                Log.w("HTTP1:",statusLine.getReasonPhrase());
                response.getEntity().getContent().close();
                throw new IOException(statusLine.getReasonPhrase());
            }
        } catch (ClientProtocolException e) {
            Log.w("HTTP2:",e );
            Log.d("TestTag","CustomerNmae ClientProtocolException"+e);
            content = e.getMessage();
            error = true;
            cancel(true);
        } catch (IOException e) {
            Log.w("HTTP3:",e );
            content = e.getMessage();
            error = true;
            cancel(true);
        }catch (Exception e) {
            Log.w("HTTP4:",e );
            content = e.getMessage();
            error = true;
            cancel(true);
        }

        return content;
    }

    protected void onCancelled() {
        dialog.dismiss();
        Toast toast = Toast.makeText(SearchDetails.this, "Error connecting to Server", Toast.LENGTH_LONG);
        toast.setGravity(Gravity.TOP, 25, 400);
        toast.show();

    }

    protected void onPostExecute(String content) {
        dialog.dismiss();
        Toast toast;
        if (error) {
            toast = Toast.makeText(SearchDetails.this, content, Toast.LENGTH_LONG);
            toast.setGravity(Gravity.TOP, 25, 400);
            toast.show();
        } else {
            JSONObject responseObj = null; 

            try
            {

            displayData1(content);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

在这里通知......

private void displayData1(String response){JSONObject responseObj = null; 

try
{
    Gson gson = new Gson();
    responseObj = new JSONObject(response); 
    Log.d("TestTag", "Response from server displaydata:"+response);

    JSONArray ListObj = responseObj.getJSONArray("customerSearchList");

    //list = new ArrayList<ProjectVO>();
    for (int i=0; i<ListObj.length(); i++)
    {


        String info = ListObj.getJSONObject(i).toString();

        ProjectVO list1 = gson.fromJson(info, ProjectVO.class);


        searchList.add(list1);
        Log.d("TestTag","searchList"+searchList.toString());
    }
dataAdapter.notifyDataSetChanged();

}
catch (JSONException e) {
    e.printStackTrace();
}}

试试这个。