gpt4 book ai didi

android - 如何将 JSONArray 加载到 android 中 ListView 的不同行中?

转载 作者:太空狗 更新时间:2023-10-29 14:03:00 24 4
gpt4 key购买 nike

我想加载 JSONArray 并设置到 ListView 中。

这是我的 one_fragment,我将数据加载到 listiview 中

<LinearLayout 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:orientation="vertical"
tools:context="info.androidhive.materialtabs.fragments.ThreeFragmnet">

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#3C3C3C">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="35dp"
android:paddingLeft="5dp"
android:orientation="horizontal">
<TextView
android:id="@+id/txtlblTotal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:layout_gravity="center"
android:textSize="15dp"
android:text="IND-250/4(ovr-90)"/>
<View
android:layout_width="110dp"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/txtlblRuns"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:layout_gravity="center"
android:textSize="15dp"
android:text="R"/>

<TextView
android:id="@+id/txtlblBalls"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:layout_marginLeft="15dp"
android:layout_gravity="center"
android:textSize="15dp"
android:text="B"/>

<TextView
android:id="@+id/txtlblFours"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:layout_marginLeft="15dp"
android:layout_gravity="center"
android:textSize="18dp"
android:text="4s"/>

<TextView
android:id="@+id/txtSixs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:layout_marginLeft="15dp"
android:layout_gravity="center"
android:textSize="15dp"
android:text="6s"/>
</LinearLayout>
<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ListView>
</LinearLayout>
</LinearLayout>

这是我的 OneFragment,我加载了 json 数据并将其存储到适配器中

public class OneFragment extends Fragment {

View rootView;
JSONObject jsonObject;
List<Score> list=new ArrayList<Score>();
ListView lst;
FullScoreAdapter adapter=null;
private static final String url="http://10.0.2.2/JSON/fullLitz.php";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

rootView = inflater.inflate(R.layout.fragment_one, container, false);
new JSONLoader().execute();


return rootView;
}

public class JSONLoader extends AsyncTask<String, String, JSONObject> {

private ProgressDialog dialog;

@Override
protected void onPreExecute() {
super.onPreExecute();

dialog = new ProgressDialog(getActivity());
dialog.setMessage("Getting....");
dialog.setIndeterminate(false);
dialog.setCancelable(true);
dialog.show();
}

@Override
protected JSONObject doInBackground(String... params) {
JSONParser parser = new JSONParser();
jsonObject = parser.getJSONFromUrl(url);

return jsonObject;
}

@Override
protected void onPostExecute(JSONObject jsonObject) {
dialog.dismiss();

try {

JSONObject mainObject = new JSONObject(jsonObject.toString());
String status = mainObject.getString("status");
String version = mainObject.getString("version");
String status_code = mainObject.getString("status_code");
String Etag = mainObject.getString("Etag");
String cache_key = mainObject.getString("cache_key");

JSONObject dataObject = mainObject.getJSONObject("data");
JSONObject cardObject = dataObject.getJSONObject("card");

JSONObject innings = cardObject.getJSONObject("innings");

JSONObject b_1=innings.getJSONObject("b_1");

JSONArray batting_order=b_1.getJSONArray("batting_order");

int length=batting_order.length();
ArrayList<String> p_name=new ArrayList<String>();

Score s=new Score();

//for(int j=0;j<length;j++) {
for (int i = 0; i < batting_order.length(); i++) {
p_name.add(batting_order.getString(i));
}
//p_name.add(batting_order.getString(j));
//}
s.setPlayernames(p_name);
list.add(s);

lst=(ListView)rootView.findViewById(R.id.listview);
adapter=new FullScoreAdapter(getContext(),list,length);

lst.setAdapter(adapter);
}
catch (JSONException e){
e.printStackTrace();
}
}
}
}

