2017-02-14 101 views
1

我正在做一个简单的项目,我可以先从我的数据库获取JSON数据,然后在我的和应用程序的列表视图中显示它。我有第一部分工作正常,因为我可以看到所有JSON数据都在那里,但只有数组中的第一个元素显示在我的列表视图中。希望任何人都可以提供帮助,因为我不确定哪里会出错。由于在ListView中显示JSON数据只显示数组中的第一个元素

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; 

} 

} 

回答

1

尝试从

while(count<jsonObject.length())

改变这一行

while(count<jsonArray.length())

由于要遍历的jsonObject长度,如果对象包含单个array,其length1。它看起来像你实际上想循环的长度,而不是对象的长度。

+0

完美!非常感谢! – Dsk