在Poputaing

时间:2015-11-22 12:13:28

标签: android listview

我正在尝试将数据库中的项目添加到listView中,当我使用System.out.println时,我看到了值但是在尝试将它们填充到自定义列表视图时,在列表视图中没有返回数据。

启动应用程序时,我得到一行listview项目,但当然是空的。

public class MainActivity extends Activity implements AdapterView.OnItemClickListener {

 String jsonResult;
 String url = "http://www.mywebsite/query.php";


    ListView list;
    List<String> arrName;
    List<String> arrNumber;
    List<String> arrUsername;
    List<String> arrStatus;

    String name;
    String number;
    String username;
    String status;



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

    arrName = Arrays.asList(name);  
    arrNumber = Arrays.asList(number);  
    arrUsername = Arrays.asList(username);  
    arrStatus = Arrays.asList(status);  

    list = (ListView) findViewById(R.id.listContacts);
    ConatctsAdapter adapter = new ConatctsAdapter(this, arrName, arrNumber, arrUsername, arrStatus);
    list.setAdapter(adapter);
    list.setOnItemClickListener(this);

    accessWebService();
}

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

}






 // Async Task to access the web
 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) {
   ListDrwaer();
  }
 }// end async task

 public void accessWebService() {
  JsonReadTask task = new JsonReadTask();
  // passes values for the urls string array
  task.execute(new String[] { url });
 }

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

  try {
   JSONObject jsonResponse = new JSONObject(jsonResult);
   JSONArray jsonMainNode = jsonResponse.optJSONArray("main");
   for (int i = 0; i < jsonMainNode.length(); i++) {
    JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
    name = jsonChildNode.getString("Name");
    number = jsonChildNode.getString("Number");
    username = jsonChildNode.getString("Username");
    status = jsonChildNode.getString("Status");


    System.out.println("Name: "+name);
    System.out.println("Number: "+number);
    System.out.println("Username: "+username);
    System.out.println("Status: "+status);

   }
  } catch (JSONException e) {

      Log.i("Error Log: ", e.toString());


      System.out.println("Error: "+e.toString());
      Toast.makeText(getApplicationContext(), "Error" + e.toString(),
     Toast.LENGTH_SHORT).show();
  }

     ContactsAdapter adapter = new ContactsAdapter(this, arrName, arrNumber, arrUsername, arrStatus);
     list.setAdapter(adapter);
     list.setOnItemClickListener(this);


  }


class ContactsAdapter extends ArrayAdapter<String>
{
    Context context;
    List<String> Name;
    List<String> Number;
    List<String> Username;
    List<String> Status;

    ContactsAdapter(Context c, List<String> Name, List<String> Number, List<String> Username, List<String> Status)
    {
        super(c, R.layout.activity_contacts_single, R.id.textName, Name);
        this.context=c;
        this.Name=Name;
        this.Number=Number;
        this.Username=Username;
        this.Status=Status;
    }

    @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_contacts_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 txtStatus = (TextView) row.findViewById(R.id.textStatus);

        txtName.setText(Name.get(position));
        txtNumber.setText(Number.get(position));
        txtUsername.setText(Username.get(position));
        txtStatus.setText(Status.get(position));

        return row;
        }
    }
}

1 个答案:

答案 0 :(得分:2)

这是因为您还没有将这些项目添加到列表中,请更改此部分代码:

 try {
   JSONObject jsonResponse = new JSONObject(jsonResult);
   JSONArray jsonMainNode = jsonResponse.optJSONArray("main");
   for (int i = 0; i < jsonMainNode.length(); i++) {
    JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
    name = jsonChildNode.getString("Name");
    number = jsonChildNode.getString("Number");
    username = jsonChildNode.getString("Username");
    status = jsonChildNode.getString("Status");

    //add this part to your code
    arrName.add(name);
    arrNumber.add(number);
    arrUsername.add(username);
    arrStatus.add(status);

    System.out.println("Name: "+name);
    System.out.println("Number: "+number);
    System.out.println("Username: "+username);
    System.out.println("Status: "+status);

   }
  } catch (JSONException e) {
      Log.i("Error Log: ", e.toString());
      System.out.println("Error: "+e.toString());
      Toast.makeText(getApplicationContext(), "Error" + e.toString(),
     Toast.LENGTH_SHORT).show();
  }

     ContactsAdapter adapter = new ContactsAdapter(this, arrName, arrNumber, arrUsername, arrStatus);
     list.setAdapter(adapter);
     list.setOnItemClickListener(this);

  }

对于您的第二个问题,我认为您可以通过这种方式更改代码:

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

    //arrName = Arrays.asList(name);  
    //arrNumber = Arrays.asList(number);  
    //arrUsername = Arrays.asList(username);  
    //arrStatus = Arrays.asList(status);  

    arrName = new ArrayList<String>();
    arrNumber =new ArrayList<String>();
    arrUsername =new ArrayList<String>();
    arrStatus =new ArrayList<String>();

    list = (ListView) findViewById(R.id.listContacts);
    ConatctsAdapter adapter = new ConatctsAdapter(this, arrName, arrNumber, arrUsername, arrStatus);
    list.setAdapter(adapter);
    list.setOnItemClickListener(this);

    accessWebService();
}