我有一个自定义适配器,我需要在onPostExecute
方法中使用它来填充带有图像和文本的ListView
。
这是自定义适配器
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
public class MyAdapter extends BaseAdapter {
private Activity mActivity;
private ArrayList listaElementi;
private static LayoutInflater infilater = null;
public MyAdapter( Activity a, ArrayList list) {
mActivity = a;
listaElementi = list;
infilater = (LayoutInflater) mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return listaElementi.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if( convertView == null )
vi = infilater.inflate(R.layout.riga, null);
TextView nome = (TextView) vi.findViewById(R.id.nome);
ImageView immagine = (ImageView) vi.findViewById(R.id.immagine);
Riga rigaCorrente = (Riga) listaElementi.get(position);
nome.setText(rigaCorrente.getNome());
immagine.setImageDrawable(mActivity.getResources().getDrawable(rigaCorrente.getImmagine()));
return vi;
}
}
这是我的onPostExecute方法:
private class LongOperation extends AsyncTask<String, String, JSONObject> {
//Other methods here
protected void onPostExecute(JSONObject result) {
JSONObject jobj = null;
JSONArray elenco_cartelle = null;
try {
jobj = new JSONObject(String.valueOf(result));
} catch (JSONException e) {
e.printStackTrace();
}
//Provo a recuperare i campi json
try {
elenco_cartelle = jobj.getJSONArray("elenco");
} catch (JSONException e) {
e.printStackTrace();
}
ArrayList<Riga> arrayCartelle = new ArrayList<Riga>();
//DEvo scorrere le'elenco delle cartelle
for (int i = 0; i < elenco_cartelle.length(); i++) {
try {
String nome = elenco_cartelle.getString(i);
arrayCartelle.add( new Riga( nome , R.drawable.ic_launcher ) );
} catch (JSONException e) {
e.printStackTrace();
}
}
MyAdapter myAdapter = new MyAdapter(CartelleActivity.class,arrayCartelle);
mainListView.setAdapter(myAdapter);
// Close progress dialog
Dialog.dismiss();
}
}
这是我的里加课程
public class Riga {
String nome;
Integer idImmagine;
public Riga( String nome, Integer idImmagine ) {
super();
this.nome = nome;
this.idImmagine = idImmagine;
}
public String getNome() {
return this.nome;
}
public Integer getImmagine() {
return this.idImmagine;
}
public void setNome( String nome ) {
this.nome = nome;
}
public void setIdImmagine( Integer idImmagine ) {
this.idImmagine = idImmagine;
}
}
问题在于这一行:
MyAdapter myAdapter = new MyAdapter(CartelleActivity.class,arrayCartelle);
IDE(AndroidStudio)说我不能使用CartelleActivity 我该如何使用我的适配器?
答案 0 :(得分:1)
解决。 使用:
CartelleActivity.this
代替
CartelleActivity.class
答案 1 :(得分:0)
你的代码中有些东西可疑。尝试通过将适配器更改为ArrayList
:
ArrayList<Riga>
private Activity mActivity;
private ArrayList<Riga> listaElementi;
private static LayoutInflater infilater = null;
public MyAdapter( Activity a, ArrayList<Riga> list) {
mActivity = a;
listaElementi = list;
infilater = (LayoutInflater) mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
答案 2 :(得分:0)
public class LongOperation extends AsyncTask<String, String, JSONObject> {
private Activity a;
public CustomAsync(Activity a) {
this.a = a;
}
@Override
protected JSONObject doInBackground(String... params) {
// TODO Auto-generated method stub
return null;
}
@Override
protected void onPostExecute(JSONObject result) {
super.onPostExecute(result);
MyAdapter myAdapter = new MyAdapter(a, new ArrayList());
}
}