我面临的问题是来自服务器的JSON数据保存在ArrayList中,当在自定义适配器中调用ArrayList时返回null。自定义适配器用于列出android中微调器中的数据。
public class PhotoCommnFragment extends android.support.v4.app.Fragment {
EditText rechargeMobileNumber,rechargeAmount;
Spinner selectMenu;
int flags[] = {R.drawable.airteltv, R.drawable.aircel, R.drawable.dishtv, R.drawable.sundirect, R.drawable.tatasky, R.drawable.videocon};
List<SpinnerMenu> selectedNetwork = new ArrayList<>();
public PhotoCommnFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_tab_mobile, container, false);
mobileRecahrgeHistory();
rechargeMobileNumber = (EditText)rootView.findViewById(R.id.recharge_mobile_number);
rechargeAmount = (EditText)rootView.findViewById(R.id.recharge_amount);
selectMenu = (Spinner)rootView.findViewById(R.id.selectNetwork);
settingSpinnerDropDown();
return rootView;
}
public void mobileRecahrgeHistory(){
Ion.with(this)
.load("http://192.168.1.105/TotalRecharge/?api=ol&uid=1")
.asJsonObject().withResponse()
.setCallback(new FutureCallback<Response<JsonObject>>() {
@Override
public void onCompleted(Exception e, Response<JsonObject> result) {
JSONObject json = null;
try {
json = new JSONObject(result.getResult().toString());
} catch (JSONException e1) {
e1.printStackTrace();
}
// Create the root JSONObject from the JSON string.
JSONObject jsonRootObject = null;
jsonRootObject = json.optJSONObject("DS");
//Get the instance of JSONArray that contains JSONObjects
JSONArray jsonArray = jsonRootObject.optJSONArray("LST");
//Iterate the jsonArray and print the info of JSONObjects
for(int i=0; i < jsonArray.length(); i++){
JSONObject jsonObject = null;
try {
jsonObject = jsonArray.getJSONObject(i);
} catch (JSONException e1) {
e1.printStackTrace();
}
String iph = null;
String oid = jsonObject.optString("OID").toString();
String ocd = jsonObject.optString("OCD").toString();
String opd = jsonObject.optString("OPE").toString();
String mil = jsonObject.optString("MIL").toString();
String mxl = jsonObject.optString("MXL").toString();
try {
iph = jsonObject.getString("IPH").toString();
} catch (JSONException e1) {
e1.printStackTrace();
}
String urldisplay = "http://192.168.1.105/TotalRecharge/"+iph;
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e3) {
e3.printStackTrace();
}
SpinnerMenu spinnerData = new SpinnerMenu();
spinnerData.setOid(oid);
spinnerData.setOcd(ocd);
spinnerData.setOpd(opd);
spinnerData.setMil(mil);
spinnerData.setMix(mxl);
spinnerData.setImage(mIcon11);
selectedNetwork.add(spinnerData);
}
}
});
}
public void settingSpinnerDropDown(){
Fragment_DTH_Main_Spinner_Adapter customAdapter=new Fragment_DTH_Main_Spinner_Adapter(getActivity(),R.layout.fragment_dth_main_spinner_items,R.id.serviceName,selectedNetwork);
selectMenu.setAdapter(customAdapter);
}
在上面的代码中,来自服务器的json数据保存在ArrayList中的selectedNetwork.add(spinnerData);
中,但是当我在以下内容中调用已保存的arraylist数据时:
Fragment_DTH_Main_Spinner_Adapter customAdapter=new Fragment_DTH_Main_Spinner_Adapter(getActivity(),R.layout.fragment_dth_main_spinner_items,R.id.serviceName,selectedNetwork);
当在自定义适配器中传递数据时,ArrayList返回null。
当我通过调试检查时,JSON数据保存在ArrayList中,但是当它在适配器中调用时返回null。
我不知道这有什么错误。请帮我解决这个问题。 `
以下代码是在自定义adatapter中设置数据
public class Fragment_DTH_Main_Spinner_Adapter extends ArrayAdapter<SpinnerMenu> {
Context context;
int flags[];
List<SpinnerMenu> countryNames;
LayoutInflater inflter;
public Fragment_DTH_Main_Spinner_Adapter(FragmentActivity activity, int resouceId, int textviewId, List<SpinnerMenu> data) {
// super(activity, R.layout.fragment_dth_main_spinner_items,userstories);
super(activity,resouceId,textviewId,data);
this.countryNames = data;
}
public class ViewHolder
{
TextView names;
ImageView icon;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.fragment_dth_main_spinner_items, parent, false);
// convertView = inflter.inflate(fragment_dth_main_spinner_items, null);
SpinnerMenu spinnerData = countryNames.get(position);
ViewHolder viewHolder;
View result;
if(convertView == null){
viewHolder = new ViewHolder();
viewHolder.icon = (ImageView) convertView.findViewById(R.id.imageView);
viewHolder.names = (TextView) convertView.findViewById(R.id.serviceName);
result = convertView;
}
else {
viewHolder = (ViewHolder) convertView.getTag();
result = convertView;
}
// viewHolder.icon.setImageBitmap(spinnerData.getImage());
viewHolder.names.setText(spinnerData.getOpd());
return convertView;
}
}
在上面的代码中查看Holder.name.setText();该值返回null。我试过但仍然返回null。我不知道这个方法有什么问题。
答案 0 :(得分:0)
不是将项添加到selectedNetwork
数组,而是将它们添加到适配器。 e.g。
private Fragment_DTH_Main_Spinner_Adapter customAdapter;
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
...
customAdapter = new Fragment_DTH_Main_Spinner_Adapter(getActivity(),
R.layout.fragment_dth_main_spinner_items,
R.id.serviceName,
new ArrayList<SpinnerMenu>() );
}
然后替换:
selectedNetwork.add(spinnerData);
带
customAdapter.add(spinnerData);
答案 1 :(得分:0)
这是因为您的mobileRecahrgeHistory()
方法正在调用以下代码:
Ion.with(this)
.load("http://192.168.1.105/TotalRecharge/?api=ol&uid=1")
.asJsonObject().withResponse()
.setCallback(new FutureCallback<Response<JsonObject>>() {
...
selectedNetwork.add(spinnerData);
...
}
异步。 settingSpinnerDropDown();
完成后,mobileRecahrgeHistory
已经完成。
所以你需要告诉适配器它的数据是用notifyDataSetChanged()
方法改变的。