这里是我的 list_rows.xml 来创建自定义 ListView

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#3C3C3C">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="35dp"
android:paddingLeft="5dp"
android:orientation="horizontal">
<TextView
android:id="@+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:layout_gravity="center"
android:textSize="10dp"
android:text="IND-250/4(ovr-90)"/>
<View
android:layout_width="110dp"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/txtRuns"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:layout_gravity="center"
android:textSize="10dp"
android:text="R"/>

<TextView
android:id="@+id/txtBalls"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:layout_marginLeft="15dp"
android:layout_gravity="center"
android:textSize="10dp"
android:text="B"/>

<TextView
android:id="@+id/txtFours"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:layout_marginLeft="15dp"
android:layout_gravity="center"
android:textSize="10dp"
android:text="4s"/>

<TextView
android:id="@+id/txtSixs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:layout_marginLeft="15dp"
android:layout_gravity="center"
android:textSize="15dp"
android:text="6s"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>

这是我的 FullScoreAdapter.java,它膨胀 list_rows.xml 并设置到 OneFragment.java

public class FullScoreAdapter extends BaseAdapter {

private static final int TYPES_COUNT = 2;
private static final int TYPE_LEFT = 0;
private static final int TYPE_RIGHT = 1;

private Context context;
private List<Score> items,lst;
private int length;
private static LayoutInflater inflater;
public FullScoreAdapter(Context context,List<Score> items,int length){
this.context=context;
this.items=items;
this.length=length;
inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return items.size();
}

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

@Override
public long getItemId(int position) {
return position;
}
public class Holder {
TextView txtname;
TextView txtballs;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
Holder holder=new Holder();
final View view;

view = inflater.inflate(R.layout.list_rows, null);

holder.txtname=(TextView)view.findViewById(R.id.txtName);
holder.txtballs=(TextView)view.findViewById(R.id.txtBalls);

Score score=items.get(position);

String batting="";
for(String str:score.getPlayernames()){
//batting+=str;
Log.e("BESTMAN NAME", str);
holder.txtname.setText(str);
}
return view;
}

}

这是我的 score.java 类,它有 setter 和 getter 方法。

package com.domore.tabfinaldemo;

import java.util.List;

public class Score {

private List<String> playernames;
private String target;

public List<String> getPlayernames() {
return playernames;
}

public void setPlayernames(List<String> playernames) {
this.playernames = playernames;
}

public String getTarget() {
return target;
}

public void setTarget(String target) {
this.target = target;
}
}

