Android ListView not showing on real device but on emulator does

江枫思渺然 提交于 2020-01-25 03:52:04

问题


I'm having a weird problem regarding the simple ListView. On my emulator everything is allright and the data is loaded correctly from JSON API, the data also loads on my device. The problem is that on my emulator, the listview is populated but on my real device, not, even if there is data from JSON, why ?

    private void addItemsToListView(JSONObject message) {
    try {
        JSONArray jsonArray = message.getJSONArray("android");
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject c = jsonArray.getJSONObject(i);
            String imagePath = c.getString("path");
            try {
                myImages.add(new ImagesModel(i, imagePath, R.drawable.mtr));
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

回答1:


Got the problem, I've changed the layout from RelativeLayout to LinearLayout and it is all working fine.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/view_background"
android:orientation="vertical"
android:padding="10dp"
android:weightSum="1">

<ProgressBar
    android:id="@+id/progressBar1"
    style="@style/Base.Widget.AppCompat.ProgressBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:progress="@integer/abc_config_activityShortDur"
    android:layout_centerInParent="true"
    android:maxHeight="100dp"
    android:maxWidth="100dp"
    android:minHeight="200dp"
    android:minWidth="200dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/btnUploadedActivity"
    android:id="@+id/imagesListViewTitle"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imagesListView"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignLeft="@+id/imagesListViewTitle"
    android:layout_below="@+id/imagesListViewTitle"
    android:smoothScrollbar="true" />
</LinearLayout>


来源:https://stackoverflow.com/questions/29938466/android-listview-not-showing-on-real-device-but-on-emulator-does

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!