Android - listView没有显示

时间:2016-03-05 13:16:23

标签: android listview

我尝试使用来自领域的内容制作列表视图,但问题是当我在模拟器中打开活动时只显示白屏。

代码是

public class ListActivity extends AppCompatActivity {

ListView list;
Realm realm;
Button button;

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

    realm = Realm.getDefaultInstance();

    RealmResults<ArticleList> results = realm.where(ArticleList.class).equalTo("category", "Танковые сражения").findAll();

    ListActivityListAdapter adapter = new ListActivityListAdapter(this, results);
    list = (ListView) findViewById(R.id.listactivity_listview);
    list.setAdapter(adapter);

    button = (Button) findViewById(R.id.button);

}

}

public class ListActivityListAdapter extends ArrayAdapter<String> {

private final Activity context;
private final RealmResults<ArticleList> articleList;

public ListActivityListAdapter(Activity context, RealmResults<ArticleList> articleList) {
    super(context, R.layout.listview_listactivity_item);
    this.context = context;
    this.articleList = articleList;
}

public View getView(int position,View view,ViewGroup parent) {

    LayoutInflater inflater=context.getLayoutInflater();
    View rowView=inflater.inflate(R.layout.listview_listactivity_item, null, true);

    TextView title = (TextView) rowView.findViewById(R.id.listview_item_title);
    TextView subtitle = (TextView) rowView.findViewById(R.id.listview_item_subtitle);
    ImageView image = (ImageView) rowView.findViewById(R.id.listview_item_imageView);

    ArticleList item = articleList.get(position);

    title.setText(item.getTitle());
    subtitle.setText(item.getSubtitle());

    String imageName =  item.getImage();
    File imageFile = ImageStorage.getImage(imageName, getContext());

    Bitmap bitmap = null;

    try {
        bitmap = BitmapFactory.decodeFile(context.getFilesDir() + "/tanks/" + imageName + ".jpg");
    } catch (Exception e) {
        e.printStackTrace();
    }

    image.setImageBitmap(bitmap);

    return rowView;

}
}

activity_list_of_items.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.me.demo2.ListActivity"
tools:showIn="@layout/listview_main_list">

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/listactivity_listview"
    android:layout_gravity="center_horizontal|top"
    />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="164dp" />

listview_listactivity_item.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="213dp"
    android:id="@+id/listview_item_imageView"
    android:layout_gravity="center_horizontal|top" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:id="@+id/listview_item_title"
    android:paddingTop="100dp"
    android:layout_gravity="center_horizontal|top" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:id="@+id/listview_item_subtitle"
    android:paddingTop="150dp"
    android:layout_gravity="center_horizontal|top" />

我做错了什么?

2 个答案:

答案 0 :(得分:2)

我认为您错过了getCount()中的ArrayAdapter方法。另外,不要忘记您覆盖的所有方法的@Override注释。

答案 1 :(得分:0)

adapter.notifyDataChanged()

之后尝试list.setAdapter(adapter);