这是我要加载的json数据

    {
"status": true,
"version": "2.0.3",
"status_code": 200,
"expires": "1427298827.0",
"Etag": "1427287936.0",
"cache_key": "match|iplt20_2013_g30|full_card",
"data": {
"card_type": "full_card",
"card": {
"related_name": "30th Match",
"inactive_balls": [],
"msgs": {
"info": "CSK won by 5 wickets.",
"completed": "CSK won by 5 wickets.",
"others": []
},
"batting_order": [
[
"b",
"1"
],
[
"a",
"1"
]
],
"toss": {
"decision": "bat",
"won": "b",
"str": "Rajasthan Royals won the toss and chose to bat first"
},
"winner_team": "a",
"long_description": "CSK won by 5 wickets. Chennai Super Kings scored 186/5 in 19.5 and Rajasthan Royals scored 185/4 in 20.0. Michael Hussey played good and he is man of the match. - Chennai Super Kings vs Rajasthan Royals (International Cricket Match) - 30th Match - IPL 2013 - IPL T20 2013. On 22 April 2013 in MA Chidambaram Stadium, Chepauk, Chennai.",
"innings": {
"b_1": {
"batting_order": [
"s_watson",
"a_rahane",
"d_yagnik",
"r_dravid",
"s_binny",
"b_hodge"
],
"runs": 185,
"balls": 120,
"fall_of_wickets": [
"AM Rahane at 71 runs, in 7.2 over",
"DH Yagnik at 84 runs, in 9.3 over",
"RS Dravid at 113 runs, in 13.2 over",
"SR Watson at 159 runs, in 17.3 over"
],
"wide": 0,
"run_rate": "9.25",
"fours": 12,
"run_str": "185/4 in 20.0",
"wickets": 4,
"wicket_order": [
"a_rahane",
"d_yagnik",
"r_dravid",
"s_watson"
],
"extras": 10,
"bowling_order": [
"a_chandila",
"r_shukla",
"j_faulkner",
"k_cooper",
"s_trivedi",
"s_watson",
"s_binny"
],
"key": "b_1",
"noball": 0,
"sixes": 7,
"legbye": 0,
"bye": 0,
"overs": "20.0",
"dotballs": 30,
"partnerships": [
{
"player_a_six": 3,
"player_a_four": 5,
"first_ball": "bfee9a98-76a9-4959-b1cb-638989c96bff",
"overs_balls": "7.1",
"four": 6,
"index": 1,
"six": 3,
"dismissed": false,
"player_b_four": 1,
"start_over": "0.1",
"end_over": "7.1",
"innings": "1",
"player_b_runs": 16,
"player_b_six": 0,
"player_b_balls": 14,
"runs": 71,
"balls": 43,
"player_b": "a_rahane",
"player_a": "s_watson",
"player_a_balls": 29,
"last_ball": "4825aad2-25d8-434c-86bb-11f8eedbdc4f",
"player_a_runs": 54,
"match": "iplt20_2013_g30",
"team": "b",
"run_rate": "9.91"
},
{
"player_a_six": 0,
"player_a_four": 0,
"first_ball": "c7351d98-9012-4361-ba2a-f5cd8665587d",
"overs_balls": "2.1",
"four": 1,
"index": 2,
"six": 0,
"dismissed": true,
"player_b_four": 0,
"start_over": "7.2",
"end_over": "9.2",
"innings": "1",
"player_b_runs": 4,
"player_b_six": 0,
"player_b_balls": 5,
"runs": 13,
"balls": 13,
"player_b": "s_watson",
"player_a": "a_rahane",
"player_a_balls": 1,
"last_ball": "c4134225-b057-4a43-9e0f-cd227b377540",
"player_a_runs": 0,
"match": "iplt20_2013_g30",
"team": "b",
"run_rate": "6.00"
},
{
"player_a_six": 0,
"player_a_four": 0,
"first_ball": "17d6fc7f-d37e-45ee-9b0a-aaf15221d8c4",
"overs_balls": "3.5",
"four": 1,
"index": 3,
"six": 1,
"dismissed": true,
"player_b_four": 1,
"start_over": "9.3",
"end_over": "13.1",
"innings": "1",
"player_b_runs": 21,
"player_b_six": 1,
"player_b_balls": 14,
"runs": 29,
"balls": 23,
"player_b": "s_watson",
"player_a": "d_yagnik",
"player_a_balls": 1,
"last_ball": "cf9137bb-9fd7-49c1-9fdc-3e5c8492ee3b",
"player_a_runs": 0,
"match": "iplt20_2013_g30",
"team": "b",
"run_rate": "7.57"
},
{
"player_a_six": 0,
"player_a_four": 0,
"first_ball": "0ea39992-7647-4916-bfcc-b2beba9dbf00",
"overs_balls": "4.1",
"four": 2,
"index": 4,
"six": 2,
"dismissed": true,
"player_b_four": 0,
"start_over": "13.2",
"end_over": "17.2",
"innings": "1",
"player_b_runs": 22,
"player_b_six": 2,
"player_b_balls": 12,
"runs": 46,
"balls": 25,
"player_b": "s_watson",
"player_a": "r_dravid",
"player_a_balls": 1,
"last_ball": "2eaaa026-485f-493c-9a70-551abc2bb83c",
"player_a_runs": 0,
"match": "iplt20_2013_g30",
"team": "b",
"run_rate": "11.04"
},
{
"player_a_six": 0,
"player_a_four": 0,
"first_ball": "4720b7ff-e963-4bc8-9138-6124c2b261fc",
"overs_balls": "2.4",
"four": 2,
"index": 5,
"six": 1,
"dismissed": true,
"player_b_four": 1,
"start_over": "17.3",
"end_over": "19.6",
"innings": "1",
"player_b_runs": 16,
"player_b_six": 1,
"player_b_balls": 9,
"runs": 26,
"balls": 16,
"player_b": "s_binny",
"player_a": "s_watson",
"player_a_balls": 1,
"last_ball": "76481b09-e7e4-4097-9e88-b6884f069082",
"player_a_runs": 0,
"match": "iplt20_2013_g30",
"team": "b",
"run_rate": "9.75"
}
]
}
}
}
}
}

