片段加载第一次出错的网络数据

时间:2016-07-08 08:13:38

标签: android android-fragments

当我第一次从片段的第一个标签中获取数据时,出现了问题。通过网页打开它,这些数据是正常的。

public class OnlineAllInsuranceFragment extends Fragment implements OnListViewListener {

private ScrollListView MainView;
private BasicAdapter2 Adapter;
private Integer Step=10;
private Integer Start=0;
private Integer End=Step;
private Handler handler,handler2;
private DataTable dTable;
public static String SelectCode="";
private static final int OVER = 1;
private String polnum;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_online_insurance,container,false);
    handler2 = new Handler();
    new Thread(new Runnable() {
        public void run() {
            Looper.prepare();
            handler2.post(runSetList);
            Looper.loop();
        }
    }).start();
        return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
            this.MainView=(ScrollListView)getView().findViewById(R.id.contractlistView1);
            this.MainView.setPullLoadEnable(true);
            this.MainView.setXListViewListener(this);
}

private void SetList(){
    Start=0;
    End=3;
    this.dTable = new DataTable(getActivity(),"newcontract");  
    this.dTable.Load("AgentCode="+OnlineAllInsuranceFragment.SelectCode+"&Start=0&End="+Step);
    if(dTable.GetRowCount()>0){
        this.MainView.setVisibility(0);
        this.StartData(this.dTable.GetList(),R.layout.userlistitem4);   

    }else{
        this.MainView.setVisibility(8);
        TextView NullView=(TextView) getView().findViewById(R.id.listViewNull1);
        NullView.setVisibility(0);
        NullView.setHeight(50);
    }
} 

private void StartData(ArrayList<Map<String, Object>> List, int Resource) {
    this.Adapter = new BasicAdapter2(getActivity(), List, Resource,
            new String[] { "Provider", "ProdName", "NoType", "ContractNo",
                    "AcceptTime", "ContractStatus", "Premium", "Gain",
                    "ReceivedPremium", "Name" }, 
            new int[] {R.id.contractitem_label_Provider,
                    R.id.contractitem_label_ProdName,
                    R.id.contract_label_cno,
                    R.id.contractitem_label_contractNo,
                    R.id.contractitem_label_acceptTime,
                    R.id.contractitem_label_contractStatus,
                    R.id.contractitem_label_premium,
                    R.id.contractitem_label_feilv,
                    R.id.contractitem_label_receivedPremium,
                    R.id.contractitem_label_Name }, 
            new int[] {R.id.listitem_button_look, R.id.listitem_button_pay,
        R.id.listitem_main1,R.id.contractitem_label_companyImage,R.id.contractitem_label_payImage },
            new BasicAdapter2.ListAdapterListener() {

                @Override
                public void onClickAtOKButton(View v) {
                    String company = dTable.GetValue((Integer)v.getTag(), 1).toString();//产品名
                    String status = dTable.GetValue((Integer)v.getTag(), 4).toString();//保单状态
                    switch (v.getId()) {
                    case R.id.listitem_button_pay:
                        polnum = dTable.GetValue((Integer)v.getTag(), 3).toString();
                        String numPol = dTable.GetValue((Integer)v.getTag(), 2).toString();//是“保单号”还是“投保单号”
                        if (numPol.contains("aa")) {
                            Intent intent = new Intent();
                            intent.setClass(getActivity(),PolicyInformationActivity.class);
                            intent.putExtra("polnum", polnum);
                            startActivity(intent);
                        } else if (numPol.contains("bb")) {
                            toastDialog();
                        }
                        break;
                    case R.id.listitem_button_look:
                        Toast.makeText(getActivity(),"...", Toast.LENGTH_SHORT).show();
                        LogUtil.e("company+status", company+"=="+status);
                        break;
                    case R.id.listitem_main1:
                        break;
                    }

                }
            },
            new BasicAdapter2.VisibleAdapterListener() {

                @Override
                public int onSetVisible(View v) {
                    String company = dTable.GetValue((Integer)v.getTag(), 1).toString();//产品名
                    String status = dTable.GetValue((Integer)v.getTag(), 4).toString();//保单状态
                    LogUtil.e("status","=="+status);
                    int drawId;
                    switch (v.getId()) {
                    case R.id.contractitem_label_companyImage:
                        if ( company.contains("cc") ) {
                            drawId = R.drawable.taikang2x;
                            return drawId;
                        } else if (company.contains("dd") ) {
                            drawId = R.drawable.youban2x;
                            return drawId;
                        } 
                        break;
                    case R.id.contractitem_label_payImage:
                        if ( status.equals("ee") ) {
                            drawId = R.drawable.security2x;
                            return drawId;
                        } else if (status.equals("ff")) {
                            drawId = R.drawable.nopay2x;
                            return drawId;
                        } else if ( status.equals("gg")) {
                            drawId = R.drawable.invalid2x;
                            return drawId;
                        } 
                        break;
                    case  R.id.listitem_button_pay:
                        if (company.contains("hh") && status.equals("ii")) {
                            return 0;

                        } else if (company.contains("jj") || status.equals("kk")) {
                            return 4;
                        }
                        break;

                    }
                    return 99;
                }
            });

    handler = new Handler();
    this.Adapter.Start(this.MainView);
    this.Start += Step;
    this.End += Step;
}

private void LoadData(){
    this.dTable.Load("AgentCode="+OnlineAllInsuranceFragment.SelectCode+"&Start="+Start+"&End="+End);
    this.Start += Step;
    this.End += Step;
}

private void stopRefresh() {
    this.MainView.stopRefresh();
    this.MainView.setRefreshTime("just");
}

private void stopLoadMore() {
    this.MainView.stopLoadMore();
}

@Override
public void onRefresh() {
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            SetList();
            stopRefresh();
        }
    }, 2000);
}

@Override
public void onLoadMore() {
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            LoadData();
            stopLoadMore();
        }
    }, 2000);
}


@SuppressLint("HandlerLeak")
Handler handler1 = new Handler() {
    public void handleMessage(Message msg) {
            switch (msg.what) {
            case OVER:
                Control.ExitDialog();
                    break;
            default:
                    break;
            }
            super.handleMessage(msg);
    }
};

Runnable runSetList = new Runnable() {

    @Override
    public void run() {
        SetList();

    }
};
 private void toastDialog() {
     CustomDialog.Builder builder = new CustomDialog.Builder(getActivity());
     builder.setTitle("xx");
     builder.setMessage("yy");
     builder.setNegativeButton("vv", new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialog, int which) { 
             dialog.dismiss();
         } 
    });
     builder.setPositiveButton("ww", new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialog, int which) { 
             dialog.dismiss();
             Control.StartDialog(getActivity());
            new Thread(new Runnable() {
                 public void run() {
                    Looper.prepare();
                    startActivity(new Intent(getActivity(),HomeActivity.class));
                    send();
                    handler1.sendEmptyMessage(1);
                     Looper.loop();
                 }
         }).start();
         } 
    });
     Dialog noticeDialog = builder.create();
        noticeDialog.setCancelable(false);
        noticeDialog.setCanceledOnTouchOutside(false);
        noticeDialog.show();
    }

    private void send() {
    }

}

当我远离这个片段然后回来时,数据也变得正常了。数据不正常就在我第一次进入片段时。

0 个答案:

没有答案