0

I really confused to think the way to send ArrayList into different class, i have an ArrayList named = AgenList and i want to send it into different class that extend BaseAdapter, is there anyone that can help me to solve my problem..? this is my source code : 1. MainActivity.java

public class MainActivity extends Activity {    
List AgenList = new ArrayList();
boolean boolStatusKoneksi=true;
private ProgressDialog Dialog;
protected Context applicationContext;



@Override
public void onCreate(Bundle savedInstanceState){
    new AgenAsyncTask().execute();
    super.onCreate(savedInstanceState);     
    setContentView(R.layout.mainmenu);
    GridView gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(new TesAdapter(this));
}
public class AgenAsyncTask extends AsyncTask<String, String, String>
{

@Override
    protected void onPreExecute() {
        super.onPreExecute();
        Dialog = new ProgressDialog(MainActivity.this);
        Dialog.setMessage("Mohon Tunggu sebentar...");
        Dialog.setIndeterminate(false);
        Dialog.setCancelable(true);
        Dialog.show();
    }
protected String doInBackground(String... args) {


String url = ("http://www.abc.com/xyz.php");
              try{
                  JSONParser j=new JSONParser();
                    JSONArray jsonArray = j.takeJson(url);
                  for(int i =0; i<jsonArray.length(); i++)
                  {
                      JSONObject c  = jsonArray.getJSONObject(i);
                        HashMap<String, String> map = new HashMap<String, String>();
                        if (c.has("atasan")) 
                            map.put("atasan", c.get("atasan").toString());
                        if (c.has("nama_agen")) 
                            map.put("nama_agen", c.get("nama_agen").toString());
                        if (c.has("kode_agen")) 
                            map.put("kode_agen", c.get("kode_agen").toString());
                        if (c.has("no_aaji")) 
                            map.put("no_aaji", c.get("no_aaji").toString());
                        if (c.has("jenis")) 
                            map.put("jenis", c.get("jenis").toString());
                          AgenList.add(map);


                     }
                } catch (JSONException e) {
                    e.printStackTrace();
                    }
              return null;
                }
    @Override
protected void onPostExecute(String arg) {Dialog.dismiss();}

As you see, i have an arrayList Named AgenList that containing JSONArray that i want to send into TesAdapter.java so this is the TesAdapter.java :

public class TesAdapter extends BaseAdapter{
private LayoutInflater layout;

public TesAdapter(MainActivity context) {
layout = LayoutInflater.from(context);
List AgenList = new ArrayList();
}

@Override
public int getCount() {
    return (Integer) null;
}

@Override
public Object getItem(int position) {
    return 0;
}

        @Override
        public long getItemId(int position) {
            return position;
        }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        convertView= layout.inflate(R.layout.list_item,parent,false);
        holder = new ViewHolder();

        holder.ATASAN = (TextView) convertView.findViewById(R.id.atasan);
        holder.NAMA_AGEN= (TextView) convertView.findViewById(R.id.nama_agen);
        holder.KODE_AGEN= (TextView)convertView.findViewById(R.id.kode_agen);
        holder.NO_AAJI= (TextView) convertView.findViewById(R.id.no_aaji);
        holder.JENIS= (TextView) convertView.findViewById(R.id.jenis);
        return convertView;

    }

}

 class ViewHolder{
    TextView ATASAN;
    TextView NAMA_AGEN;
    TextView KODE_AGEN;
    TextView NO_AAJI;
    TextView JENIS;

}

i hope somebody can help me to solve this problem, thanks

Aoyama Nanami
  • 2,402
  • 9
  • 29
  • 46

5 Answers5

0

Simplest way is to add an ArrayList property to the BaseAdapter class.

Then, in the class where you instantiate a new BaseAdapter, pass your ArrayList in the constructor.

public class Foo {
    public void createBaseAdapter() {
        ArrayList arList = new ArrayList();
        BaseAdapter ba = new BaseAdapter(arList);
    }
}

public class BaseAdapter {
    ArrayList arList;
    public BaseAdapter(ArrayList arList) {
        this.arList = arList;
    }
}

In your case

public TesAdapter(MainActivity context, ArrayList list) {
    layout = LayoutInflater.from(context);
    this.AgenList = list; //Declared as a class property
}
SomeShinyObject
  • 6,986
  • 6
  • 36
  • 57
0

Using public static modifier.

public static List AgenList = new ArrayList();

In other class using : MainActivity.AgenList to using AgenList

Tuan Vu
  • 5,827
  • 2
  • 12
  • 22
0

According to me. the simplest way is to public getAgentList method in your MainActivity class which can be called from TesAdapter.Offcourse there are other ways also.

Jaydeep Rajput
  • 3,359
  • 14
  • 35
0

Send ArrayList into different class

use TesAdapter constructor for sending ArrayList from MainActivity to TesAdapter. you will need to use onPostExecute method for adding adapter to GridView instead of onCreate method of Activity:

Change TesAdapter constructor as:

public class TesAdapter extends BaseAdapter{
 private LayoutInflater layout;
 List AgenList;
 Context context;
 public TesAdapter(Context context,List AgenList) {
 layout = LayoutInflater.from(context);
 this.AgenList = AgenList;
}
....

and from onPostExecute add adapter to GridView as:

@Override
protected void onPostExecute(String arg) {
 GridView gridview = (GridView)MainActivity.this. findViewById(R.id.gridview);
 gridview.setAdapter(new TesAdapter(MainActivity.this,AgenList));
 Dialog.dismiss();
}
ρяσѕρєя K
  • 127,886
  • 50
  • 184
  • 206
0

You can pass that arraylist through the construct or of the other class. Create a class member arraylist in other class. Then do this in constructor this.agentlist=passedAgentList

Vishal Vijay
  • 2,334
  • 2
  • 20
  • 41