返回的数据未填充ListView

时间:2017-03-16 19:32:28

标签: android

我正在尝试使用数据填充ListView,但由于某种原因,这些数据与我的ListView不能很好地匹配。从我的服务器检索数据并使用JSONException在MainClass中输出错误。错误是“字符串无法转换为JSONArray”。有什么想法吗?

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

        }

    }

}
}

1 个答案:

答案 0 :(得分:0)

更新适配器列表数据后,您必须调用PostgreSQL