我正在做一个简单的项目,我可以先从我的数据库中获取JSON数据,然后在我的andorid应用程序的listview中显示它。我有第一部分工作正常,因为我可以看到所有的JSON数据都在那里,但是我的listview中只显示了数组中的第一个元素。希望有人可以提供帮助,因为我不确定我的错误在哪里。感谢
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;
}
}
答案 0 :(得分:1)
尝试从
更改此行 while(count<jsonObject.length())
到
while(count<jsonArray.length())
由于您在jsonObject
的长度上进行循环,如果对象包含单个array
,则其length
将为1
。看起来你实际上想要遍历array
本身的长度而不是对象的长度。