如何从Api获取数据到listview?

时间:2013-04-17 08:31:29

标签: android android-activity

当我试图从另一个Api类调用主要活动的方法“刷新”时,该方法被调用并且它也显示了一些致命的错误。并且它没有改变适配器值。任何人都可以给出任何想法清楚。?

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;
    }

}

hotspot.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();
            }

        }
    }

}

HotspotAdapter.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;
    }

}

2 个答案:

答案 0 :(得分:0)

hot = new HotSpot(); ??你不能那样用! HotSpot是一个活动,应该由Framework调用,例如,activitymanager。或者使用startActivity()来显示活动。

刷新方法(adapter.notifyDataSetChanged();)将导致刷新UI。但是,hot = new HotSpot()不会调用onCreated()方法,这意味着不会创建UI。所以它肯定会导致致命的错误。

我从未见过有人用新操作员调用Activity。

您应该参考有关如何使用活动和适配器的常见过程。

答案 1 :(得分:0)

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

解决方案:

在HotspotApi中更改以下内容而不是调用activity方法:

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

希望这会对你有所帮助。