2016-03-29 52 views
0

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; 
} 

有這一個問題,當我打開這個片段有錯誤試圖從空閱讀陣列怎麼能解決這個問題。 請幫忙。

+0

在哪裏它給錯誤? –

+0

類中的HistoryList.java on datetoday.setText(datetime [position]); @VivekMishra –

+0

Plz檢查答案,希望它會幫助你。 – Darshak

回答

0

在HistryFragment.java中,您已爲每個循環迭代創建datatime,mobile,.. etc數組。

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; 
      } 

這將導致問題。在循環之前創建數組並添加項目。

+0

這是行不通的。這條線上有一個錯誤** super(context,R.layout.list_history,abpNo); **在HistoryList.java中 –

0

這樣做。

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()]; 

    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[i] = Datetime; 
      mobile[i] = Mobile; 
      amount[i] = Amount; 
      operator[i] = Operator; 
      abpNo[i] = ABPNo; 
      optNo[i] = OPTNO; 
      rechargeStatus[i] = Recharge_Status; 
     } 

您正在for循環中創建新數組 - 這是錯誤的事情。

+0

這不起作用。 這條線上有錯誤 ** super(context,R.layout.list_history,abpNo); ** in HistoryList.java –

0

這裏的問題是字符串數組沒有得到values.Do以下。在循環開始之前 初始化String數組,

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()]; 

後,纔開始你的循環迭代。 否則它會在循環的每次迭代中重置數組。

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[i] = Datetime; 
       mobile[i] = Mobile; 
       amount[i] = Amount; 
       operator[i] = Operator; 
       abpNo[i] = ABPNo; 
       optNo[i] = OPTNO; 
       rechargeStatus[i] = Recharge_Status; 
      } 

快樂編碼...

+0

這是行不通的。這條線上有一個錯誤** super(context,R.layout.list_history,abpNo); ** HistoryList.java中的** –

+0

確保在填充列表之前您的字符串數組大小大於零 – Sreejin

+0

是字符串大小更大比零。 –

0

試試這個

首頁打印日誌爲datetime獲取價值與否。

並給出設定值前TextView這樣的檢查條件。

if(datetime[position]!=null){ 

    datetoday.setText(datetime[position]); 

} 

有可能datatime[position]回報null

+0

是的,它返回空值,但爲什麼? 你有任何解決方案嗎? –