上面的 json 文件不完整,我只发布了我需要解决这个问题。

我想得到局内的batting_order->b_1->batting order

我已经完成获取 batting_order 数据,但它仅在 ListView 的单行上设置。

如何将单个名称设置为 ListView 的单行??例如。第一行是s_watson,第二行是a_rahane等等

最佳答案

如果您使用此适配器以外的 Score 模型类,我会建议您不要更改模型类,但如果您只想显示击球顺序列表,请在 BaseAdapter 中进行更改。如果您只想显示击球顺序列表,这里我已从列表更改为列表

public class FullScoreAdapter extends BaseAdapter {

private static final int TYPES_COUNT = 2;
private static final int TYPE_LEFT = 0;
private static final int TYPE_RIGHT = 1;

private Context context;
private List<String> items, lst;
private int length;
int size = 0;
private static LayoutInflater inflater;

public FullScoreAdapter(Context context, List<String> items, int length) {
this.context = context;
this.items = items;
if (items != null && !items.isEmpty()) {
size = items.length();
}
this.length = length;

}

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

@Override
public String getItem(int position) {
return items.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

public class Holder {
TextView txtname;
TextView txtballs;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
Holder holder = null;
final View view;
if (convertView == null) {
holder = new Holder();
convertView =LayoutInflater.from(this.context).inflate(R.layout.list_rows, parent,false);
holder.txtname = (TextView) convertView.findViewById(R.id.txtName);
holder.txtballs = (TextView) convertView.findViewById(R.id.txtBalls);
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
//check answer for baseadapter
//http://stackoverflow.com/a/34349311/1140237
}


//Score score = items.get(position);

String batting = "";
holder.txtname.setText(this.items.get(position));
return convertView;
}

}

onPostExecute 的变化

protected void onPostExecute(JSONObject jsonObject) {
dialog.dismiss();

try {

JSONObject mainObject = new JSONObject(jsonObject.toString());
String status = mainObject.getString("status");
String version = mainObject.getString("version");
String status_code = mainObject.getString("status_code");
String Etag = mainObject.getString("Etag");
String cache_key = mainObject.getString("cache_key");

JSONObject dataObject = mainObject.getJSONObject("data");
JSONObject cardObject = dataObject.getJSONObject("card");

JSONObject innings = cardObject.getJSONObject("innings");

JSONObject b_1=innings.getJSONObject("b_1");

JSONArray batting_order=b_1.getJSONArray("batting_order");

int length=batting_order.length();
ArrayList<String> p_name=new ArrayList<String>();

Score s=new Score();

//for(int j=0;j<length;j++) {
for (int i = 0; i < batting_order.length(); i++) {
p_name.add(batting_order.getString(i));
}
//p_name.add(batting_order.getString(j));
//}
s.setPlayernames(p_name);
list.add(s);

lst=(ListView)rootView.findViewById(R.id.listview);
adapter=new FullScoreAdapter(getContext(),p_name,length);

lst.setAdapter(adapter);
}
catch (JSONException e){
e.printStackTrace();
}
}

关于android - 如何将 JSONArray 加载到 android 中 ListView 的不同行中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34368882/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com