2016-03-06 112 views
0

我想用自定義列表視圖創建警報對話框。爲此,我爲列表視圖創建了自定義佈局和適配器。 我想單選項目的警報對話框。所以我在對話框中設置了一個適配器。現在我想從數組列表中選擇項目?帶自定義列表視圖的警報對話框

selectTable.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       final TimeTable table = new TimeTable(); 

       List<TimeTable> allTables = new ArrayList<TimeTable>(); 

       TimeTableHelper timeTableHelper = new TimeTableHelper(AddEventActivity.this); 

       allTables = timeTableHelper.getAllTables(); 



       allTables.add(table); 

       final List<TimeTable> t = th.getAllTables(); 
       for (TimeTable cn : allTables) { 
        String log = "Id: " + cn.getId() + " ,Title: " + cn.getTitle() + 
          "Status: " + cn.getStatus() + ",Color: " + cn.getTableColor(); 

        Log.d("Data ", log); 
       } 
       final AlertDialog.Builder alertDialog = new AlertDialog.Builder(AddEventActivity.this); 
       LayoutInflater inflater = getLayoutInflater(); 
       View convertView = (View) inflater.inflate(R.layout.tablelist, null, false); 

       lv = (ListView) convertView.findViewById(R.id.tableslist); 

       final CustomAlertAdapter adapter = new CustomAlertAdapter(allTables, AddEventActivity.this); 


       alertDialog.setView(convertView); 

            alertDialog.setView(customView); 

       alertDialog.setSingleChoiceItems(adapter, selected, new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 


         final List<TimeTable> tables = new ArrayList<TimeTable>(); 

         // what to do here to get item of TimeTable list? 
        } 
       }); 

       alertDialog.show(); 
      } 
     }); 

編輯:

  alertDialog.setSingleChoiceItems(adapter, selected, new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog,int which) { 


        TimeTable selectedItem = allTables.get(which); 

        selectedItem.getId(); 
        txtTable.setText(selectedItem.getTitle()); 
        Log.d("TableId", String.valueOf(selectedItem)); 

        dialog.dismiss(); 

       } 
      }); 

      alertDialog.show(); 
     } 

customAlertAdapter

public class CustomAlertAdapter extends BaseAdapter{ 


     List<TimeTable> mData; 
     Context mContext; 
     LayoutInflater inflater; 
     public CustomAlertAdapter(List<TimeTable> data, Context context) { 
      mData = data; 
      mContext = context; 
      inflater = LayoutInflater.from(context); 
     } 
     @Override 
     public int getCount() { 
      return mData.size(); 
     } 

     @Override 
     public Object getItem(int arg0) { 
      return null; 
     } 

     @Override 
     public long getItemId(int arg0) { 
      // TODO Auto-generated method stub 
      return 0; 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 

      if (convertView == null) { 

       LayoutInflater mInflater = (LayoutInflater) mContext 
         .getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 

       convertView = mInflater.inflate(R.layout.alertlistrow, null); 

      } 

      TextView tvTitle = (TextView) convertView.findViewById(R.id.tableTitle); 
      Button color = (Button)convertView.findViewById(R.id.selectColor); 


      tvTitle.setText(mData.get(position).getTitle()); 

      ((GradientDrawable)color.getBackground()).setColor(mData.get(position).getTableColor()); 

      return convertView; 
     } 

} 

回答

2

which該參數表示選定的項目的索引。所以,你可以寫:

TimeTable selectedItem = allTables.get(which); 

此外,我認爲當你調用setSingleChoiceItem方法,沒有必要爲對話提供自定義視圖。

這裏是字符串數組工作示例:

AlertDialog dialog = new AlertDialog.Builder(getActivity()).setTitle("Dialog").setSingleChoiceItems(new String[]{"Item 1", "Item 2"}, 0, new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        switch (which) { 
         case 0: 
          //Item 1 was selected 
          break; 
         case 1: 
          //Item 2 was selected 
          break; 
        } 
        dialog.dismiss(); 
       } 
      }).create(); 
dialog.show(); 

可以考績ListAdapter,而不是字符串數組。

+0

項目沒有被點擊。如果我點擊物品,沒有任何反應。 – Sid

+0

請檢查編輯。 @Misagh Emamverdi – Sid

+0

@Sid嘗試從警報對話框中刪除自定義視圖。 –

相關問題