gpt4 book ai didi

android - 如何从 Api 获取数据到 ListView ?

转载 作者:太空狗 更新时间:2023-10-29 15:11:36 26 4
gpt4 key购买 nike

当我试图从另一个 Api 类调用主要 Activity 的“刷新”方法时,该方法被调用并且它还显示一些 fatal error 。它没有改变适配器值。任何人都可以给出任何想法清除那个。?

package com.example.hotspot;

import com.example.hotspot.HotspotApi;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ListView;
import android.widget.TextView;

public class HotSpot extends Activity {
TextView textview;
ListView listview;
HotspotAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.hot_spot);
textview = (TextView) findViewById(R.id.textView1);
listview = (ListView) findViewById(R.id.listView1);
adapter = new HotspotAdapter(this);
listview.setAdapter(adapter);
new HotspotApi(adapter).execute();

}

public void refresh() {

System.out.println("refresh() is called");
adapter.notifyDataSetChanged();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.hot_spot, menu);
return true;
}

}

热点.java

package com.example.hotspot;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.hotspot.HotspotModel;
import com.example.hotspot.HotspotAdapter;

import android.os.AsyncTask;

public class HotspotApi extends AsyncTask<Void, Integer, Void> implements
Icommon {

public Boolean IsServerErr = false;
private JSONArray response_array;
String url = "some url";
HotspotAdapter adapter;
HotSpot hot;

public HotspotApi(HotspotAdapter adapter) {

this.adapter = adapter;
}

@Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
getresult();
return null;
}

@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub

hot=new HotSpot();
hot.refresh();
super.onPostExecute(result);

}

void getresult() {

InternetManager manager = new InternetManager(url);
String category_jsonresponse = manager.URLRequest();
if (!manager.IsServerConn) {
IsServerErr = true;
}
if (category_jsonresponse != null) {
System.out.println("Hotspot_jsonresponse" + category_jsonresponse);
try {
response_array = new JSONArray(category_jsonresponse);
for (int i = 1; i < response_array.length(); i++) {
JSONObject image_object = response_array.getJSONObject(i);
HotspotModel h = new HotspotModel();
h.setId(image_object.getString("id") == null ? ""
: image_object.getString("id"));
h.setContent(image_object.getString("content") == null ? ""
: image_object.getString("content"));
h.setImg(image_object.getString("img") == null ? ""
: image_object.getString("img"));
h.setName(image_object.getString("name") == null ? ""
: image_object.getString("name"));
arraylist.add(h);


}
System.out.println("HotspotModelsize() is " + arraylist.size());

} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

}

热点适配器.java

package com.example.hotspot;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class HotspotAdapter extends BaseAdapter implements Icommon{
private TextView textview;
private View view;
ImageView imageview;
private LayoutInflater inflater;

public HotspotAdapter(Context context ){
inflater = LayoutInflater.from(context);

}

@Override
public int getCount() {
// TODO Auto-generated method stub
return arraylist.size();
}

@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arraylist.get(arg0);
}

@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}

@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {

if (arg1 == null) {
view = inflater.inflate(R.layout.custom_layout, null);
} else {
view = arg1;
}

textview = (TextView) view.findViewById(R.id.txt_content);
textview.setText(arraylist.get(arg0).getName());




return view;
}

}

最佳答案

在您的 HotSpotApi 类中,您正在创建一个新的 HotSpot Activity ,这似乎是错误的。我猜您是从互联网获取 json 数据并将其加载到 listview 中。

解决方法:

在 HotspotApi 中更改以下而不是调用 Activity 方法:

@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
adapter.notfiyDatasetChanged();
}

希望对您有所帮助。

关于android - 如何从 Api 获取数据到 ListView ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16055240/

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