gpt4 book ai didi

Android JSON 解析 - 从 MySQL 数据库读取数据,解析并在 ListView 中显示结果

转载 作者:行者123 更新时间:2023-11-30 22:20:08 25 4
gpt4 key购买 nike

我正在关注 youtube 上关于 Android JSON 解析 的教程。我想从 MySQL db 中读取数据并解析为 JSON 格式 并将它们显示在我的 Android 应用程序的 ListView 中。

我创建了一个 PHP解码 JSON 数据,它显示在我的 Android 应用程序中。但是,当我尝试在 ListView 中显示它们时,应用程序显示空白屏幕

这是我的 Main Activiy 类。

public class MainActivity extends Activity {
String json_string;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void getJSON(View view){
new AsyncTaskRunner().execute();
}

class AsyncTaskRunner extends AsyncTask<Void, Void, String> {

String JSON_STRING;
String json_url;
TextView textView;


@Override
protected void onPostExecute(String result) {
TextView textView= (TextView)findViewById(R.id.textView);
textView.setText(result);
json_string=result;

}

@Override
protected String doInBackground(Void... voids) {
try {
URL url = new URL(json_url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();

InputStream inputStream=httpURLConnection.getInputStream();
BufferedReader bufferedReader =new BufferedReader(new InputStreamReader(inputStream));

StringBuilder stringBuilder =new StringBuilder();

while((JSON_STRING=bufferedReader.readLine())!=null){
stringBuilder.append(JSON_STRING+"\n");
}

bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();

return stringBuilder.toString().trim();

}catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;

}

@Override
protected void onPreExecute() {

json_url="http://10.0.2.2/ahala_new/json_get_data.php";
}


@Override
protected void onProgressUpdate(Void... values) {

super.onProgressUpdate(values);
}
}

public void parseJSON(View view)
{
if(json_string==null){
Toast.makeText(getApplicationContext(),"First get JSON Data",Toast.LENGTH_LONG).show();
}
else{
Intent intent=new Intent(this,ListView_Display.class);
intent.putExtra("json_data",json_string);
startActivity(intent);
}
}}

显示 ListView 类

public class ListView_Display extends Activity {
String json_string;
JSONObject jsonObject;
JSONArray jsonArray;
PromoAdapter promoAdapter;
ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display_listview_layout);

listView= (ListView) findViewById(R.id.listView);
listView.setAdapter(promoAdapter);

promoAdapter =new PromoAdapter(this,R.layout.row_layout);
json_string =getIntent().getExtras().getString("json_data");

try {
jsonObject = new JSONObject(json_string);
jsonArray=jsonObject.getJSONArray("server_response");
int count=0;
String name,description,price;
while (count<jsonObject.length()){

JSONObject SA=jsonArray.getJSONObject(count);
name=SA.getString("name");
description=SA.getString("description");
price=SA.getString("price");

Promotions promotions=new Promotions(name,description,price);
promoAdapter.add(promotions);
count++;

}

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

}}

自定义 ListView 适配器类

public class PromoAdapter extends ArrayAdapter{
List list=new ArrayList();
public PromoAdapter(Context context, int resource) {
super(context, resource);
}


public void add(Promotions 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;
PromotionHolder promotionHolder;
if(row==null){
LayoutInflater layoutInflater= (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row=layoutInflater.inflate(R.layout.row_layout,parent,false);

promotionHolder = new PromotionHolder();
promotionHolder.txName=(TextView)row.findViewById(R.id.txName);
promotionHolder.txDesc=(TextView)row.findViewById(R.id.txDesc);
promotionHolder.txPrice=(TextView)row.findViewById(R.id.txPrice);

row.setTag(promotionHolder);

}

else{
promotionHolder= (PromotionHolder) row.getTag();
}
Promotions promotions=(Promotions)this.getItem(position);
promotionHolder.txName.setText(promotions.getName());
promotionHolder.txDesc.setText(promotions.getDescription());
promotionHolder.txPrice.setText(promotions.getPrice());

return row;
}

static class PromotionHolder{
TextView txName,txDesc,txPrice;

}

}

请帮我解决这个问题。

最佳答案

试试这个

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getJSON();
}
public void getJSON(){
new AsyncTaskRunner().execute();
}

关于Android JSON 解析 - 从 MySQL 数据库读取数据,解析并在 ListView 中显示结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36885021/

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