2017-03-16 87 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();更新后的适配器列表数据。