我是回收视图中的新手。这是我的RecycerView的示例代码。我从互联网获取数据,在stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent event) {
System.out.println("I will not close");
event.consume();
}
});
我设置了适配器。
onPostExecute()
运行后,我收到此错误:
RecyclerView recycle;
MyAdapter adapters;
private static String url;
private static final String TAG_CONTACTS = "contacts";
JSONArray contacts = null;
ProgressDialog pDialog;
private int preLast;
int page = 0, in, to;
Boolean loadmore = true;
HashMap<String, String> item;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
url = "http://192.168.1.20/adres/getAds.php";
recycle = (RecyclerView) findViewById(R.id.recycle);
recycle.setItemAnimator(new DefaultItemAnimator());
new GetContacts().execute();
final GestureDetector mGestureDetector = new GestureDetector(MainActivity.this, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
});
}
private class GetContacts extends AsyncTask<Void, Void, ArrayList<HashMap<String, String>>> {
Boolean goterr = false;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected ArrayList<HashMap<String, String>> doInBackground(Void... arg0) {
String jsonStr = Fun.getHtml(url);
Log.v("this", jsonStr);
ArrayList<HashMap<String, String>> dataC = new ArrayList<HashMap<String, String>>();
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
contacts = jsonObj.getJSONArray(TAG_CONTACTS);
if (contacts.length() < 20)
loadmore = false;
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
HashMap<String, String> contact = new HashMap<String, String>();
contact.put("id", new String(c.getString("id").getBytes("ISO-8859-1"), "UTF-8"));
contact.put("name", new String(c.getString("name").getBytes("ISO-8859-1"), "UTF-8"));
dataC.add(contact);
dataC.add(contact);
}
} catch (JSONException e) {
Log.v("this", e.getMessage());
goterr = true;
} catch (UnsupportedEncodingException e) {
Log.v("this", e.getMessage());
goterr = true;
}
} else {
goterr = true;
}
return dataC;
}
@Override
protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
super.onPostExecute(result);
if (pDialog.isShowing() && pDialog != null)
pDialog.dismiss();
if (!isCancelled() && goterr == false && result != null) {
if (adapters == null) {
adapters = new MyAdapter(MainActivity.this, result);
recycle.setAdapter(adapters);
recycle.setLayoutManager(new LinearLayoutManager(MainActivity.this));
} else {
adapters.addAll(result);
}
} else {
//MyToast.makeText(MainActivity.this, DariGlyphUtils.reshapeText(MainActivity.this.getResources().getString(R.string.problemload)));
}
}
}
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private LayoutInflater inflater;
public ArrayList<HashMap<String, String>> list;
public MyAdapter(Context context, ArrayList<HashMap<String, String>> list) {
inflater = LayoutInflater.from(context);
this.list = list;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parrent, int i) {
View view = inflater.inflate(R.layout.customrow, parrent, false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
public void addAll(ArrayList<HashMap<String, String>> result) {
if (this.list == null) {
this.list = result;
} else {
this.list.addAll(result);
}
notifyDataSetChanged();
}
public HashMap<String, String> geting(int position) {
return list.get(position);
}
@Override
public void onBindViewHolder(MyViewHolder viewHolder, int position) {
item = list.get(position);
viewHolder.txt.setText(item.get("onvan"));
}
@Override
public int getItemCount() {
return list.size();
}
class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView txt;
ImageView img;
public MyViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
txt = (TextView) itemView.findViewById(R.id.txt);
img = (ImageView) itemView.findViewById(R.id.img);
}
@Override
public void onClick(View v) {
item = adapters.geting(getPosition());
Log.v("this", "id " + item.get("id"));
/*Intent in=new Intent (FistActiivty.this,AdDetails.class);
in.putExtra("ad_id",item.get("id"));
startActivity(in)*/
;
}
}
}
这段代码有什么问题?
答案 0 :(得分:1)
问题是在第一次布局过程中,当您的AsyncTask
仍在从网络获取数据时,您的RecyclerView没有适配器。
您可以在onCreate()
中附加(空)适配器,并在onPostExecute()
中更新适配器的数据。只需确保适配器正确处理空数据集。