-2

我使用 arrayAdapter 创建了一个列表视图。它包含

  1. 一个文本视图

  2. 两个单选按钮(即一个单选组)

  3. 一个编辑文本

此外,添加了具有提交按钮的页脚。TextView 正在动态设置。单选按钮有 yes 和 no 值。

问题是,在单击提交按钮时,我想从 listview 获取所有值将其发送到某处,但获取单选按钮和 edittext 的空值。

这是我的代码

    final Question weather_data[] = new Question[]
            {
            new Question(str[0], r1, r2, etext, radio_group),
            new Question(str[01], r1, r2, etext, radio_group),
            new Question(str[02], r1, r2, etext, radio_group),
            new Question(str[03], r1, r2, etext, radio_group),
            new Question(str[04], r1, r2, etext, radio_group),
            };

    QuestionAdapter adapter = new QuestionAdapter(this, 
            R.layout.listview_item_row, weather_data);


    listView1 = (ListView)findViewById(R.id.securityListView);

    View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null);
    listView1.addHeaderView(header);
    View footer = (View)getLayoutInflater().inflate(R.layout.listview_footer_row, null);
    listView1.addFooterView(footer);
    Button btnSubmit=(Button)(footer.findViewById(R.id.submit));
    listView1.setAdapter(adapter);
    Question w1=    (Question) listView1.getItemAtPosition(2);

    System.out.println(w1);
    System.out.println(listView1.getParent().toString());
    System.out.println("Child Count is "+listView1.getChildCount());


    btnSubmit.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "Footer Toast", Toast.LENGTH_LONG).show();
            System.out.println("In Footer OnClick");
            Toast.makeText(MainActivity.this,"Footer Toast", Toast.LENGTH_LONG).show();

            myQue=(Question) listView1.getItemAtPosition(3);
            System.out.println(((Question) listView1.getItemAtPosition(3)).editResponse.getText()+" In Edit Text");
            System.out.println(((Question) listView1.getItemAtPosition(3)).radioYes.isChecked());
            // I want these 2 lines to print the answer (values entered by user). But showing null or values set by me at runtime.

        }
    });

}

}

4

2 回答 2

0

for that you have to save all this info with some parallel data structure and when these value changes like when radio button change and text view text changes you have to update your values,send these values when click on submit button.

于 2013-05-14T09:27:05.407 回答
0

所以,有几种方法可以解决这个问题。

尝试从 listView 获取项目,他们会找到文本和布尔值:

        public void footerButtonClick(){
            for(int i=0;i<listView.getChildCount;i++){
                String text=((EditText)listView.getChildAt(i).findViewById(R.id.your_edit_text_id)).
                getText().toString();
               //and the same with your radioGroups.

            }

        }

或者您可以尝试在 MainActivity 类中创建布尔值和字符串值的静态数组,并通知 EditTexts 的 textWatchers 和 radioGroups 的 valueChangers。当文本或布尔值发生变化时,在静态数组中更改它们。然后你可以在按下按钮时使用这个数组

于 2013-05-14T09:24:53.300 回答