实现"刷新按钮"用于解析JSONArray

时间:2014-06-25 01:45:32

标签: android android-json

我目前有一个程序可以解析JSONArray并将每个项目放入listview。但是,如果您向JSONArray添加内容,则在您再次运行应用程序之前,listview不会更新。那么当新项目添加到JSONArray时,我将如何更新listview。这是我解析JSONArray的代码:

public class jarray extends Activity {

ArrayList<Actors> actorsList;

ActorAdapter adapter;
Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_jarray);
    actorsList = new ArrayList<Actors>();
    new JSONAsyncTask().execute("http://bisonsoftware.us/hhs/messages.json");

    ListView listview = (ListView) findViewById(R.id.list);
    adapter = new ActorAdapter(getApplicationContext(), R.layout.row, actorsList);

    listview.setAdapter(adapter);

    button = (Button)findViewById(R.id.button);

}



class JSONAsyncTask extends AsyncTask<String, Void, Boolean> {

    ProgressDialog dialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog = new ProgressDialog(jarray.this);
        dialog.setMessage("Loading, please wait");
        dialog.setTitle("Connecting server");
        dialog.show();
        dialog.setCancelable(false);
    }

    @Override
    protected Boolean doInBackground(String... urls) {
        try {

            //------------------>>
            HttpGet httppost = new HttpGet(urls[0]);
            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse response = httpclient.execute(httppost);

            // StatusLine stat = response.getStatusLine();
            int status = response.getStatusLine().getStatusCode();

            if (status == 200) {
                HttpEntity entity = response.getEntity();
                String data = EntityUtils.toString(entity);


                JSONObject jsono = new JSONObject(data);
                JSONArray jarray = jsono.getJSONArray("messages");


                for (int i = 0; i < jarray.length(); i++) {


                    Actors actor = new Actors();

                    String message = jarray.getString(i);
                    actor.setName(message);


                    //actorsList.add(actor);
                    adapter.add(actor);


                }
                return true;
            }

            //------------------>>

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

            e.printStackTrace();
        }
        return false;
    }

    protected void onPostExecute(Boolean result) {
        dialog.cancel();
        adapter.notifyDataSetChanged();
        if (result == false)
            Toast.makeText(getApplicationContext(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();

    }
}

}

如果您需要更多我的代码或只需要更多解释,请随时提出。真的可以使用一些帮助,谢谢。

这是ActorAdapter代码:

public class ActorAdapter extends ArrayAdapter<Actors> {
ArrayList<Actors> actorList;
LayoutInflater vi;
int Resource;
ViewHolder holder;

@Override
public void add(Actors object) {
    super.add(object);
    notifyDataSetChanged();
}

public ActorAdapter(Context context, int resource, ArrayList<Actors> objects) {
    super(context, resource, objects);
    vi = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    Resource = resource;
    actorList = objects;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // convert view = design
    View v = convertView;
    if (v == null) {
        holder = new ViewHolder();
        v = vi.inflate(Resource, null);
        holder.tvName = (TextView) v.findViewById(R.id.tvName);

        v.setTag(holder);
    } else {
        holder = (ViewHolder) v.getTag();
    }

    holder.tvName.setText(actorList.get(position).getName());

    return v;

}

static class ViewHolder {
    public TextView tvName;

}

}

2 个答案:

答案 0 :(得分:0)

不要直接添加到ArrayList,而是尝试从适配器添加它。

在运行ActorAdapter

之前,您需要立即启动AsyncTask

<强>样品:

setContentView(R.layout.activity_jarray);
actorsList = new ArrayList<Actors>();

adapter = new ActorAdapter(getApplicationContext(), R.layout.row, actorsList);
ListView listview = (ListView) findViewById(R.id.list);
listview.setAdapter(adapter);

new JSONAsyncTask().execute("http://bisonsoftware.us/hhs/messages.json");

button = (Button)findViewById(R.id.button);

向适配器添加项时,请调用适配器的add方法而不是arrayList

改变这一点:

actorsList.add(actor);

adapter.add(actor);

ActorAdapter适配器中添加

    @Override
public void add(Actors object) {
    super.add(object);
    notifyDataSetChanged();
}

改变你的想法:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_jarray);
    actorsList = new ArrayList<Actors>();

    adapter = new ActorAdapter(getApplicationContext(), R.layout.row, actorsList);
    ListView listview = (ListView) findViewById(R.id.list);
    listview.setAdapter(adapter);

    new JSONAsyncTask().execute("http://bisonsoftware.us/hhs/messages.json");

    button = (Button)findViewById(R.id.button);

}

答案 1 :(得分:-1)

更新列表视图的方法是使用更新的信息重新生成适配器,设置listadapter,然后调用NotifyDataSetChanged()。这告诉设备刷新布局。

ListView listview = (ListView) findViewById(R.id.list);
    adapter = new ActorAdapter(getApplicationContext(), R.layout.row, actorsList);

    listview.setAdapter(adapter);
adapter.NotifyDataSetChanged();