2017-01-23 30 views
1

我創建動態列表視圖以編程方式,點擊特定的視圖我打開日期dialog.and設置它的日期值在特定的視圖,現在我有保存按鈕,點擊保存按鈕我想列表行中每個視圖的值。on button click從每個視圖獲取數據

這是我的代碼:

private void createlist(ArrayList<publicationcheckeddetail> pb) { 
     try { 
      listContainer.removeAllViews(); 
      final LayoutInflater inflater = (LayoutInflater)  getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      LinearLayout mainlayout = new LinearLayout(MainActivity.this); 
      LinearLayout.LayoutParams mainlayoutparameter = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); 
      mainlayout.setLayoutParams(mainlayoutparameter); 
      mainlayout.setOrientation(LinearLayout.VERTICAL); 
      for (int i = 0; i < pb.size(); i++) { 
       final ViewGroup viewgroup = (ViewGroup) inflater.inflate(R.layout.row_view, null); 
       final LinearLayout sublayout = new LinearLayout(MainActivity.this); 
       LinearLayout.LayoutParams sublayoutparameter = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
       sublayout.setLayoutParams(sublayoutparameter); 
       sublayout.setOrientation(LinearLayout.HORIZONTAL); 
       sublayout.setGravity(Gravity.CENTER); 
       viewHolder = new ListViewHolder(); 
       viewHolder.addImage = (ImageView) viewgroup.findViewById(R.id.addImage); 
       viewHolder.addchildimage = (ImageView) viewgroup.findViewById(R.id.childimage); 
       viewHolder.date = (TextView) viewgroup.findViewById(R.id.txtDate); 
       viewHolder.color = (TextView) viewgroup.findViewById(R.id.childdata); 
       Bitmap bitmap = BitmapFactory.decodeByteArray(pb.get(i).getPub_image(), 0, pb.get(i).getPub_image().length); 
       viewHolder.addImage.setImageBitmap(bitmap); 
       viewHoldersCollection.add(viewHolder); 
       sublayout.addView(viewgroup); 
       mainlayout.addView(sublayout); 
       viewHolder.date.setOnClickListener(new View.OnClickListener() { 
        @Override 

        public void onClick(View v) { 
         int index = ((ViewGroup) sublayout.getParent()).indexOfChild(sublayout); 
        v1 = ((ViewGroup) sublayout.getParent()).getChildAt(index).findViewById(R.id.txtDate); 
         openDateDialogtoSelectDate(v1); 

        } 
       }); 
       viewHolder.color.setOnClickListener(new View.OnClickListener() { 
        @Override 

        public void onClick(View v) { 
         int index = ((ViewGroup) sublayout.getParent()).indexOfChild(sublayout); 
         colorview = ((ViewGroup) sublayout.getParent()).getChildAt(index).findViewById(R.id.childdata); 
         strcolor = selectcolor(colorview); 
        } 
       }); 

       } 

      listContainer.addView(mainlayout); 
      listContainer.requestLayout(); 


     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 




private void openDateDialogtoSelectDate(View v1) { 
     try { 
      Calendar c = Calendar.getInstance(); 
      dialog = new DatePickerDialog(this, datePickerListener, year, month, day); 
      dialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000); 
      dialog.show(); 

     } catch (Exception e) { 
      e.printStackTrace(); 

     } 
    } 

    public DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() { 
     public void onDateSet(DatePicker view, int selectedYear, 
           int selectedMonth, int selectedDay) { 
      year = selectedYear; 
      month = selectedMonth; 
      day = selectedDay; 
      tv = (TextView) v1.findViewById(R.id.txtDate); 

      tv.setText(new StringBuilder().append(day).append("-") 
        .append((month + 1)).append("-").append(year) 
        .append(" ")); 
} 
} 
+0

不知道你需要什麼 – firegloves

+0

我需要保存按鈕從每個列表視圖row.Understood單擊每個視圖值。 – Rims

回答

1

簡單,遍歷的LinearLayout(mainlayout)

 int childCount=mainlayout.getChildCount(); 

    for(int i=0;i<childCount;i++) 
    { 
      View v=mainlayout.getChildAt(i); 
      TextView txtDate=(TextView)v.findViewById(R.id.txtDate); //get the date value 
      String strDate= txtDate.getText().toString(); 


      // do other operations 
    } 
0

你可能想嘗試使用CustomListView。這些都不是很難使用,他們有一個很好的方法,你可以重寫稱爲getView()。

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    LayoutInflater inflater = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    final View rowView = inflater.inflate(R.layout.settingsrowlayout, parent, false); 

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

    //What you want to happen when the button in the generated ListView using a CustomListView to do. 
} 

我希望這是有道理的,自定義列表視圖是非常有益的,一個很好的工具來使用