Alexey K Alexey K - 5 months ago 24
Android Question

Android - listView not showing

I try to make a list view with content from realm, but problem is that nothing shows when I open activity in emulator, just white screen.

Code is

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" />




What did I do wrong ?

Answer

I think you're missing the getCount() method in your ArrayAdapter. Also, dont forget about @Override annotation for all the method you're overriding.

Comments