如何在访问empty adapters
时注册fragment
,并在服务器收到数据时重新绑定到RecyclerView
?
我仍然很困惑将适配器设置为空,然后当我连接到数据库时,结果将在我使用RecyclerView时显示
也许有人可以教我如何实现它
我的适配器
package adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.transvision.bertho.transvisiondashboardapp.R;
import java.util.List;
import model.Channel;
public class ChannelAdapter extends RecyclerView.Adapter<ChannelAdapter.ChannelViewHolder> {
private List<Channel> channels;
private int rowLayout;
private Context context;
public static class ChannelViewHolder extends RecyclerView.ViewHolder {
LinearLayout moviesLayout;
TextView movieTitle;
TextView data;
TextView movieDescription;
TextView rating;
TextView name;
TextView code;
TextView description;
TextView number;
TextView definition;
TextView paket;
ImageView logo;
public ChannelViewHolder(View v) {
super(v);
moviesLayout = (LinearLayout) v.findViewById(R.id.movies_layout);
name = (TextView) v.findViewById(R.id.title);
definition = (TextView) v.findViewById(R.id.subtitle);
description = (TextView) v.findViewById(R.id.description);
}
}
public ChannelAdapter(List<Channel> channels, int rowLayout, Context context) {
this.channels = channels;
this.rowLayout = rowLayout;
this.context = context;
}
@Override
public ChannelAdapter.ChannelViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(rowLayout, parent, false);
return new ChannelViewHolder(view);
}
@Override
public void onBindViewHolder(ChannelViewHolder holder, final int position) {
holder.name.setText(channels.get(position).getName());
holder.definition.setText(channels.get(position).getDefinition());
holder.description.setText(channels.get(position).getDescription());
}
@Override
public int getItemCount() {
return channels.size();
}
}
我的片段
package page;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.transvision.bertho.transvisiondashboardapp.R;
import java.util.ArrayList;
import java.util.List;
import adapter.ChannelAdapter;
import model.Channel;
import model.ChannelResponse;
import rest.ApiClient;
import rest.ApiInterface;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class HomeFragment extends Fragment {
private RecyclerView recyclerView;
private static final String TAG = HomeFragment.class.getSimpleName();
private ChannelAdapter adapter;
List<Channel> listChannel;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
adapter = new ChannelAdapter (listChannel, R.layout.list_channel, getActivity());
recyclerView = (RecyclerView) rootView.findViewById(R.id.movies_recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
getChannelData();
return rootView;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
@Override
public void onDetach() {
super.onDetach();
}
public void getChannelData() {
ApiInterface apiService = ApiClient.getChannel().create(ApiInterface.class);
Call<ChannelResponse> call = apiService.getItems();
call.enqueue(new Callback<ChannelResponse>() {
@Override
public void onResponse(Call<ChannelResponse> call, Response<ChannelResponse> response) {
int statusCode = response.code();
List<Channel> channel = response.body().getItems();
recyclerView.setAdapter(new ChannelAdapter(channel, R.layout.list_channel, getActivity()));
adapter.notifyDataSetChanged();
showToast("CONNECTION SUCCESS");
}
@Override
public void onFailure(Call<ChannelResponse> call, Throwable t) {
// Log error here since request failed
Log.e(TAG, t.toString());
showToast("CONNECTION ERROR");
}
});
}
public void showToast(String output){
Toast.makeText(getActivity(), output, Toast.LENGTH_SHORT).show();
}
}
使用notifyDataSetChanged()
请帮助我理解这个过程
谢谢
答案 0 :(得分:0)
使用空的通道列表初始化ChannelAdapter并将其设置为recyclerview。 然后,当您从api接收频道列表时,更新适配器中的列表并调用notifyDataSetChanged()。这将更新recyclerview。 您不必创建新适配器并重新设置它。
答案 1 :(得分:0)
希望你能得到你想要的东西: 在适配器中,
public void addItems(List<SomeTeam> list){
this.list.addAll(list);
notifyDataSetChanged();
}
public void clearItems(){
teams.clear();
}
您可以在适配器中将列表设置为:
时在活动中调用此方法public void setSomeTeamList(List<SomeTeam>teamList){
teamAdapter.clearItems();
teamAdapter.addItems(teamList);
}
希望您能从上面的示例代码中获得建议。
答案 2 :(得分:0)
首先初始化你的清单
List<Channel> listChannel = new ArrayList<Channel>();
现在在getChannelData()
您正在创建新适配器,同时将其设置为recyclerView
,尽管您已在onCreateView()
中创建了
recyclerView.setAdapter(adapter); //pass your adapter object instead of creating new object.
adapter.notifyDataSetChanged();