CardView没有显示任何内容,使用RecyclerView和Parse.com

时间:2016-02-07 17:51:05

标签: android parse-platform android-recyclerview android-cardview

我的问题是RecyclerViewAdapter未显示内容,即CardView。它确实显示了日志中的Parse.com数据。 我几乎使用了相同的代码(除了CardView.xml和Parse.com),它工作正常。希望有人能给我解释这个问题。

Fragment.java

public class GalleryFragment extends Fragment {

View view;

private List<SocialMessage> list;
private RecyclerView recyclerView;

public GalleryFragment() {
    // Required empty public constructor
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    if (isNetworkAvailable() == false) {
        Toast.makeText(getContext(), "No Network Available...\nMake sure you're on Wifi or Cellulair Network", Toast.LENGTH_LONG).show();
    }

    getActivity().setTitle("Social Wall");

    view = inflater.inflate(R.layout.fragment_gallery, container, false);

    recyclerView = (RecyclerView) view.findViewById(R.id.social_wall_recyclerview);

    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(inflater.getContext());
    recyclerView.setLayoutManager(linearLayoutManager);
    recyclerView.setHasFixedSize(false);

    list = new ArrayList<SocialMessage>();

    getData();

    return view;
}


private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager = (ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

private void getData() {

    ParseQuery<ParseObject> query = ParseQuery.getQuery("Sweets");
    query.setLimit(10);
    query.orderByDescending("createdAt");
    query.findInBackground(new FindCallback<ParseObject>() {
        @Override
        public void done(List<ParseObject> objects, ParseException e) {
            if (e == null) {
                Log.i("AppInfo", "Berichten: " + String.valueOf(objects.size()));
                if (objects.size() > 0) {
                    for (final ParseObject object : objects) {

                        final SocialMessage message = new SocialMessage();
                        message.setMessageObjectId(object.getObjectId());
                        message.setMessageText(object.getString("content"));
                        message.setMessageDate(object.getCreatedAt().toString());

                        ParseUser userpointer = (ParseUser) object.getParseObject("sweeter");
                        String userObjectId = userpointer.getObjectId();

                        Log.i("AppInfo", userObjectId.toString());

                        ParseQuery<ParseUser> user = ParseUser.getQuery();
                                    user.whereEqualTo("objectId", userObjectId);
                                    user.findInBackground(new FindCallback<ParseUser>() {
                                        @Override
                                        public void done(List<ParseUser> objects, ParseException e) {
                                            if (e == null) {
                                    Log.i("AppInfo", "User: " + String.valueOf(objects.size()));
                                    if (objects.size() > 0) {
                                        for (ParseUser user : objects) {

                                            message.setProfileObjectId(user.getObjectId());
                                            message.setProfileUsername(user.getUsername());

                                            list.add(message);

                                            Log.i("AppInfo", message.getProfileUsername());
                                        }
                                    }
                                }
                            }
                        });
                    }
                    initializeAdapter();
                }
            } else {
                Toast.makeText(getContext(), "Oeps.. Something went wrong! Please check your internet connection", Toast.LENGTH_LONG).show();
            }
        }
    });
}

private void initializeAdapter() {
    SocialRecyclerViewAdapter adapter = new SocialRecyclerViewAdapter(list);
    recyclerView.setAdapter(adapter);
}
}

RecyclerViewAdapter.java

public class SocialRecyclerViewAdapter extends RecyclerView.Adapter<SocialRecyclerViewAdapter.SocialViewHolder> {

public class SocialViewHolder extends RecyclerView.ViewHolder {

    CardView cardView;
    TextView usernameTextField;
    TextView messageTextfield;

    public SocialViewHolder(View itemView) {
        super(itemView);

        cardView = (CardView) itemView.findViewById(R.id.socialwall_cardview);
        usernameTextField = (TextView) itemView.findViewById(R.id.socialwall_profile_username);
        messageTextfield = (TextView) itemView.findViewById(R.id.socialwall_profile_message);
    }
}

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
}

List<SocialMessage> messages;

SocialRecyclerViewAdapter(List<SocialMessage> messages) {
    this.messages = messages;
}

@Override
public SocialViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.social_wall_cardview, parent, false);
    SocialViewHolder socialViewHolder = new SocialViewHolder(v);
    return socialViewHolder;
}

@Override
public void onBindViewHolder(SocialRecyclerViewAdapter.SocialViewHolder holder, int position) {
    holder.messageTextfield.setText(messages.get(position).getMessageText());
    holder.usernameTextField.setText(messages.get(position).getMessageText());
}

@Override
public int getItemCount() {
    return messages.size();
}

fragmet.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.daxrahusen.navigationdrawervideotutorial.GalleryFragment">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/social_wall_recyclerview"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

cardview

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    style="@style/carbon_CardView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/socialwall_cardview">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="8dp">

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:id="@+id/frameLayout_imageView">

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="150dp"
                    android:id="@+id/socialwall_profile_message_image_view"
                    android:src="@drawable/hulletje"
                    android:scaleType="centerCrop"/>

                    <View
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="@drawable/imageview_gradient_dark"/>

            </FrameLayout>

            <de.hdodenhof.circleimageview.CircleImageView
                android:id="@+id/socialwall_profile_circleImageView"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_below="@+id/frameLayout_imageView"
                android:src="@drawable/tongerenlogo"
                android:scaleType="centerCrop"
                android:layout_marginTop="16dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignTop="@+id/socialwall_profile_circleImageView"
                android:layout_alignBaseline="@+id/socialwall_profile_circleImageView"
                android:text="Dax Rahusen"
                android:layout_alignEnd="@+id/frameLayout_imageView"
                android:layout_toEndOf="@+id/socialwall_profile_circleImageView"
                android:layout_marginLeft="8dp"
                android:layout_marginTop="10dp"
                android:layout_marginRight="8dp"
                android:textStyle="bold"
                android:id="@+id/socialwall_profile_username" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/socialwall_profile_message"
                android:layout_below="@+id/socialwall_profile_circleImageView"
                android:text="Dit is een CardView van de Social Wall!"
                android:layout_marginTop="8dp" />

            <carbon.widget.Button
                style="@style/carbon_Button.Flat"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Reageer"
                android:layout_below="@+id/socialwall_profile_message"
                android:layout_marginTop="8dp"
                android:textSize="12dp"
                android:id="@+id/socialwall_reageer_button"
                android:textColor="@color/colorAccent"
                app:carbon_cornerRadius="2dp"
                app:carbon_elevation="@dimen/carbon_elevationLow"
                app:carbon_rippleColor="#40ff0000"
                app:carbon_touchMarginBottom="6dp"
                app:carbon_touchMarginLeft="100dp"
                app:carbon_touchMarginRight="100dp"
                app:carbon_touchMarginTop="6dp" />

        </RelativeLayout>
    </android.support.v7.widget.CardView>
</RelativeLayout >

1 个答案:

答案 0 :(得分:0)

你正在调用findInBackground,它会调动另一个线程,然后立即调用你的主线程中的initializeAdapter,从而产生竞争条件。

如果您在initializeAdapter内拨打findInBackground FindCallback方法,那么如果您的数据从您的查询中回来,您应该会看到这些数据