相同的数据出现在整个recylerview中

时间:2019-06-11 06:08:31

标签: android mvvm android-recyclerview

相同的数据出现在整个Recyclerview中,并且位置增加,但是数据相同。

在useradapter中,我记录了位置和位置增加,但是onbindviewholder中的数据相同

useradapter

public class useradapter extends RecyclerView.Adapter<useradapter.CustomView> {

String nn = "m";

List<allusermodel> list1;
private Context context;
private LayoutInflater layoutInflater;


public useradapter(Context context, List<allusermodel> list1) {
    Log.e("reached1", nn);
    this.context = context;
    this.list1 = list1;

}


@Override
public useradapter.CustomView onCreateViewHolder(final ViewGroup parent, final int viewType) {

    Log.e("reached2", nn);
    if (layoutInflater == null) {
        layoutInflater = LayoutInflater.from(parent.getContext());
    }

    final Entrys newsBinding = Entrys.inflate(layoutInflater, parent, false);


    //  View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.innerlayout,parent,false);
    return new CustomView(newsBinding);
}

@Override
public void onBindViewHolder(useradapter.CustomView holder,int position) {
    Log.e("reached3", nn);

    //  News news = newsList.get(position);

    // holder.desc.setText(news.getDesc());

     allusermodel newsModel1 = list1.get(position);
   // Log.e("list", String.valueOf(list1));
    Log.e("nameeeee",newsModel1.getAll_user());
   // Log.e("position", String.valueOf(position));

    //Log.e("names",newsModel1.getAll_user());
    holder.bind(newsModel1);


}

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

}

public class CustomView extends RecyclerView.ViewHolder {

    private Entrys newsBinding;
   // public TextView title;

     //TextView title, desc;
    public CustomView(Entrys newsBinding) {
        super(newsBinding.getRoot());

        this.newsBinding = newsBinding;
        Log.e("reached4", nn);
       // title = (TextView)itemView.findViewById(R.id.titleval);
        //desc =(TextView)itemView.findViewById(R.id.descval);
        newsBinding.setRecyclerclick(new Presenters2() {
            @Override
            public void onclickListener() {

                int pos = getAdapterPosition();
                if (pos != RecyclerView.NO_POSITION) {
                    allusermodel clickedDataItem = list1.get(pos);

                    Intent intent = new Intent(context, messagelist.class);
                    intent.putExtra("clickid", clickedDataItem.getId());

                    context.startActivity(intent);
                }

            }
        });
    }

    public void bind(allusermodel newsModel1)

    {
        Log.e("reached5", String.valueOf(newsModel1));
        //String j = newsModel1.getAll_user();
        // Log.e("bind",nn);
        this.newsBinding.setAlluserentry(newsModel1);
    }


    public Entrys getNewsBinding() {
        Log.e("reached6", nn);
        return newsBinding;
    }

}
}

用户列表

public class userlist extends AppCompatActivity {

private static final String TAG = "userlist";

private RecyclerView recyclerView;
private LinearLayoutManager linearLayoutManager;
private DividerItemDecoration dividerItemDecoration;
private useradapter customAdapter;
private DataManager2 dataManger;
private List<allusermodel> newsList;
private RecyclerView.Adapter adapter;

String token2;
String URL1 = "https://chat.promactinfo.com/api/user";

private int userid;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_userlist);

    SharedPreferences pref = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);

    token2 = pref.getString("sherdtoken", "");
    Log.e("token", token2);

    dataManger = new DataManager2(this);

    recyclerView = (RecyclerView) findViewById(R.id.recycle);
    /*recyclerView.setLayoutManager(new LinearLayoutManager(this));
    newsList = new ArrayList<>();

    customAdapter = new useradapter(this, newsList);
    recyclerView.setAdapter(customAdapter);*/

    newsList = new ArrayList<>();


    getdata();
}

