2016-10-23 66 views
2

我正在解析一個JSon文件,它有5個足球隊,他們每個人都玩過4場比賽。
我創建了一個包裝類OutputClass來存儲每個團隊的所有細節,即玩過,贏得,畫畫和丟失的遊戲數量。
由於有5支隊伍,我創建了一個名單OutputClass。 所以在將值傳遞給onPostExecute後,我調用一個名爲FootballAdapter的適配器。 此適配器只執行一次。
即使我在outputClassList.get(int position)中將位置值更改爲2或3,我也會在Toast中得到相同的結果,如下圖所示。
我檢查了outputClassList是否爲空,但沒有。 對象是一個列表中,從傳遞:
android:ArrayAdapter在AsyncTask中只執行一次

FootballAdapter adapter= new FootballAdapter(getApplicationContext(),R.layout.row,result); 
     jsonList.setAdapter(adapter); 

上面的代碼是在AsyncTaskonPostExecute方法。

public class JSONTask extends AsyncTask<String,String,List<OutputClass>> { 

    @Override 
    protected List<OutputClass> doInBackground(String... params) { 
     HttpURLConnection connection=null; 
     BufferedReader r=null; 

     try { 
      URL url=new URL(params[0]); 

      connection=(HttpURLConnection)url.openConnection(); 
      connection.connect(); 
      InputStream stream= connection.getInputStream(); 
      r= new BufferedReader(new InputStreamReader((stream))); 
      StringBuffer stringBuffer=new StringBuffer(); 
      String line=""; 
      while((line=r.readLine())!=null){ 
       stringBuffer.append(line); 
      } 

      String finalJSON=stringBuffer.toString(); 


      /// read and parse JSON file for desired output..... 

      JSONObject parentObject= new JSONObject(finalJSON); 

      // gets the name of all the objects namels alpha, bravo charlie etc etc 
      JSONArray names=parentObject.names(); 

      // multiple objets for each team..... 
      List<OutputClass> outputClassList =new ArrayList<>(); 

      String games[]=new String[4]; 
      int finalScore[]=new int[names.length()]; 
      int gd[]=new int[names.length()]; 
      int won[]=new int[names.length()]; 
      int draw[]=new int[names.length()]; 
      int lost[]=new int[names.length()]; 



      JSONObject[] mainObject=new JSONObject[names.length()]; 
      for(int i=0;i<names.length();i++){ 

       String teamName=names.getString(i); 
       mainObject[i]=parentObject.getJSONObject(teamName); 

       //outputClass[i].name=teamName; 
       // Gets the game data for each game.... 

       games[0]=mainObject[i].getString("match_1"); 
       games[1]=mainObject[i].getString("match_2"); 
       games[2]=mainObject[i].getString("match_3"); 
       games[3]=mainObject[i].getString("match_4"); 

       // Calculates the final score and goal difference for each team. 

       for(int j=0;j<4;j++) { 
        String score[] = games[j].split("-"); 
        int score1 = Integer.parseInt(score[0]); 
        int score2 = Integer.parseInt(score[1]); 

        if (score1 > score2) { 
         finalScore[i] += 3; 
         won[i]++; 
        } 
        else if (score1 == score2) { 
         finalScore[i] += 1; 
         draw[i]++; 
        }else{ 
         lost[i]++; 
        } 
        gd[i]+=score1-score2; 

       } 



       // Calculating the position based on final score and Goal Difference... 

       OutputClass outputClass=new OutputClass(); 
       outputClass.goalDifference=gd[i]; 
       outputClass.draw=draw[i]; 
       outputClass.finalScore=finalScore[i]; 
       outputClass.lost=lost[i]; 
       outputClass.won=won[i]; 
       outputClass.played=4; 
       outputClass.name=teamName; 

       outputClassList.add(outputClass); 


      } 
      //Arrays.sort(outputClass,OutputClass.FinalScoreComparator); 
      //Arrays.sort(outputClass,new OutputClass().FinalScoreComparator); 
      Collections.sort(outputClassList,new OutputClass().FinalScoreComparator); 

      Log.i(TAG,"Final Scre: "+outputClassList.get(0).finalScore+" won : "+outputClassList.get(0).won); 
      Log.i(TAG,"Final Scre: "+outputClassList.get(1).finalScore+" won : "+outputClassList.get(1).won); 
      Log.i(TAG,"Final Scre: "+outputClassList.get(2).finalScore+" won : "+outputClassList.get(2).won); 
      Log.i(TAG,"Final Scre: "+outputClassList.get(3).finalScore+" won : "+outputClassList.get(3).won); 
      //return stringBuffer.toString(); 
      return outputClassList; 

     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     }catch (IOException e){ 
      e.printStackTrace(); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } finally { 
      if(connection != null) 
       connection.disconnect(); 
      try { 
       if(r!=null) 
        r.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 

     return null; 
    } 


    @Override 

    protected void onPostExecute(List<OutputClass> result){ 
     super.onPostExecute(result); 

     FootballAdapter adapter= new FootballAdapter(getApplicationContext(),R.layout.row,result); 
     jsonList.setAdapter(adapter); 

     //to remove the button and text view after clicking them... 
     imageButton.setVisibility(View.GONE); 
     jsonData.setVisibility(View.GONE); 
     //showing the list..... 
     setContentView(jsonList); 


    } 


} 

public class FootballAdapter extends ArrayAdapter<OutputClass>{ 

    private List<OutputClass> outputClassList; 
    private int resource; 
    private LayoutInflater inflater; 

    public FootballAdapter(Context context, int resource, List<OutputClass> objects) { 
     super(context, resource, objects); 
     outputClassList = objects; 
     this.resource=resource; 
     inflater =(LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); 
    } 

    @NonNull 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     if(convertView== null) 
      convertView=inflater.inflate(R.layout.row,null); 


     TextView rank; 
     TextView played; 
     TextView won; 
     TextView lost; 
     TextView draw; 
     TextView gd; 

     rank=(TextView)findViewById(R.id.rank); 
     played=(TextView)findViewById(R.id.played); 
     won=(TextView)findViewById(R.id.won); 
     lost=(TextView)findViewById(R.id.lost); 
     draw=(TextView)findViewById(R.id.draw); 
     gd=(TextView)findViewById(R.id.gd); 

     if(rank!=null) 
      played.setText("Played: " + outputClassList.get(position).played); 
     if(won!=null) 
      won.setText("Won: " + outputClassList.get(position).won); 
     if(lost!=null) 
      lost.setText("Lost: " + outputClassList.get(position).lost); 
     if(draw!=null) 
      draw.setText("Draw: " + outputClassList.get(position).draw); 
     if(gd!=null) 
      gd.setText("Goal Diff: " + outputClassList.get(position).goalDifference); 


     Toast toast=Toast.makeText(getApplicationContext(),"Played: "+outputClassList.get(4).played+"Won: "+outputClassList.get(4).won,Toast.LENGTH_LONG); 
     toast.show(); 
     return convertView; 
    } 
} 

這是我創建OT檢查outputClassList是空的或不是日誌文件:

I/enpFootball: Final Scre: 8 won : 2 
I/enpFootball: Final Scre: 7 won : 2 
I/enpFootball: Final Scre: 7 won : 2 
I/enpFootball: Final Scre: 5 won : 1 

甲剪斷的ListView可以在下面的圖像中可以看出:

enter image description here

回答

1

一些更多的信息,這個問題可能是在該行

rank=(TextView)findViewById(R.id.rank); 

ArrayAdapter沒有findViewById()(請參閱ArrayAdapter Reference

由於上述行正在工作,因此您可能已將您的FootballAdapter類定義爲上下文中的嵌套類(例如活動或片段),它使用findViewById()方法。在這種情況下,您將在整個上下文中搜索具有指定標識的「第一個」小部件。因此,您的適配器實際上正在執行列表中的所有項目,但每次調用它時,都會覆蓋第一個小部件(例如,第一個.id.rank和第一個rid.played等)

試試這個

rank= (TextView) convertView.findViewById(R.id.rank); 

這樣,你指的是列表項適配器是爲了填補,而不是在整個上下文中的第一個項目內的TextView。 (見View Reference

+0

感謝噸人@艾哈邁德,我找到了完全相同的答案,但是不知道爲什麼。現在我明白了。 – Subhash

0

嘗試打印postexecute()的結果,看看你在postupdate中得到了什麼,之後你會知道什麼是exa ctly的問題是,如果你無法理解,然後再張貼關於你的反應和適配器類