ListView不在真实设备上工作

时间:2015-12-07 20:22:35

标签: android listview

我的listview有问题,它适用于模拟器但不适用于真实设备。我测试了2个真实的设备,但没有填充。我使用json从数据库获取listview数据。 Json结果没问题,因为它打印到logcat并在模拟器上填充listview。

ListView Java:

        public class ActivityRequestsFrom extends MainActivity implements AdapterView.OnItemClickListener {

    ______________________________________________________________________________

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

    lv = (ListView) findViewById(R.id.listRequests);

    RequestsAdapter adapter = new RequestsAdapter(this, arrRequest_Name, arrRequest_Number, 
            arrRequest_Username, arrRequest_Result, imageId);

    lv.setAdapter(adapter);     
    lv.setOnItemClickListener(this);

        }

    ______________________________________________________________________________

    class RequestsAdapter extends ArrayAdapter<String>
                {
                    Context context;
                    List<String> Request_Name;
                    List<String> Request_Number;
                    List<String> Request_Username;
                    List<String> Request_Result;
                    Integer[] imgid;

    RequestsAdapter(Context c, List<String> Request_Name, 
                    List<String> Request_Number, List<String> Request_Username,
                    List<String> Request_Result, Integer[] imgid)
                    {

    super(c, R.layout.activity_requests_single, R.id.textName, Request_Name);
                    this.context=c;
                    this.Request_Name=Request_Name; 
                    this.Request_Number=Request_Number; 
                    this.Request_Username=Request_Username; 
                    this.Request_Result=Request_Result; 
                    this.imgid=imgid;   
                    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
                    // TODO Auto-generated method stub
    View row=convertView;               
    if(row==null)
        {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    row = inflater.inflate(R.layout.activity_requests_single, parent, false);       }
    TextView txtName = (TextView) row.findViewById(R.id.textName);
    TextView txtNumber = (TextView) row.findViewById(R.id.textNumber);
    TextView txtUsername = (TextView) row.findViewById(R.id.textUsername);
    TextView txtResult = (TextView) row.findViewById(R.id.textResult);
    ImageView imageView = (ImageView) row.findViewById(R.id.imageView);
    Map<String, Integer> drawableMap = new HashMap<String, Integer>();
    drawableMap.put("ok",R.drawable.request_pending_from);
    drawableMap.put("pending",R.drawable.request_pending_from);         
    drawableMap.put("rejected",R.drawable.request_rejected_from);
            drawableMap.put("blocked",R.drawable.request_blocked_from);
            txtName.setText(Request_Name.get(position));
            txtNumber.setText(Request_Number.get(position));
            txtUsername.setText(Request_Username.get(position));
            txtResult.setText(Request_Result.get(position));
            //imageView.setImageResource(imgid[position]);
    imageView.setImageResource(drawableMap.get(Request_Result.get(position).toLowerCase()));
    return row;
                        }
                    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    // TODO Auto-generated method stub

    TextView tvUsername = (TextView) view.findViewById(R.id.textUsername);
    usernameSelected = tvUsername.getText().toString();

    TextView tvResult = (TextView) view.findViewById(R.id.textResult);
    resultSelected = tvResult.getText().toString();

    if (resultSelected.equals("Pending"))
        {
        pendingOptions();                   
        }

    else if (resultSelected.equals("Rejected"))
        {
        rejectedOptions();  
        }

    else if (resultSelected.equals("Blocked"))
        {
        blockedOptions();   

        }

    else
        {
        }


    }

ListView布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ListView
    android:id="@+id/listRequest"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
</ListView>

ListView单一布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<TextView
    android:id="@+id/textName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/LLdummy"
    android:text="Name"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textStyle="italic"
    android:layout_marginLeft="10dp"
     />

<TextView
    android:id="@+id/textUsername"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textNumber"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/textNumber"
    android:text="Username"
    android:textAppearance="?android:attr/textAppearanceSmall" />



