我从MySQL数据库中获取元素并将它们输入到ListView中。 ListView中的arraylist获取元素,但ListView不会显示在屏幕上。这是代码:
String name, url;
final List<String> myContacts = new ArrayList<String>();
RequestQueue requestQueue;
private ListView listView;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_inbox);
listView = (ListView) findViewById(R.id.myMessages);
url = Values.baseUrl + "getData.php?sender=" + Values.sender;
System.out.println(url);
requestQueue = Volley.newRequestQueue(getApplicationContext());
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
System.out.println(response.toString());
try {
JSONArray details = response.getJSONArray("result");
for (int i = 0; i < details.length(); i++) {
JSONObject detail = details.getJSONObject(i);
myContacts.add(detail.getString("receiver"));
System.out.println(myContacts.get(i));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
System.out.append(error.getMessage());
}
});
requestQueue.add(jsonObjectRequest);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myContacts);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(this);
}
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
try {
String output = myContacts.get(pos).toString().trim();
System.out.println(output);
Values.receiver = output;
Intent intent = new Intent(AllMessages.this, CreateMessage.class);
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
public void goToAdd(View view){
Intent intent = new Intent(AllMessages.this, AddContact.class);
startActivity(intent);
}
这是布局文件 - activity_main_inbox:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/viewText"
android:textSize="10pt"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:text="Inbox"/>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/myMessages"
android:layout_marginTop="20dp"></ListView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:onClick="goToAdd"
android:layout_marginTop="20dp"/>
</LinearLayout>
日志显示:
V/ActivityThread: updateVisibility : ActivityRecord{2942f886 token=android.os.BinderProxy@4e99420 {com.example.farhaan.test/com.example.farhaan.test.MainActivity}} show : false
我该如何解决这个问题?