gpt4 book ai didi

java - 通过单击按钮和 JSON 增加 TextView

转载 作者:行者123 更新时间:2023-12-01 09:03:20 25 4
gpt4 key购买 nike

好的,所以我想增加一些 TextView,它显示来自 JSON 代码的数据。

package com.example.kane.bibliokane;

import android.content.DialogInterface;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONArray;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.io.IOException;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.net.HttpURLConnection;

import static android.R.attr.data;


public class ListesLivres extends AppCompatActivity {

TextView Titre, Auteur, Categorie , Editeur , Prix;
int score = 0;
Button b1,b2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listeslivres);

Titre = (TextView) findViewById(R.id.textView16);
Auteur = (TextView) findViewById(R.id.textView17);
Categorie = (TextView) findViewById(R.id.textView18);
Editeur = (TextView) findViewById(R.id.textView19);
Prix = (TextView) findViewById(R.id.textView20);
b1 = (Button)findViewById(R.id.button4);
b2 = (Button)findViewById(R.id.button3);

new getData().execute();
}

class getData extends AsyncTask<String, String, String> {

HttpURLConnection urlConnection;

@Override
protected String doInBackground(String... args) {

StringBuilder result = new StringBuilder();

try {
URL url = new URL("http://192.168.43.13:8000/api/liste.json");
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());

BufferedReader reader = new BufferedReader(new InputStreamReader(in));

String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}

} catch (Exception e) {
e.printStackTrace();
} finally {
urlConnection.disconnect();
}


return result.toString();
}

@Override
protected void onPostExecute(String result) {
try {
JSONArray jsonArray = new JSONArray(result);
JSONObject jsonObject = jsonArray.getJSONObject(0);
String t = jsonObject.getString("titre");
String a = jsonObject.getString("auteur");
String c = jsonObject.getString("categorie");
String e = jsonObject.getString("editeur");
String p = jsonObject.getString("prix");
Titre.setText(t);
Auteur.setText(a);
Categorie.setText(c);
Editeur.setText(e);
Prix.setText(p);

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

}

}
}

我在本主题中看到了如何通过按钮增加 TextView :Increment TextView with button click

但我不知道如何适应我的代码,因为我希望程序首先显示来自 jsonObject (0) 的数据,然后如果我单击按钮 (b1) ,它会增加++ ,并且在另一个按钮 (b2) 中,它递减 - 直到达到 0 。

谢谢

最佳答案

1) 您需要将 jsonArray 变量设置为全局变量。并定义一个变量 int currentIndex 来跟踪显示的当前索引2)为按钮附加一个onclick监听器来修改索引;

b1.setOnClickListener(new View.onClickListner(){
@Override
public void onClick(View v){
currentIndex++;
updateTextViews();
}
});

b2 将有一个类似的递减监听器

3) 定义一个使用 currentIndexjsonArray 的方法,并根据它设置 TextViews 的值

public void updateTextViews(){
//Add checks for current index to ensure it lies within range
try{
JSONObject object = jsonArray.getJSONObject(currentIndex);
//Set content for textviews
}catch(JSONException e){

}
}

关于java - 通过单击按钮和 JSON 增加 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41496006/

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