简体   繁体   中英

Android - Custom adapter with imageview and textview not displaying listview

I am working on a college project to build an android based mobile learning app. I'm using Parse for backend services. There is a class, namely 'Course' which contains name of the courses to be offered along with an icon for each course. I have written code for custom adapter to display a list of all the courses with icons. The project is executing but the list is not appearing. I cannot figure out what is going wrong.

Here is my SelectCourse.java code

final List<Item> items=new ArrayList<Item>();

    ParseQuery<ParseObject> query = ParseQuery.getQuery("Course");
    query.orderByAscending("name");

    query.findInBackground(new FindCallback<ParseObject>() {
        @Override
        public void done(List<ParseObject> list, ParseException e) {
            if (e == null) {
                if (list.size() > 0)
                    for (int i = 0; i < list.size(); i++) {
                        final String course = list.get(i).getString("name");
                        ParseFile image = list.get(i).getParseFile("image");
                        //adapter.add(course.getString("name"));
                        image.getDataInBackground(new GetDataCallback() {
                            public void done(byte[] data, ParseException e) {
                                if (e == null) {
                                    Bitmap icon = BitmapFactory.decodeByteArray(
                                            data, 0, data.length);
                                    Item item = new Item(icon, course);
                                    items.add(item);
                                } else {
                                    Toast.makeText(getApplicationContext(),
                                            e.getMessage(),
                                            Toast.LENGTH_LONG).show();
                                }
                            }
                        });
                    }

            } else {
                Toast.makeText(getApplicationContext(),
                        e.getMessage(),
                        Toast.LENGTH_LONG).show();
            }
        }
    });

    CustomAdapter adapter=new CustomAdapter(this,items);
    ListView listView = (ListView) findViewById(R.id.course_list);
    listView.setAdapter(adapter);

This is my CustomAdapter.java code

public class CustomAdapter extends BaseAdapter {

private Context context;
private List<Item> list;

CustomAdapter(Context context, List<Item> list){
    this.context = context;
    this.list = list;

}

@Override
public int getCount() {
    return list.size();
}

@Override
public Object getItem(int position) {
    return list.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View rowView=convertView;

    if(rowView==null) {
        ViewHolder viewHolder = new ViewHolder();
        LayoutInflater layoutInflater = (LayoutInflater) context.
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rowView = layoutInflater.inflate(R.layout.list_select_course, parent, false);
        viewHolder.icon = (ImageView) rowView.findViewById(R.id.rowImageView);
        viewHolder.text = (TextView) rowView.findViewById(R.id.rowTextView);
        rowView.setTag(viewHolder);
    }

    ViewHolder viewHolder = (ViewHolder) rowView.getTag();
    viewHolder.icon.setImageBitmap(list.get(position).image);
    viewHolder.text.setText(list.get(position).text);

    return rowView;
}}

Here are ViewHolder and Item

public class ViewHolder {
ImageView icon;
TextView text;}

public class Item {
Bitmap image;
String text;
Item(Bitmap image, String text){
    this.image=image;
    this.text=text;
}}

This is content_select_course.xml

<LinearLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.rsa.minerva.SelectCourseActivity"
tools:showIn="@layout/app_bar_select_course">

<ListView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:id="@+id/course_list"
    android:layout_weight="1" />

And finally list_select_course.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="16dp">

<ImageView
    android:id="@+id/rowImageView"
    android:layout_width="48dp"
    android:layout_height="48dp" />

<TextView
    android:id="@+id/rowTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/colorButton"
    android:text="Hello World"/>

This part of the code:

query.findInBackground(new FindCallback<ParseObject>()

Doesn't run synchronously , meaning that you are actually creating an adapter with no data:

Put this snippet before you call the query.findInBackground() :

CustomAdapter adapter=new CustomAdapter(this,items);
ListView listView = (ListView) findViewById(R.id.course_list);
listView.setAdapter(adapter);

And then inside the public void done() callback, put:

adapter.notifyDataSetChanged();

After you add the items to the list with items.add(item) .

That should do it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM