Android ListView不可见
我从MySQL数据库中获取元素,并将它们输入ListView。ListView中的arraylist获取元素,但ListView不显示在屏幕上。代码如下:Android ListView不可见,android,listview,Android,Listview,我从MySQL数据库中获取元素,并将它们输入ListView。ListView中的arraylist获取元素,但ListView不显示在屏幕上。代码如下: String name, url; final List<String> myContacts = new ArrayList<String>(); RequestQueue requestQueue; private ListView listView; protected void onCreate(Bundle
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);
}
如何解决此问题?您是否尝试过将ListView的高度和宽度设置为除内容外的其他值?尝试
match_parent
并忽略android studio的警告(它可能会告诉你wrap_content
可能更好)。哦,你应该在添加数据后在适配器上使用notifyDataSetChanged
。是的,我在添加第二条评论后就这么认为了。尝试通知…
thingmatch\u父项不起作用。有时也会显示列表。这完全是随机的。这些时候,日志显示以下3行:D/OpenGLRenderer:endAllStagingAnimators on 0xb92b9138(RippledRavable),句柄0xb93460f8 I/时间线:时间线:活动\空闲id:android.os。BinderProxy@2ff361f6时间:35910615 V/ActivityThread:UpdateVibility:ActivityRecord{2adcb561 token=android.os。BinderProxy@22b87723 {com.example.farhaan.test/com.example.farhaan.test.MainActivity}}show:false抱歉,我无法在3行中显示它们。您是否尝试将列表视图的高度和宽度设置为wrap\u content
以外的值?请尝试match\u parent
并忽略android studio的警告(它可能会告诉您,wrap\u内容
可能更好)。哦,添加数据后,您应该在适配器上使用notifydatasetchange
。添加第二条注释后,我也这么认为。尝试notify…
thingmatch\u父项不起作用。有时会显示列表。它完全是随机的。这些时候,日志会显示以下3行:D/OpenGLRenderer:endAllStagingAnimators位于0xb92b9138(RippledRavable)上,句柄为0xb93460f8 I/时间线:时间线:活动\空闲id:android.os。BinderProxy@2ff361f6时间:35910615 V/ActivityThread:UpdateVibility:ActivityRecord{2adcb561 token=android.os。BinderProxy@22b87723{com.example.farhaan.test/com.example.farhaan.test.MainActivity}show:false对不起,我不能在3行中显示它们
<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