android-不能将适配器与 recyclerView 一起使用
android-can't use adapter with recyclerView
我正在从网络获取数据 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>)'
第二个错误在适配器上,onBindViewHolder 方法,这一行:
Information current=list.get(position);
它说:
不兼容的类型。
必填。com.example.navid.MainActivity.Information
发现:java.util.hashmap
我做错了什么?
你不应该做以下事情吗?
adapters.addAll instead of MyAdapter.addAll
你的适配器有如下列表数据
public ArrayList<HashMap<String,String>> list;
所以当你想做的时候
list.get(position);
它将 return 类型为 HashMap 的元素。但是您正在尝试将其分配给信息类型的元素。
我正在从网络获取数据 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>)'
第二个错误在适配器上,onBindViewHolder 方法,这一行:
Information current=list.get(position);
它说: 不兼容的类型。 必填。com.example.navid.MainActivity.Information 发现:java.util.hashmap
我做错了什么?
你不应该做以下事情吗?
adapters.addAll instead of MyAdapter.addAll
你的适配器有如下列表数据
public ArrayList<HashMap<String,String>> list;
所以当你想做的时候
list.get(position);
它将 return 类型为 HashMap 的元素。但是您正在尝试将其分配给信息类型的元素。