我从网上获取数据json数据,这是我的设置适配器到我的recycleView的代码:
RecyclerView recycle;
MyAdapter adapters;
private static String url;
private static final String TAG_CONTACTS = "contacts";
JSONArray contacts = null;
ProgressDialog pDialog;
private int preLast;
@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();
}
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) {
Spots_tab1_json sh = new Spots_tab1_json();
String jsonStr = sh.makeServiceCall(url, Spots_tab1_json.GET);
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("url", new String(c.getString("url").getBytes("ISO-8859-1"), "UTF-8"));
contact.put("text", new String(c.getString("text").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);
}else{
MyAdapter.addAll(result);
}
}else{
//MyToast.makeText(MainActivity.this, DariGlyphUtils.reshapeText(MainActivity.this.getResources().getString(R.string.problemload)));
}
}
}
public class Information {
String thumbnail;
String title ;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getThumbnail() {
return thumbnail;
}
public void setThumbnail(String thumbnail) {
this.thumbnail = thumbnail;
}
}
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();
}
@Override
public void onBindViewHolder(MyViewHolder viewHolder, int position) {
Information current=list.get(position);
viewHolder.txt.setText(current.title);
}
@Override
public int getItemCount() {
return list.size();
}
class MyViewHolder extends RecyclerView.ViewHolder{
TextView txt ;
ImageView img ;
public MyViewHolder(View itemView) {
super(itemView);
txt= (TextView) itemView.findViewById(R.id.txt);
img= (ImageView) itemView.findViewById(R.id.img);
}
}
}
有两部分代码我遇到问题: 1-在onPostExecute中,这一行:
MyAdapter.addAll(result);
错误是:非静态方法'addAll(java.util.ArrayList&gt;)'无法从静态上下文引用
第二个错误发生在适配器onBindViewHolder方法上,这一行:
Information current=list.get(position);
它说:
不兼容的类型。
必需.com.example.navid.MainActivity.Information
找到:java.util.hashmap
我在做什么是错的?
答案 0 :(得分:2)
你不应该这样吗?
adapters.addAll instead of MyAdapter.addAll
您的适配器具有以下列表数据。
public ArrayList<HashMap<String,String>> list;
所以当你想要做的时候
list.get(position);
它将返回HashMap类型的元素。但是你试图将它分配给信息类型的元素。