<TextView
    android:id="@+id/textNumber"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textName"
    android:layout_below="@+id/textName"
    android:text="Number"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textStyle="bold" />

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/LLdummy"
    android:layout_centerVertical="true"
    android:src="@drawable/request_pending_to" />

的Json

private class JsonReadTask extends AsyncTask<String, Void, String> {
      @Override
      protected String doInBackground(String... params) {
       HttpClient httpclient = new DefaultHttpClient();
       HttpPost httppost = new HttpPost(params[0]);
       try {
        HttpResponse response = httpclient.execute(httppost);
        jsonResult = inputStreamToString(response.getEntity().getContent()).toString();
       }


       catch (ClientProtocolException e) {
        e.printStackTrace();
       } catch (IOException e) {
        e.printStackTrace();
       }
       return null;
      }

      private StringBuilder inputStreamToString(InputStream is) {
       String rLine = "";
       StringBuilder answer = new StringBuilder();
       BufferedReader rd = new BufferedReader(new InputStreamReader(is));

       try {
        while ((rLine = rd.readLine()) != null) {
         answer.append(rLine);
        }
       }

       catch (IOException e) {
        // e.printStackTrace();
        Toast.makeText(getApplicationContext(),"Error..." + e.toString(), Toast.LENGTH_LONG).show();
       }
       return answer;
      }

      @Override
      protected void onPostExecute(String result) {
        try{  
       ListDrwaer(); //has ConnectionException (when it cannot reach server)
        }catch (Exception e){
            Toast.makeText(getApplicationContext(), "Please check your connection..", Toast.LENGTH_LONG).show();
            }
      }
     }// end async task

     public void accessWebService() {
      JsonReadTask task = new JsonReadTask();
      // passes values for the urls string array
      task.execute(new String[] { "http://server/file.php?pIMEI="+IMEI });
      }

     // build hash set for list view
     public void ListDrwaer() {

      try {
       JSONObject jsonResponse = new JSONObject(jsonResult);
       JSONArray jsonMainNode = jsonResponse.optJSONArray("request_info");

       for (int i = 0; i < jsonMainNode.length(); i++) {
        JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);

        String request_name = jsonChildNode.optString("Request_Name");
        String request_number = jsonChildNode.optString("Request_Number");
        String request_username = jsonChildNode.optString("Request_Username");
        String request_result = jsonChildNode.optString("Request_Result");

        arrRequest_Name.add(request_name);
        arrRequest_Number.add(request_number);
        arrRequest_Username.add(request_username);
        arrRequest_Result.add(request_result);

        System.out.println("Request_Name: "+request_name);
        System.out.println("Request_Number: "+request_number);
        System.out.println("Request_Username: "+request_username);
        System.out.println("Request_Result: "+request_result);

       }
      } catch (JSONException e) {
          System.out.println("Json Error Requests" +e.toString());
          Toast.makeText(getApplicationContext(), "No Requests Pending", Toast.LENGTH_SHORT).show();

      }
}

调用异步

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

     ...

    accessWebService();

    //ListView
    lv = (ListView) findViewById(R.id.listRequests);

    RequestsAdapter adapter = new RequestsAdapter(this, arrRequest_Name, arrRequest_Number, arrRequest_Username, arrRequest_Result, imageId);
    lv.setAdapter(adapter);
    lv.setOnItemClickListener(this);

1 个答案:

答案 0 :(得分:2)

此处的问题是,Adapter上设置ListView时未收到数据。这是因为数据填充在AysncTask中,其本质上是 async hronous。这意味着它将在后台运行而另一个代码运行(即ListView填充。

因此,在onPostExecute()中,您需要使用notifyDataSetChanged()ListView知道要填充新项目。

@Override
  protected void onPostExecute(String result) {
    // your code

    adapter.notifyDataSetChanged(); // this line here
 }// end async task

在您的示例中,这显然会要求您将Adapter作为成员变量或将其传递给AsyncTask