JSON存在問題。JSON有問題
HistoryFragment.java
private View rootView;
ListView list;
AppSharedPreference appSharedPreference;
private ProgressDialog progress;
String[] datetime;
String[] mobile;
String [] amount;
String[] operator;
String[] abpNo;
String[] optNo;
String[] rechargeStatus;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_history, container, false);
appSharedPreference = AppSharedPreference.getInstance(getActivity());
callHistoryApi();
HistoryList adapter = new HistoryList(getActivity(),datetime,mobile,amount,operator,abpNo,optNo,rechargeStatus);
list=(ListView)rootView.findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
return rootView;
}
@SuppressWarnings("unchecked")
private void callHistoryApi() {
Map<String, String> param = null;
try {
param = new HashMap<String, String>();
param.put("username", appSharedPreference.getUserName());
param.put("trpass", appSharedPreference.getPassword());
param.put("action", "RECH_HISTORY");
}
catch(Exception e)
{
e.printStackTrace();
}
String jobjstr = param.toString();
Log.e("LOGINREQUESTTTTTTT", jobjstr);
if (jobjstr != null) {
AllInOneAsyncTask asyncTask = new AllInOneAsyncTask(getActivity());
asyncTask.setServiceResultListener(this);
asyncTask.setServiceType(Constants.SERVICE_TYPE_GET_PROFILE);
asyncTask.execute(param);
}
}
@Override
public void onResult(String resultData, int requestType) {
// TODO Auto-generated method stub
if (requestType == Constants.SERVICE_TYPE_GET_PROFILE) {
try {
JSONObject jObject = new JSONObject(resultData);
String Status = jObject.getString("Status");
JSONArray history = jObject.getJSONArray("History");
for (int i=0; i < history.length(); i++)
{
JSONObject oneObject = history.getJSONObject(i);
String Datetime = oneObject.getString("Datetime");
String Mobile = oneObject.getString("Mooble");
String Amount = oneObject.getString("Amount");
String Operator = oneObject.getString("Operator");
String ABPNo = oneObject.getString("ABPNo");
String OPTNO = oneObject.getString("OPTNO");
String Recharge_Status = oneObject.getString("Recharge_Status");
Log.e("HISTORYYYYYYYYYYYY",Datetime+" "+Mobile+" "+Amount+" "+Operator+" "+ABPNo+" "+
OPTNO+" "+Recharge_Status);
datetime = new String[history.length()];
mobile = new String[history.length()];
amount = new String[history.length()];
operator = new String[history.length()];
abpNo = new String[history.length()];
optNo = new String[history.length()];
rechargeStatus = new String[history.length()];
datetime[i] = Datetime;
mobile[i] = Mobile;
amount[i] = Amount;
operator[i] = Operator;
abpNo[i] = ABPNo;
optNo[i] = OPTNO;
rechargeStatus[i] = Recharge_Status;
}
}
catch(Exception e)
{
}
}
}
HistoryList.java
public HistoryList(Activity context,String[] datetime,String[] mobile,String[] amount,String[] operator,String[] abpNo,
String[] optNo,String[] rechargeStatus) {
super(context, R.layout.list_history, abpNo);
this.context = context;
this.datetime=datetime;
this.mobile=mobile;
this.amount = amount;
this.operator=operator;
this.abpNo=abpNo;
this.optNo=optNo;
this.rechargeStatus=rechargeStatus;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_history, null, true);
TextView datetoday = (TextView) rowView.findViewById(R.id.date);
TextView txtTitle = (TextView) rowView.findViewById(R.id.id);
TextView oper = (TextView) rowView.findViewById(R.id.operator);
TextView amoun = (TextView) rowView.findViewById(R.id.amount);
TextView statu = (TextView) rowView.findViewById(R.id.status);
datetoday.setText(datetime[position]);
txtTitle.setText("id ="+abpNo[position]);
oper.setText(" operator ="+operator[position]);
amoun.setText(" amount ="+amount[position]);
statu.setText(" status ="+rechargeStatus[position]);
return rowView;
}
有這一個問題,當我打開這個片段有錯誤試圖從空閱讀陣列怎麼能解決這個問題。 請幫忙。
在哪裏它給錯誤? –
類中的HistoryList.java on datetoday.setText(datetime [position]); @VivekMishra –
Plz檢查答案,希望它會幫助你。 – Darshak