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

标签 android android-activity

当我试图从另一个 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/

相关文章:

java - 如果我使用 API 版本不支持的样式,会发生什么情况

android - 使用空对象引用的 Transformations.map 和 MediatorLiveData 崩溃应用程序

java - 如何使用 osmdroid 的 OSM MAP Tile Packager

java - 无法关闭 Android Activity

android - 获取选定复选框android的文本?

java - 我需要采取哪些步骤才能将我的 Eclipse Android 项目转换为 Gradle 项目?

android - 如何在 android 中的两个 Activity 之间应用 3d 转换?

android - 无法加载新的空白 Activity - Android 教程

java - 无法同时将两个字符串从一个 Activity 传递到另一 Activity

android-activity - 哪个 Activity 应该首先出现?定义为启动器?