public void getdata() {



    dataManger.sendVolleyRequest1(token2, userlist.this, new DataValue() {

        @Override
        public void setJsonDataResponse1(JSONArray response) {

            allusermodel userModel = new allusermodel();
           // newsList = new ArrayList<>();
            for (int i = 0; i < response.length(); i++) {

                try {


                    JSONObject jsonObject = response.getJSONObject(i);
                   // Log.e("final", String.valueOf(i));
                  //  userid = jsonObject.getInt("id");
                    userModel.setId(jsonObject.getInt("id"));
                  //  Log.e("getid", String.valueOf(jsonObject.getInt("id")));
                    userModel.setAll_user(jsonObject.getString("name"));
                   // Log.e("getname", String.valueOf(jsonObject.getString("name")));

                    newsList.add(userModel);
                  //  Log.e("userlist", String.valueOf(newsList));



                } catch (JSONException jsonDataResponse) {
                    Log.e("error", String.valueOf(jsonDataResponse));

                }
            }
            adapter = new useradapter(getApplicationContext(),newsList);

            linearLayoutManager = new LinearLayoutManager(getApplicationContext());
            linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(), linearLayoutManager.getOrientation());

            recyclerView.setHasFixedSize(true);
            recyclerView.setLayoutManager(linearLayoutManager);
            recyclerView.addItemDecoration(dividerItemDecoration);
            recyclerView.setAdapter(adapter);

            adapter.notifyDataSetChanged();


        }

        @Override
        public void setVolleyError1(VolleyError volleyError) {
            Log.e("Volley", volleyError.toString());
        }
    });


}
}

allusermodel

public class allusermodel extends BaseObservable {


public int id;
public String all_user;


public allusermodel() {
}

public allusermodel(int id,String name ) {


    this.id = id;
    this.all_user = name;


}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getAll_user() {
    return all_user;
}

public void setAll_user(String all_user) {
    this.all_user = all_user;
}

@Override
public String toString() {
    return "allusermodel{" +
            "id=" + id +
            ", all_user='" + all_user + '\'' +
            '}';
}
}

userlist.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.example.mayurpancholi.chat_mvvm.userlist"
android:orientation="vertical">


<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:id="@+id/recycle"
    android:scrollbars="vertical">

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

innerlayout.xml

<?xml version="1.0" encoding="utf-8"?>

<layout xmlns:android="http://schemas.android.com/apk/res/android">

<data class ="Entrys">
    <variable
        name="alluserentry"

type="com.example.mayurpancholi.chat_mvvm.viewmodel.allusermodel"/>

    <variable
        name="recyclerclick"

type="com.example.mayurpancholi.chat_mvvm.interfaces.Presenters2"/>

</data>


<LinearLayout
 android:layout_width="match_parent"
android:layout_marginTop="10dp"
android:padding="10dp"
android:onClick="@{()->recyclerclick.onclickListener()}"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/titleval"
    android:textSize="20dp"
    android:text="@{alluserentry.all_user}"
    android:textStyle="bold"
    android:layout_marginTop="10dp"
   />

</LinearLayout>
</layout>

整个recyclerview具有我之前在api中添加的相同数据 (如果我在条目中输入名称,那么它将在整个recylerview中显示上一个条目 ane然后如果我在输入中输入name1,那么它将在整个recylerview中显示名称)

1 个答案:

答案 0 :(得分:0)

似乎出现了问题,因为当您从服务器填充数据时,您已经在for循环之外创建了Model对象。结果,列表中的最后一个值将显示给所有RecyclerView项目。

现在的解决方案是,在“ for-loop”中创建模型作为第一条语句。

public void getdata() {

dataManger.sendVolleyRequest1(token2, userlist.this, new DataValue() {

    @Override
    public void setJsonDataResponse1(JSONArray response) {

       //allusermodel userModel = new allusermodel();
       // newsList = new ArrayList<>();
        for (int i = 0; i < response.length(); i++) {
            allusermodel userModel = new allusermodel();
            try {
                JSONObject jsonObject = response.getJSONObject(i);

我希望它能解决您的问题。