2015-11-22 20 views
1

我想從數據庫添加項目到一個listView,當我使用System.out.println我看到的值,但嘗試將它們填充到自定義列表視圖時不會返回列表視圖中的任何數據。ListView空當Poputaing

啓動應用程序時,我得到一行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; 
     } 
    } 
} 
+0

請刪除'ConatctsAdapter'類的重複代碼! – pRaNaY

+0

刪除重複代碼感謝 – user5174952

回答

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

添加代碼後出現此錯誤:java.util.AbstractList.add(AbstarctList.java) – user5174952

+0

由於您使用了arrayAsList()方法,因此出現此錯誤。在onCreate方法的第一行。 http://stackoverflow.com/questions/9320409/unsupportedoperationexception-at-java-util-abstractlist-add –

+0

同樣的錯誤,即使我評論使用arrayAsList()方法出 – user5174952