简体   繁体   中英

Displaying JSON data in a ListView only displays the first element in the Array

I am doing a simple project in which I can firstly get JSON data from my database and then display it in a listview in my andorid application. I have the first part working fine as I can see that all the JSON data is there, however only the first element in the array is being displayed in my listview. Was hoping anyone could help as I am unsure as to where I cam going wrong. Thanks

PickPlayerActivity.java

public class PickPlayerActivity extends AppCompatActivity {


String json_string;
JSONObject jsonObject;
JSONArray jsonArray;
PlayerAdapter playerAdapter;
ListView listview;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pick_player);
    listview = (ListView) findViewById(R.id.listview);
    playerAdapter = new PlayerAdapter(this, R.layout.row_layout);
    listview.setAdapter(playerAdapter);
    json_string = getIntent().getExtras().getString("json_data");

    try {
        jsonObject = new JSONObject(json_string);
        jsonArray = jsonObject.getJSONArray("server_response");
        int count = 0;
        String firstname, surname, position;

        while(count<jsonObject.length()){

            JSONObject jo = jsonArray.getJSONObject(count);

            firstname = jo.getString("firstname");
            surname = jo.getString("surname");
            position = jo.getString("position");


            Players players = new Players(firstname, surname, position);

            playerAdapter.add(players);
            count ++;


        }


    } catch (JSONException e) {
        e.printStackTrace();
    }


}
}

activity_pick_player.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="testproject.PickPlayerActivity">


<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/listview"

    />

row_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="75dp">


<TextView

    android:layout_height="match_parent"
    android:layout_width="120dp"
    android:id="@+id/tx_firstname"
    android:layout_alignParentLeft="true"
    android:text="firstname"
    android:gravity="center"
    android:textAppearance="?android:textAppearanceLarge">

</TextView>

<TextView

    android:layout_height="match_parent"
    android:layout_width="120dp"
    android:id="@+id/tx_lastname"
    android:layout_toRightOf="@+id/tx_firstname"
    android:text="surname"
    android:gravity="center"
    android:textAppearance="?android:textAppearanceLarge">

</TextView>

<TextView

    android:layout_height="match_parent"
    android:layout_width="120dp"
    android:id="@+id/tx_position"
    android:layout_toRightOf="@+id/tx_lastname"
    android:text="position"
    android:gravity="center"
    android:textAppearance="?android:textAppearanceLarge">

</TextView>

players.java

public class Players {

private String position, firstname, lastname;

public Players(String firstname, String lastname, String position){

    this.setFirstname(firstname);
    this.setLastname(lastname);
    this.setPosition(position);

}

public String getFirstname(){
    return firstname;
}

public void setFirstname(String firstname)
{
    this.firstname = firstname;
}


public String getLastname() {
    return lastname;
}

public void setLastname(String lastname) {
    this.lastname = lastname;
}

public String getPosition() {
    return position;
}

public void setPosition(String position) {
    this.position = position;
}
}

PlayerAdapter.java

public class PlayerAdapter extends ArrayAdapter {

List list = new ArrayList();

public PlayerAdapter(Context context, int resource){
    super(context, resource);

}


public void add(Players object) {
    super.add(object);
    list.add(object);
}

@Override
public int getCount() {
    return list.size();
}

@Override
public Object getItem(int position) {
    return list.get(position);
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row;
    row = convertView;
    PlayerHolder playerHolder;
    if (row==null){
        LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = layoutInflater.inflate(R.layout.row_layout,parent,false);
        playerHolder = new PlayerHolder();
        playerHolder.tx_firstname = (TextView) row.findViewById(R.id.tx_firstname);
        playerHolder.tx_lastname = (TextView) row.findViewById(R.id.tx_lastname);
        playerHolder.tx_position = (TextView) row.findViewById(R.id.tx_position);
        row.setTag(playerHolder);

    }
    else{
        playerHolder = (PlayerHolder) row.getTag();


    }
    Players players = (Players) this.getItem(position);
    playerHolder.tx_firstname.setText(players.getFirstname());
    playerHolder.tx_lastname.setText(players.getLastname());
    playerHolder.tx_position.setText(players.getPosition());

    return row;
}


class PlayerHolder{

    TextView tx_firstname, tx_lastname, tx_position;

}

}

Try changing this line from

while(count<jsonObject.length())

to

while(count<jsonArray.length())

Since you are looping over the length of jsonObject , if the object contains a single array , its length will be 1 . It looks like you actually want to loop over the length of that array itself and not the length of the object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM