android - 在按钮上单击从每个 View 获取数据

标签 android

我正在以编程方式创建动态 ListView ,单击特定 View 我打开日期对话框。并在特定 View 上设置它的日期值,现在我有保存按钮,单击保存按钮我想要列表行中每个 View 的值。

这是我的代码:

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

最佳答案

简单的,遍历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
     }

关于android - 在按钮上单击从每个 View 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41806096/

相关文章:

Android 应用程序不是从 Eclipse 启动的

android - Kotlin 扩展函数 - 如何使其全局化?

android - 在EditText末尾添加单位符号

Android:GPS 时间戳与 SensorEvent 时间戳

javascript - 如何在 React Native 中向动态创建的 ListView 按钮添加功能?

android - 确定用户是否选择 Facebook 作为社交分享选项

java - 更改工具栏概述的文本颜色

Android 从最近的应用程序列表中恢复应用程序

android - logcat 中的 Volley 错误消息 "null"

android - 图片+语音的视频编码器(手机支持)