我正在一个大学项目上构建一个基于android的移动学习应用程序。我正在将Parse用于后端服务。有一个课程,即“课程”,其中包含要提供的课程名称以及每个课程的图标。我已经为自定义适配器编写了代码,以显示带有图标的所有课程的列表。项目正在执行,但列表未出现。我无法弄清楚出了什么问题。

这是我的SelectCourse.java代码

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);


这是我的CustomAdapter.java代码

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;
}}


这是ViewHolder和项目

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;
}}


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




最后是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"/>

最佳答案

这部分代码:

query.findInBackground(new FindCallback<ParseObject>()


不同步运行,这意味着您实际上是在创建一个没有数据的适配器:

在致电query.findInBackground()之前,请先放置以下代码段:

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


然后在public void done()回调中,放入:

adapter.notifyDataSetChanged();


items.add(item)将项目添加到列表之后。

那应该做。

08-18 07:04
查看更多