-1

I have this fragment with RecyclerView

package com.example.myapplication;

import android.annotation.TargetApi;
import android.app.FragmentManager;
import android.os.Build;
import android.os.Bundle;
import android.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import java.util.ArrayList;
import java.util.List;


public class Fragment_Username_Search extends Fragment {
private List<User> userList = new ArrayList<>();
    private RecyclerView recyclerView;
    private UserAdapter userAdapter;

    @TargetApi(Build.VERSION_CODES.M)
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    private void prepareUserData() {
User user = new User(R.drawable.emilypic,"Marry Young","marbear",R.id.item_follow_button);
userList.add(user);
        user = new User(R.drawable.emilypic,"Marry Young","marbear",R.id.item_follow_button);
        userList.add(user);
        user = new User(R.drawable.emilypic,"Sammy Lee","sambam",R.id.item_follow_button);
        userList.add(user);
        user = new User(R.drawable.emilypic,"Lilly Martinson","lilmartini",R.id.item_follow_button);
        userList.add(user);
        userAdapter.notifyDataSetChanged();

    }

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

       View view =  inflater.inflate(R.layout.fragment__username__search, container, false);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
        recyclerView = (RecyclerView) view.findViewById(R.id.RecyclerView);

        userAdapter = new UserAdapter(userList);
prepareUserData();
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());

        recyclerView.setAdapter(userAdapter);
        userAdapter.SetOnItemClickListener(new UserAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                FragmentManager fm = getFragmentManager();
                android.app.DialogFragment dialogFragment = new DialogFragment();


                dialogFragment.show(fm, "Sample Fragment");
            }
        });


        return view;
    }


}

and when I must use preparedata()?When I use this after

 userAdapter = new UserAdapter(userList);

then I have only one item in my list.But in prepare data is 4 elements,how can I fix that?When I put preparedata() before this code(initialize adapter) I have error because notifyDataSetChanged recall to null object. UserAdapter:

public class UserAdapter extends RecyclerView.Adapter<UserAdapter.MyViewHolder> {
    private List<User> UserList;
OnItemClickListener mItemClickListener;
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder viewHolder = null;
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());

                    View viewUser = inflater.inflate(R.layout.row_item_layout,parent,false);
                    viewHolder = new MyViewHolder(viewUser,mItemClickListener);


        return (MyViewHolder) viewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.UserTitle.setText(UserList.get(position).getTitle());
        holder.UserDescription.setText(UserList.get(position).getDescription());
        holder.UserIcon.setImageResource(R.drawable.emilypic);
    }


    public UserAdapter(List<User> UserList){
    this.UserList = UserList;
}
    @Override
    public int getItemCount() {
        return UserList.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        public ImageView UserIcon;
        public TextView UserTitle;
        public TextView UserDescription;
        public Button UserFollowButton;
        public MyViewHolder(View itemView, UserAdapter.OnItemClickListener clickListener) {
            super(itemView);
            clickListener = (OnItemClickListener) mItemClickListener;
            UserIcon = (ImageView) itemView.findViewById(R.id.item_icon);
            UserTitle = (TextView) itemView.findViewById(R.id.item_title);
            UserDescription = (TextView) itemView.findViewById(R.id.item_description);
            UserFollowButton = (Button) itemView.findViewById(R.id.item_follow_button);
//itemView.setOnClickListener(this);
            UserFollowButton.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            mItemClickListener.onItemClick(v, getPosition());
        }

    }
    public interface OnItemClickListener{
        public void onItemClick(View view,int position);

    }
    public void SetOnItemClickListener(final OnItemClickListener mItemClickListener) {
        this.mItemClickListener = mItemClickListener;
    }

}

layout for fragment:

<RelativeLayout 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.myapplication.Fragment_Username_Search">
<LinearLayout
    android:id="@+id/Search_field"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:focusable="true"
    android:focusableInTouchMode="true"

    >
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_search_black_48dp"
        />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:tag="Search"
        android:textIsSelectable="false"

        />




</LinearLayout>

    <android.support.v7.widget.RecyclerView
       android:layout_below="@id/Search_field"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/RecyclerView"

        >

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

layout for item:

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

    <ImageView
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:id="@+id/item_icon"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="1dp"
        />
<LinearLayout
    android:layout_toRightOf="@id/item_icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/item_title"
        android:textSize="22dp"
        android:textColor="@android:color/darker_gray"

        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/item_description"
        android:textSize="16dp"

        />
</LinearLayout>
    <Button
        android:layout_width="90dp"
        android:layout_height="40dp"
        android:id="@+id/item_follow_button"
        android:layout_marginRight="30dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"


        android:background="@drawable/follow_btn"
        />
</RelativeLayout>
Rost
  • 53
  • 8

1 Answers1

1

You don't need this line userAdapter.notifyDataSetChanged() in your function prepareUserData(). Then, put the call to prepareUserData() before the initialization of the UserAdapter and remove the call of notifyDataSetChanged().

Besides that, using RecyclerView you can update the adapter itens more efficiently than notifyDataSetChanged() which is costly. Check that for more details http://developer.android.com/intl/pt-br/reference/android/support/v7/widget/RecyclerView.Adapter.html

Pedro Fernandes
  • 336
  • 1
  • 3
  • 11
  • I tried this but nothing changed:( One element on the list – Rost Mar 23 '16 at 18:18
  • 1
    prepareUserData() is only initializing one User, look, replace the code of prepareUserData() to that code: User user = new User(R.drawable.emilypic,"Marry Young","marbear",R.id.item_follow_button); userList.add(user); User user2 = new User(R.drawable.emilypic,"Marry Young","marbear",R.id.item_follow_button); userList.add(user); and then: userList.add(user); userList.add(user2); – Pedro Fernandes Mar 23 '16 at 18:19
  • I tried this,but nothing happened.If you know data structures List can have repeated records,its not a Set. – Rost Mar 23 '16 at 18:24
  • So debug the code after the call of prepareUserData() and check if there are four instances of User and answear me after. – Pedro Fernandes Mar 23 '16 at 18:30
  • I tried what you say before my question but this doesnt give a result – Rost Mar 23 '16 at 18:33
  • Did you debug your code and checked how many Users you have after the call to prepareUserData() ? – Pedro Fernandes Mar 23 '16 at 18:38
  • I make Toast with size of user list and this toast give me uselist.size()=4 – Rost Mar 23 '16 at 18:42
  • And is it still showing only one item on the screen? – Pedro Fernandes Mar 23 '16 at 19:03
  • There is nothing wrong with this code man, may you not be showing some code? – Pedro Fernandes Mar 23 '16 at 19:35