В этом приложении Mvvm Arch Chat, когда я вызываю остальные API, используя залп, я получаю ответ, но он не будет отображаться на экране в программе повторного просмотра
Userlist.class
package com.example.mayurpancholi.chat_mvvm;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import com.android.volley.VolleyError;
import com.example.mayurpancholi.chat_mvvm.adapter.useradapter;
import com.example.mayurpancholi.chat_mvvm.remote.data2.DataManager2;
import com.example.mayurpancholi.chat_mvvm.remote.data2.DataValue;
import com.example.mayurpancholi.chat_mvvm.viewmodel.allusermodel;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class userlist extends AppCompatActivity {
private RecyclerView recyclerView;
private useradapter customAdapter;
private DataManager2 dataManger;
private List<allusermodel> newsList;
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);
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"));
userModel.setAll_user(jsonObject.getString("name"));
newsList.add(userModel);
} catch (JSONException jsonDataResponse) {
Log.e("error", String.valueOf(jsonDataResponse));
}
}
customAdapter.notifyDataSetChanged();
}
@Override
public void setVolleyError1(VolleyError volleyError) {
Log.e("Volley", volleyError.toString());
}
});
}
}
InВ ответ я получу идентификатор и имя пользователя, но я хочу напечатать только имя, залп используется для вызова API, и этот код находится в арке mvvm.
useradapter.class (адаптер для списка пользователей)
package com.example.mayurpancholi.chat_mvvm.adapter;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.Toast;
import com.example.mayurpancholi.chat_mvvm.databinding.Entrys;
import com.example.mayurpancholi.chat_mvvm.interfaces.Presenters2;
import com.example.mayurpancholi.chat_mvvm.messagelist;
import com.example.mayurpancholi.chat_mvvm.viewmodel.allusermodel;
import java.util.List;
public class useradapter extends RecyclerView.Adapter<useradapter.CustomView> {
List<allusermodel> list1;
private Context context;
private LayoutInflater layoutInflater;
public useradapter(Context context,List<allusermodel> list1)
{
this.context =context;
this.list1 = list1;
}
@Override
public CustomView onCreateViewHolder(final ViewGroup parent, final int viewType) {
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(CustomView holder, int position) {
// News news = newsList.get(position);
// holder.title.setText(news.getTitle());
// holder.desc.setText(news.getDesc());
allusermodel newsModel1 = list1.get(position);
holder.bind(newsModel1);
}
@Override
public int getItemCount() {
return list1.size();
}
public class CustomView extends RecyclerView.ViewHolder {
private Entrys newsBinding;
// TextView title, desc;
public CustomView(Entrys newsBinding) {
super(newsBinding.getRoot());
this.newsBinding = newsBinding;
//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("bind", String.valueOf(newsModel1));
this.newsBinding.setAlluserentry(newsModel1);
}
public Entrys getNewsBinding()
{
return newsBinding;
}
}
}
alluser.class (класс модели для списка пользователей)
package com.example.mayurpancholi.chat_mvvm.model;
public class allusers {
public int id;
public String all_user;
public allusers(int Id,String All_user ) {
id = Id;
all_user = All_user;
}
public allusers() {
}
}
allusermodel.class (просмотр класса модели дляuserlist)
package com.example.mayurpancholi.chat_mvvm.viewmodel;
import com.example.mayurpancholi.chat_mvvm.model.allusers;
public class allusermodel {
public int id;
public String all_user;
public allusermodel() {
}
public allusermodel(allusers user2) {
this.id= user2.id;
this.all_user=user2.all_user;
}
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;
}
}
activity_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:layout_marginBottom="20dp"
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"
/>
Это не таквозвращать ошибки, но он не отображал список пользователей в представлении recyler. Я думаю, что проблема в recyclerview
, поэтому дайте мне знать, в чем проблема.