2 sawdouma sawdouma 于 2013.06.09 12:03 提问

gridview无法更新啊,求教~~~~

package jx.jvtc.bestfoddServer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import jx.jvtc.model.CustomerBuyFood;
import jx.jvtc.model.FoodAndroid;
import jx.jvtc.webserviceimpl.Orderbuyimpl;

import android.R.integer;
import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.NavUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class SendMssion extends Activity {

private GridView gridListView;
private List<HashMap<String, Object>> data;
private String test = "多列显示Test";
List<CustomerBuyFood> lsInfo;
SimpleAdapter sa;

public void showFoodInfo(List<FoodAndroid> lsFoodMenu, final int id) {
    String[] str = new String[lsFoodMenu.size()];
    for (int i = 0; i < lsFoodMenu.size(); i++) {
        String value = lsFoodMenu.get(i).getMaterial();
        value += " × ";
        value += lsFoodMenu.get(i).getId() + "份";
        str[i] = value;
    }
    new AlertDialog.Builder(this).setTitle("需配送的菜").setItems(str, null)
            .setNegativeButton("正在派送", new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    Thread a = new Thread(new Runnable() {

                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            new Orderbuyimpl().updateorder(id, 0);
                            lsInfo = new Orderbuyimpl()
                                    .getordertime(LoginState.cellPhone);
                            data = new ArrayList<HashMap<String, Object>>();
                            for (int i = 0; i < lsInfo.size(); i++) {
                                HashMap<String, Object> map = new HashMap<String, Object>();
                                map.put("sendStateStr", lsInfo.get(i)
                                        .getOflag());
                                System.out
                                        .println(lsInfo.get(i).getOflag());
                                data.add(map);
                            }
                        }
                    });

                    a.start();
                    try {
                        a.join();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    gridListView = (GridView) findViewById(R.id.list_gridView);
                    sa = (SimpleAdapter) gridListView.getAdapter();
                    sa.notifyDataSetChanged();
                    // gridListView.setAdapter(sa);

                }
            })

            .setPositiveButton("已送达", new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    new Thread(new Runnable() {

                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            new Orderbuyimpl().updateorder(id, 1);
                            SendMssion.this.onResume();

                        }
                    }).start();
                }
            }).show();
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Thread a = new Thread(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            lsInfo = new Orderbuyimpl().getordertime(LoginState.cellPhone);
        }
    });
    a.start();
    try {
        a.join();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    if (lsInfo.size() == 0) {
        setContentView(R.layout.activity_send_mission_null);
    } else {
        setContentView(R.layout.activity_send_mssion);
        gridListView = (GridView) findViewById(R.id.list_gridView);
        fillData();
        sa = new SimpleAdapter(this, data,
                R.layout.activity_send_mission_item, new String[] {
                        "phone", "address", "homeSendTime", "sendStateStr",
                        "foodMenu", "id" }, new int[] { R.id.phone,
                        R.id.sendAddr, R.id.sendTime, R.id.sendState });
        gridListView.setAdapter(sa);
        gridListView
                .setOnItemClickListener(new AdapterView.OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> arg0, View arg1,
                            int arg2, long arg3) {
                        // TODO Auto-generated method stub
                        HashMap<String, Object> item = (HashMap<String, Object>) arg0
                                .getItemAtPosition(arg2);
                        // setTitle((String) item.get("goods_text"));
                        // String str = item.get("phone");
                        // Toast.makeText(SendMssion.this, "",
                        // Toast.LENGTH_SHORT).show();
                        List<FoodAndroid> lsFoodMenu;
                        lsFoodMenu = (List<FoodAndroid>) item
                                .get("foodMenu");
                        // System.out.println(lsFoodMenu.size()+"  ss");
                        int id = (Integer) item.get("id");
                        // System.out.println(id + "\tsendState");
                        showFoodInfo(lsFoodMenu, id);
                    }
                });
    }
}

public void fillData() {
    data = new ArrayList<HashMap<String, Object>>();
    for (int i = 0; i < lsInfo.size(); i++) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int id = lsInfo.get(i).getId();
        map.put("phone", lsInfo.get(i).getPhone());
        map.put("address", lsInfo.get(i).getAddress());
        map.put("homeSendTime", lsInfo.get(i).getStrdate());
        map.put("sendStateStr", lsInfo.get(i).getOflag());
        map.put("id", id);
        map.put("foodMenu", lsInfo.get(i).getFoodmenu());
        // System.out.println(String.valueOf(lsInfo.get(i).getId())
        // + " \tDDDDDD");
        data.add(map);
    }
}

}

1个回答

u010434246
u010434246   2017.02.13 16:21
已采纳

楼主的数据更新在子线程里面,子线程的特点是不会阻塞主线程的运行,所以你在子线程开启之后就立即刷新gridvieww ,子线程还没有执行或者没有执行完成,data数据就没有变化,所以不会刷新界面

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!