我的片段填充ListView 当我在没有List Array的情况下使用它时它工作正常,但是简单的String []具有固定大小,我想填充一个arraylist然后将其转换为String []然后将其提供给listAdapter,因为listAdapter接受String []。 但在将Arraylist转换为String []之后,它会给出异常
public class FragmentTab2 extends SherlockFragment {
private static String url_all_products = "http://192.168.0.104/StepIn/get_all_products.php";
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "products";
private static final String TAG_PID = "uid";
private static final String TAG_NAME = "unik";
private static final String TAG_profile = "uprofile";
private static final String TAG_location = "ulocation";
private static final String TAG_isactive = "uisactive";
private static final String TAG_gcm = "ugcm";
JSONArray products = null;
List<String> _nik = new ArrayList<String>(); //get data from JSON
String[] myid=new String[5];
String[] nik;
String[] profile=new String[5];
String[] location=new String[5];
String[] isactive=new String[5];
String[] gcm=new String[5];
ListView list;
Activity activity;
ProgressDialog pDialog;
View rootView;
int d=0;
int a=0;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragmenttab2, container, false);
new LoadAllProducts().execute();
/* pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Getting Users..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show(); */
Handler handler = new Handler();
handler.postDelayed(new Runnable(){
@Override
public void run(){
// do something
}
}, 3000);
// pDialog.dismiss();
// if(nik[0]!=null)
// {
CustomListAdapter adapter=new CustomListAdapter(getActivity(), nik,profile);
list=(ListView)rootView.findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
int n=+position;
//Toast.makeText(getActivity().getApplicationContext(), Slecteditem, Toast.LENGTH_SHORT).show();
Intent i = new Intent (getActivity(),ChatBubbleActivity.class );
i.putExtra("nik", nik[n]);
i.putExtra("id", myid[n]);
i.putExtra("gcm", gcm[n]);
startActivity(i);
}
});
//}
// else
//{
// Toast.makeText(getActivity(), "Plz refresh !\n no user found"+a, Toast.LENGTH_LONG).show();
//}
return rootView;
}
/////////////////////////background////////////////////////////////////////
class LoadAllProducts extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
}
/**
* getting All products from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_products, "POST", params);
// Check your log cat for JSON reponse
Log.d("All Products: ", json.toString() + "");
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
products = json.getJSONArray(TAG_PRODUCTS);
// looping through All Products
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
// Storing each json item in variable
_nik.add(c.getString(TAG_NAME));
//nik[i]=c.getString(TAG_NAME);
myid[i] = c.getString(TAG_PID);
profile[i] = c.getString(TAG_profile);
location[i] = c.getString(TAG_location);
isactive[i] = c.getString(TAG_isactive);
gcm[i] = c.getString(TAG_gcm);
d++;
// creating new HashMap
//HashMap<String, String> map = new HashMap<String, String>();
}
nik = new String[_nik.size()-1];
a= _nik.size();
int b=d;
b=a;
nik= _nik.toArray(nik);
} else {
// no products found
// Launch Add New product Activity
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute() {
}
}
我的自定义适配器
public class CustomListAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] itemname;
private final String[] imgid;
String my="";
public CustomListAdapter(Context context, String[] itemname, String[] imgid) {
super(context, R.layout.mylist, itemname);
// TODO Auto-generated constructor stub
this.context=context;
this.itemname=itemname;
this.imgid=imgid;
}
public View getView(int position,View view,ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView=inflater.inflate(R.layout.mylist, null,true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.item);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
TextView extratxt = (TextView) rowView.findViewById(R.id.textView1);
txtTitle.setText(itemname[position]);
my=imgid[position];
Bitmap myy= ImageEncoder.StringToBitMap(my);
imageView.setImageBitmap(myy);
extratxt.setText("Description "+itemname[position]);
return rowView;
};
}
logcat的 Logs
答案 0 :(得分:1)
下面:
CustomListAdapter adapter=new CustomListAdapter(getActivity(), nik,profile);
nik
和profile
都是null
因为AsyncTask是asynchronous
调用,它将在后台运行而不会在调用execute方法后执行下一行。
所以,使用onPostExecute
方法创建CustomListAdapter
对象并将Adapter设置为ListView。
覆盖onPostExecute
类中的LoadAllProducts
方法:
@Override
protected void onPostExecute(String result) {
super.onPostExecute();
// create and set Adapter for ListView here
CustomListAdapter adapter=
new CustomListAdapter(getActivity(), nik,profile);
list.setAdapter(adapter);
}