2016-06-27 62 views
1

當Activity中的listview,按鈕有效時,只要我的類擴展Fragment,就沒有任何事情發生。在listiview按鈕的點擊監聽器不工作,它擴展了片段

public class OnlineNopaymentInsuranceFragment extends Fragment implements OnItemClickListener,OnItemSelectedListener,OnListViewListener,OnClickListener { 

    private ScrollListView MainView; 
    private BasicAdapter Adapter; 
    private Integer Step=3; 
    private Integer Start=0; 
    private Integer End=Step; 
    private Handler handler,handler2; 
    //private List<Map<String, Object>> Lists; 
    private DataTable Table; 

    public static String SelectCode=""; 

    private static final int OVER = 1; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
    } 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.fragment_online_insurance,container,false); 
    } 
    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
       this.MainView=(ScrollListView)getView().findViewById(R.id.contractlistView1); 

       this.MainView.setOnItemClickListener(this); 
       this.MainView.setOnItemSelectedListener(this); 
       this.MainView.setPullLoadEnable(true); 
       this.MainView.setXListViewListener(this); 


       handler2 = new Handler(); 

       Control.StartDialog(getActivity()); 
       new Thread(new Runnable() { 
        public void run() { 
         Looper.prepare(); 
         handler2.post(runSetList); 
         handler1.sendEmptyMessage(1); 
         Looper.loop(); 
        } 
       }).start(); 



       this.MainView.setOnItemClickListener(new OnItemClickListener() { 
         @Override 
         public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
           long arg3) { 

          Toast.makeText(getActivity(), "nothing", Toast.LENGTH_LONG).show(); 
         } 
        }); 

    } 



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

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


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


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


    @Override 
    public void onClick(View v) { 
     switch (v.getId()) { 
      case R.id.listitem_button_pay: 
       LogUtil.e("OnlineNopaymentInsuranceFragment", "nothing happen in here"); 
       Toast.makeText(getActivity(),"nothing happen in here", Toast.LENGTH_SHORT).show(); 

     break; 
      case R.id.listitem_main1: 
       Toast.makeText(getActivity(),"nothing happen in here,too", Toast.LENGTH_SHORT).show(); 
       break; 

     default: 
      break; 
     } 
    } 

    @Override 
    public void onItemSelected(AdapterView<?> parent, View view, int Index, long id) {} 

    @Override 
    public void onNothingSelected(AdapterView<?> parent) {} 

    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int Index, long id) {} 


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

    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(); 

     } 
    }; 
} 

適配器代碼:

public class BasicAdapter extends SimpleAdapter { 

    private Context context; 
    private ArrayList<Integer> Indexs; 

    public BasicAdapter(Context context,List<? extends Map<String,?>> data,int resource,String[] from,int[] to){ 
     super(context,data,resource,from,to); 
     this.context=context; 
     this.Indexs =new ArrayList<Integer>(); 
    } 

    public BasicAdapter(Context context,List<? extends Map<String,?>> data,int resource,String[] from,int[] to,int[] Ids){ 
     super(context,data,resource,from,to); 
     this.context=context; 
     this.Indexs =new ArrayList<Integer>(); 
     if (Ids != null) { 
      for (int i = 0; i < Ids.length; i++) { 
       this.Indexs.add(Ids[i]); 
      } 
     } 
    } 

    public void Add(Integer ViewId){this.Indexs.add(ViewId);} 

    @Override 
    public View getView(int position,View convertView,ViewGroup parent){ 
     LogUtil.e("===basicadapter", "getview"); 
     View view = super.getView(position, convertView, parent); 
     for(int i=0;i<this.Indexs.size();i++){ 
      View TempView=(View) view.findViewById(this.Indexs.get(i)); 
      TempView.setOnClickListener((OnClickListener) this.context); 
      TempView.setTag(position); 
     } 
     return view; 
    } 

    public void Start(ListView View){ 
     View.setAdapter(this); 
     this.setViewBinder(new ViewBinder() { 
      @Override 
      public boolean setViewValue(View view,Object data,String textRepresentation) {  
       if(view instanceof TextView && data instanceof HkBoolean){ 
        HkBoolean Data = (HkBoolean) data; 
        view.setVisibility(Data.GetVisibility()); 
        return true; 
       }else if(view instanceof ImageView && data instanceof Bitmap){  
        ImageView iv = (ImageView) view;  
        iv.setImageBitmap((Bitmap) data);  
        return true;  
       }else if(view instanceof ImageView && data instanceof Integer){  
        ImageView iv = (ImageView) view;  
        iv.setBackgroundResource((Integer) data);  
        return true;  
       }else { 
        return false; 
       } 
      }  
     }); 
    } 

} 

如果我把按鈕利斯特在BasicAdapter,它可以調用,too.But我想在Fragment.Any建議的button'click聽衆可以理解的。

+1

你應該片段和適配器http://stackoverflow.com/a/37535113/5381331 –

+2

謝謝,謝謝你們之間建立接口,非常感謝你!這對我非常有幫助! – Plantago

回答

0
this.Adapter = new BasicAdapter(getActivity(), List, Resource, 
      new String[] { "Provider", "ProdName", "NoType", "ContractNo", 
        "AcceptTime", "ContractStatus", "Premium", "Gain", 
        "ReceivedPremium", "Name" }, 

片段正在實現偵聽器而不是活動。所以,你應該做的

this.Adapter = new BasicAdapter(this, List, Resource, 
      new String[] { "Provider", "ProdName", "NoType", "ContractNo", 
        "AcceptTime", "ContractStatus", "Premium", "Gain", 
        "ReceivedPremium", "Name" }, 
+0

對不起,我不能在我的類中改變getActivity()來擴展Fragment.Anyway,謝謝你們。 – Plantago

+0

您必須傳遞實現偵聽器的對象。形成你的代碼,我可以看到OnlineNopaymentInsuranceFragment正在實現監聽器。所以通過它的實例。主機活動不是實施監聽者的活動。 – user3215142

相關問題