它给了我一个空白的活动而没有显示我的列表..我不知道是什么错误。谁能帮忙? 这是我的代码:
public class Home extends AppCompatActivity implements HomeCallBack {
public ArrayList<info> info;
private Adaptor myadaptor;
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
recyclerView=(RecyclerView)findViewById(R.id.recycle_view);
BackgroundHome backgroundHome=new BackgroundHome(this,this);
backgroundHome.execute();
}
@Override
public void processData(JSONArray data)
{
JSONObject js;
if(data!=null)
{
info=new ArrayList<>();
try
{
for(int i=0;i<data.length();i++)
{
js =data.getJSONObject(i);
info in=new info(js.getString("Names"),js.getString("city"),js.getString("university"));
info.add( in);
}
myadaptor=new Adaptor(info);
recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
LinearLayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter( myadaptor);
当我在create方法中调用recyclelerView.setAdapter(myadapter)时,它会给我一个错误和app停止。 这是我的适配器代码:
public class Adaptor extends RecyclerView.Adapter<Adaptor.ViewHolder> {
private ArrayList<info> info;
public Adaptor(ArrayList<info> info) {
this.info = info;
}
public void updateList(ArrayList<info> data) {
info = data;
notifyDataSetChanged();
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_view, parent, false);
return new ViewHolder(itemView);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position)
{
info i= info.get(position);
holder.name.setText(i.getName());
holder.city.setText(i.getCity());
holder.city.setText(i.getUniversity());
}
@Override
public int getItemCount() {
return info.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView name, city, uni;
public ViewHolder(final View v) {
super(v);
name = (TextView) v.findViewById(R.id.name);
city = (TextView) v.findViewById(R.id.city);
uni = (TextView) v.findViewById(R.id.university);
}
}
}
这是我的适配器类,上面是我的Home活动,我试图显示我的列表
答案 0 :(得分:0)
我认为问题在于您尝试将适配器设置在主线程之外。你应该使用这段代码:
@Override
public void processData(JSONArray data)
{
JSONObject js;
if(data!=null)
{
info=new ArrayList<>();
try
{
for(int i=0;i<data.length();i++)
{
js =data.getJSONObject(i);
info in=new info(js.getString("Names"),js.getString("city"),js.getString("university"));
info.add( in);
}
runOnUiThread(new Runnable() {
@Override
public void run() {
myadaptor=new Adaptor(info);
recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
LinearLayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter( myadaptor);
}
});
答案 1 :(得分:0)
尝试在onCreate中初始化适配器,recyclerview和layoutmanager。然后当你在processData中调用完成后获取列表并调用mAdapter.notifyDataSetChanged。
public class Home extends AppCompatActivity implements HomeCallBack {
public ArrayList<info> info;
private Adaptor myadaptor;
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
recyclerView=(RecyclerView)findViewById(R.id.recycle_view);
info = new ArrayList<>();
myadaptor=new Adaptor(info);
recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
LinearLayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(myadaptor);
BackgroundHome backgroundHome=new BackgroundHome(this,this);
backgroundHome.execute();
}
@Override
public void processData(JSONArray data) {
JSONObject js;
if(data!=null) {
try {
for(int i=0; i<data.length(); i++)
{
js = data.getJSONObject(i);
info in = new info(js.getString("Names"),js.getString("city"),js.getString("university"));
info.add(in);
}
myadaptor.notifyDataSetChanged();