【问题标题】:Listview is not showing anything After Keyboard back key is pressed按下键盘后退键后,Listview 不显示任何内容
【发布时间】:2014-12-11 04:47:19
【问题描述】:

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

活动:

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);
        }
    }
}

【问题讨论】:

  • 您在从 displayData1 方法更新列表后没有通知适配器...所以也通知此方法中的列表。
  • 对不起,我没有得到..我必须在 displayData1 方法中通知列表。
  • 非常感谢先生。它工作正常..
  • 好...永远记得通知适配器...

标签: android json listview keyboard


【解决方案1】:

在此通知....

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();
}}

试试这个。

【讨论】:

    猜你喜欢
    • 2013-08-25
    • 1970-01-01
    • 2022-12-18
    • 1970-01-01
    • 1970-01-01
    • 2016-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多