Reputation: 7618
I have a custom adapter and I need to use it in onPostExecute
method to populate a ListView
with an image and a text.
This is the Custom Adapter
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;
}
}
This is my onPostExecute method:
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();
}
}
And this is my Riga class
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;
}
}
The problem is on this line:
MyAdapter myAdapter = new MyAdapter(CartelleActivity.class,arrayCartelle);
The IDE ( AndroidStudio ) says that I can't use CartelleActivity.
How can I use my Adapter?
Upvotes: 0
Views: 349
Reputation: 1973
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());
}
}
Upvotes: 0
Reputation: 7618
SOLVED. Used:
CartelleActivity.this
instead
CartelleActivity.class
Upvotes: 1
Reputation: 10948
Theres something fishy in your code. Try to define your ArrayList
in your adapter by changing it to 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);
}
Upvotes: 0