2016-03-29 23 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

ここでエラーが発生しますか? line Datetoday.setText(datetime [position]);クラスのHistoryList.java内の –

+0

; @VivekMishra –

+0

Plz答えを確認し、それがあなたを助けてくれることを願ってください。 – Darshak

答えて

0

HistryFragment.javaでは、ループの繰り返しごとにdatatime、mobile、..などの配列を作成しています。

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

これは機能しません。この行にエラーがあります** SuperList(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-loopで新しい配列を作成しています。これは間違っています。

+0

でこれは機能しません。 この行にはエラーがあります ** 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()]; 

をごString配列を初期化し、それだけで後にループの反復を開始 。 それ以外の場合、ループの各繰り返しで配列がリセットされます。

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内の** super(context、R.layout.list_history、abpNo); ** –

+0

リストを作成する前に文字列配列のサイズが0より大きいことを確認してください – Sreejin

+0

はい文字列サイズが大きいゼロよりも。 –

0

この

まず印刷にdatetime GET値かどうかのログをお試しください。

そして、設定前の条件をTextViewに設定してください。

if(datetime[position]!=null){ 

    datetoday.setText(datetime[position]); 

} 

それが可能だdatatime[position]リターンnull

+0

はい、それはヌル値を返していますが、なぜですか? これにはどんな解決策がありますか? –

関連する問題