2017-03-16 75 views
0

我想填充一個ListView與數據,但由於某種原因,這個數據不能很好地與我的ListView。正在從我的服務器檢索數據,並使用JSONException在MainClass中輸出錯誤。錯誤是「字符串不能轉換爲JSONArray」。有任何想法嗎?數據返回不填充ListView

AdapterTeam類:

public class AdapterTeam extends RecyclerView.Adapter<RecyclerView.ViewHolder> { 

private Context context; 
private LayoutInflater inflater; 
List<DataTeam> data= Collections.emptyList(); 
DataTeam current; 
//int currentPos=0; 

// create constructor to innitilize context and data sent from MainActivity 
public AdapterTeam(Context context, List<DataTeam> data){ 
    this.context=context; 
    inflater= LayoutInflater.from(context); 
    this.data=data; 
} 

// Inflate the layout when viewholder created 
@Override 
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View view=inflater.inflate(R.layout.adapter_team, parent,false); 
    MyHolder holder=new MyHolder(view); 
    return holder; 
} 

// Bind data 
@Override 
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 

    // Get current position of item in recyclerview to bind data and assign values from list 
    MyHolder myHolder= (MyHolder) holder; 
    DataTeam current=data.get(position); 
    myHolder.textteamName.setText(current.teamName); 
    myHolder.textSport.setText("Sport: " + current.sport); 
    myHolder.textGender.setText("Gender: " + current.gender); 
    myHolder.textAge.setText("Age " + current.age); 



} 

// return total item from List 
@Override 
public int getItemCount() { 
    return data.size(); 
} 


class MyHolder extends RecyclerView.ViewHolder implements View.OnClickListener{ 

    TextView textteamName; 
    TextView textSport; 
    TextView textAge; 
    TextView textGender; 

    // create constructor to get widget reference 
    public MyHolder(View itemView) { 
     super(itemView); 
     textteamName= (TextView) itemView.findViewById(R.id.textteamName); 
     textSport = (TextView) itemView.findViewById(R.id.textSport); 
     textGender = (TextView) itemView.findViewById(R.id.textGender); 
     textAge = (TextView) itemView.findViewById(R.id.textAge); 
     itemView.setOnClickListener(this); 
    } 

    // Click event for all items 
    @Override 
    public void onClick(View v) { 

     Toast.makeText(context, "You clicked an item",  Toast.LENGTH_SHORT).show(); 

    } 

} 

} 

MainClass:(適配器)

  List<DataTeam> data=new ArrayList<>(); 

     pdLoading.dismiss(); 
     if(result.equals("no rows")) { 
      Toast.makeText(joinTeam.this, "No Results found for entered query", Toast.LENGTH_LONG).show(); 
     }else{ 

      try { 

       JSONArray jArray = new JSONArray(result); 

       // Extract data from json and store into ArrayList as class objects 
       for (int i = 0; i < jArray.length(); i++) { 
        JSONObject json_data = jArray.getJSONObject(i); 
        DataTeam teamData = new DataTeam(); 
        teamData.teamName = json_data.getString("team_name"); 
        teamData.sport = json_data.getString("sport"); 
        teamData.gender = json_data.getString("gender"); 
        teamData.age = json_data.getString("age"); 
        data.add(teamData); 
       } 

       // Setup and Handover data to recyclerview 
       mTeam = (RecyclerView) findViewById(R.id.fishPriceList); 
       mAdapter = new AdapterTeam(joinTeam.this, data); 
       mTeam.setAdapter(mAdapter); 
       mTeam.setLayoutManager(new LinearLayoutManager(joinTeam.this)); 

      } catch (JSONException e) { 
       // You to understand what actually error is and handle it appropriately 
       Toast.makeText(joinTeam.this, e.toString(), Toast.LENGTH_LONG).show(); 
       Toast.makeText(joinTeam.this, result.toString(), Toast.LENGTH_LONG).show(); 
      } 

     } 

    } 

} 
} 
+0

如果你看到json的敬酒,這意味着你抓住了一個'JSONException'。在例外情況下,您應該可以閱讀更多詳細信息。 – GVillani82

+0

@ GVillani82 - 你是對的 - Toast發生在JSONException中。錯誤是字符串不能轉換爲JsonArray - 所以我猜想它的返回是怎麼回事.. –

+0

你確定你的結果的根對象是一個數組嗎?我已經看到這個,當根JSON對象實際上是一個單一的對象持有一個數組。 – eimmer

回答

0

你必須調用notifyDataSetChanged();更新後的適配器列表數據。