我真的很困惑想到将ArrayList发送到不同的类的方法,我有一个名为= AgenList的ArrayList,我想将它发送到扩展BaseAdapter的不同类,有没有人可以帮助我解决我的问题.. ? 这是我的源代码: 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();}
如您所见,我有一个名为AgenList的arrayList,其中包含我想要发送到TesAdapter.java的JSONArray 所以这是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;
}
我希望有人可以帮我解决这个问题,谢谢
答案 0 :(得分:0)
最简单的方法是向ArrayList
类添加BaseAdapter
属性。
然后,在您实例化new BaseAdapter
的类中,在构造函数中传递ArrayList
。
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;
}
}
public TesAdapter(MainActivity context, ArrayList list) {
layout = LayoutInflater.from(context);
this.AgenList = list; //Declared as a class property
}
答案 1 :(得分:0)
使用public static
修饰符。
public static List AgenList = new ArrayList();
在其他课程中使用:MainActivity.AgenList
使用AgenList
答案 2 :(得分:0)
据我说。最简单的方法是 MainActivity 类中的 public getAgentList 方法,可以从 TesAdapter 调用.Offcourse还有其他方法。
答案 3 :(得分:0)
将ArrayList发送到不同的类
使用TesAdapter构造函数将MainActivity
的ArrayList发送到TesAdapter
。您需要使用onPostExecute
方法将适配器添加到GridView
而不是onCreate方法的活动:
将TesAdapter构造函数更改为:
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;
}
....
并从onPostExecute
添加适配器到GridView:
@Override
protected void onPostExecute(String arg) {
GridView gridview = (GridView)MainActivity.this. findViewById(R.id.gridview);
gridview.setAdapter(new TesAdapter(MainActivity.this,AgenList));
Dialog.dismiss();
}
答案 4 :(得分:0)
您可以通过构造或其他类传递该arraylist。在其他类中创建一个类成员arraylist。然后在构造函数中执行此操作 this.agentlist = passedAgentList