Android列表视图仅显示最新项目

时间:2014-10-06 05:33:21

标签: java android xml listview

我使用parse.com中的项目填充listview。它不久前工作正常,然后由于某种原因,一旦我从eclipse更改为android工作室,它开始只显示最新项目。

适配器

public class MyCustomAdapter extends ParseQueryAdapter<ParseObject> {

        public MyCustomAdapter(Context context) {
            super(context, "Comments", R.layout.comments_listview_item);
        }

        @Override
        public View getItemView(ParseObject comment, View view, ViewGroup parent) {
            view = View.inflate(getApplicationContext(), R.layout.comments_listview_item, null);
            mListViewReferences(view);
            loadComments();

            commenttext.setText(comment.getString("Comment"));
            commentersUserName.setText(comment.getString("Commenter"));

            return super.getItemView(comment, view, parent);
        }

        /**
         * Set References
         */
        private void mListViewReferences(final View view) {
            commentersUserName = (TextView) view.findViewById(R.id.commentersUserName);
            commenttext = (TextView) view.findViewById(R.id.commenttext);
        }
    }

XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="0dp"
    android:padding="0dp">

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_margin="0dp"
        android:background="@drawable/actionmenubackground">

        <TextView
            android:id="@+id/commenttext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="11dp"
            android:layout_toRightOf="@+id/comentFullName"
            android:text="COMMENTS"
            android:textColor="@color/white"
            android:textSize="20sp"
            android:textStyle="bold" />

        <ImageButton
            android:id="@+id/backButton"
            android:layout_width="50dp"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_margin="0dp"
            android:background="@null"
            android:src="@drawable/back_arrow" />

        <TextView
            android:id="@+id/comentFullName"
            android:layout_width="2dp"
            android:layout_height="30dp"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/backButton"
            android:background="#E0E0E0" />

    </RelativeLayout>

    <EditText
        android:id="@+id/setComment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignTop="@+id/submitComment"
        android:layout_toLeftOf="@+id/submitComment"
        android:ems="10"
        android:hint="Add a comment" />

    <ImageButton
        android:id="@+id/submitComment"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:background="@drawable/actionmenubackground"
        android:src="@drawable/commentsubmitarrow" />

    <View
        android:id="@+id/view1"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_alignParentLeft="true"
        android:layout_alignTop="@+id/setComment"
        android:background="@color/green" />

    <ListView
        android:id="@+id/commentsListView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:divider="@null"
        android:dividerHeight="5dp"
        android:clickable="false"
        android:listSelector="@android:color/transparent"
        android:layout_below="@+id/relativeLayout1"
        android:layout_above="@+id/setComment"
        android:choiceMode="none" />

</RelativeLayout>

这是我的loadComments方法();

void loadComments() {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            MyCustomAdapter adapter = new MyCustomAdapter(getApplicationContext());
            commentsListView.setAdapter(adapter);
        }
    });
}

1 个答案:

答案 0 :(得分:0)

你应该返回视图

public View getItemView(ParseObject comment, View view, ViewGroup parent) {
    view = View.inflate(getApplicationContext(), R.layout.comments_listview_item, null);
    mListViewReferences(view);
    //loadComments();

    commenttext.setText(comment.getString("Comment"));
    commentersUserName.setText(comment.getString("Commenter"));

    return view;
}