在自定義ListView中,我想將所有數據發送到某處。但我無法獲取相應的行數據。
這裏是我的代碼..在Android中從自定義ListView獲取數據
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class Question {
int current = NONE; // hold the answer picked by the user, initial is NONE(see below)
public static final int NONE = 1000; // No answer selected
public static final int ANSWER_YES_SELECTED = 0; // Yes answer selected
public static final int ANSWER_NO_SELECTED = 1; // Yes answer selected
public int getCurrent() {
return current;
}
public void setCurrent(int current) {
this.current = current;
}
private String question;
private RadioButton radioYes;
private RadioButton radioNo;
private EditText editResponse;
RadioGroup radio_group;
private Question(){
super();
}
public Question(String title,RadioButton r1,RadioButton r2, EditText e1,RadioGroup radio_group) {
super();
this.radio_group=radio_group;
this.question = title;
this.radioYes=r1;
this.radioNo=r2;
this.editResponse=e1;
}
public Question(String title,RadioButton r1,RadioButton r2, EditText e1) {
super();
this.question = title;
this.radioYes=r1;
this.radioNo=r2;
this.editResponse=e1;
}
public Question (String q){
super();
this.question=q;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
public RadioButton getRadioYes() {
return radioYes;
}
public void setRadioYes(RadioButton radioYes) {
this.radioYes = radioYes;
}
public RadioButton getRadioNo() {
return radioNo;
}
public void setRadioNo(RadioButton radioNo) {
this.radioNo = radioNo;
}
public EditText getEditResponse() {
return editResponse;
}
public void setEditResponse(EditText editResponse) {
this.editResponse = editResponse;
}
public RadioGroup getRadio_group() {
return radio_group;
}
public void setRadio_group(RadioGroup radio_group) {
this.radio_group = radio_group;
}
@Override
public String toString() {
return "Question [question=" + question + ", radioYes=" + radioYes
+ ", radioNo=" + radioNo + ", editResponse=" + editResponse.getText()
+ ", radio_group=" + radio_group + "]";
}
}
我AdapterClass
public class QuestionAdapter extends ArrayAdapter<Question>{
Context context;
int layoutResourceId;
Question[] data = null;
final String[] text;
int[] chkId=null;
public QuestionAdapter(Context context, int layoutResourceId, Question[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
text=new String[data.length];
chkId=new int[data.length];
for (String s: text) {
s="";
}
}
@SuppressLint("NewApi")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final HashMap<Integer, Integer> checkeddMap= new HashMap<Integer, Integer>();
View row = convertView;
final QuestionHolder holder;
final int pos=position;
// System.out.println("Position is "+position);
if(row == null)
{
holder = new QuestionHolder();
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder.txtquestion = (TextView)row.findViewById(R.id.txtquestion);
holder.radioYes = (RadioButton)row.findViewById(R.id.radioYes);
holder.radioNo= (RadioButton)row.findViewById(R.id.radioNo);
holder.editResponse=(EditText)row.findViewById(R.id.editResponse);
holder.radio_group=(RadioGroup)row.findViewById(R.id.radio_group);
//holder.editResponse.setText(data[position].getEditResponse().toString());
row.setTag(holder);
}
else
{
holder = (QuestionHolder)row.getTag();
}
Question question = data[position];
holder.txtquestion.setText(question.getQuestion());
holder.editResponse.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
text[pos] =((EditText)v).getText().toString();
}
});
holder.editResponse.setText(text[pos]);
// System.out.println(position+"Position");
holder.radio_group.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
}
});
holder.radio_group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup arg0, int checkedId) {
// TODO Auto-generated method stub
System.out.println("In OnCheckedChaned Listener.");
arg0.setTag(pos);
Question element=data[pos];
// System.out.println("Tag of radio Group= "+(Integer)arg0.getTag());
switch (checkedId) {
case R.id.radioYes:
element.current=Question.ANSWER_YES_SELECTED;
checkeddMap.put(pos, data[pos].current);
break;
case R.id.radioNo:
element.current=Question.ANSWER_NO_SELECTED;
checkeddMap.put(pos, data[pos].current);
break;
case -1:
element.current=Question.NONE;
}
}
});
if (data[pos].current != Question.NONE) {
RadioButton r = (RadioButton) holder.radio_group.getChildAt(data[position].current);
r.setChecked(true);
} else {
holder.radio_group.clearCheck(); // This is required because although the Question could have the current
// position to NONE you could be dealing with a previous row where
// the user already picked an answer.
}
return row;
}
static class QuestionHolder
{
TextView txtquestion;
RadioButton radioYes;
RadioButton radioNo;
EditText editResponse;
RadioGroup radio_group;
}
}
而且我MainActivity.java
public class MainActivity extends Activity {
Question myQue;
private ListView listView1;
private Button btn;
private String[] str={"First Question","Second Question","Third Question","Fourth Question","Fifth Question","SIxth Question","Seventh Q","Eightth Q","Nine th Q", "Tenth Q" };
ArrayList< Question> arrayList;
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a_securitylist);
RadioButton r1 = null;
RadioButton r2 = null;
RadioGroup radio_group = null;
EditText e1 = null;
Button btn=(Button) findViewById(R.id.submit);
EditText e2;
RadioGroup rg= new RadioGroup(getApplicationContext());
rg.setOrientation(RadioGroup.HORIZONTAL);
RadioButton rYes=new RadioButton(getApplicationContext());
RadioButton rNo=new RadioButton(getApplicationContext());
rYes.setHeight(10);
rYes.setWidth(10);
EditText etext= new EditText(getApplicationContext());
etext.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 10));
RadioGroup rg1= new RadioGroup(getApplicationContext());
int p=0;
final Question weather_data[] = new Question[]
{
new Question((p+1)+"."+str[0], r1, r2, etext, radio_group),
new Question((p+2)+"."+str[01], r1, r2, etext, radio_group),
new Question((p+3)+"."+str[02], r1, r2, etext, radio_group),
new Question((p+4)+"."+str[03], r1, r2, etext, radio_group),
new Question((p+5)+"."+str[04], r1, r2, etext, radio_group),
new Question((p+6)+"."+str[05], r1, r2, etext, radio_group),
new Question((p+6)+"."+str[06], r1, r2, etext, radio_group),
new Question((p+7)+"."+str[07], r1, r2, etext, radio_group),
new Question((p+8)+"."+str[8], r1, r2, etext, radio_group),
new Question((p+9)+"."+str[9], r1, r2, etext, radio_group)
};
QuestionAdapter adapter = new QuestionAdapter(MainActivity.this, R.layout.listview_item_row, weather_data);
listView1 = (ListView)findViewById(R.id.securityListView);
listView1.setClickable(false);
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);
//adapter.notifyDataSetChanged();
System.out.println(listView1.getParent().toString());
btnSubmit.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
System.out.println("In Footer OnClick");
Toast.makeText(MainActivity.this,"Footer Toast", Toast.LENGTH_LONG).show();
System.out.println(listView1.getChildCount()); // Child count is 4. but my list view has 10 items.
System.out.println(listView1.getChildAt(2).findViewById(R.id.radio_group));
// Working
// System.out.println("Cheked Radio button at 0"+((RadioGroup)listView1.getChildAt(0).findViewById(R.id.radio_group)).getCheckedRadioButtonId());
// System.out.println("Cheked Radio button at 01"+((RadioGroup)listView1.getChildAt(1).findViewById(R.id.radio_group)).getCheckedRadioButtonId());
// System.out.println("Cheked Radio button at 02"+((RadioGroup)listView1.getChildAt(2).findViewById(R.id.radio_group)).getCheckedRadioButtonId());
// Not Working
// System.out.println("Cheked Radio button at 03"+((RadioGroup)listView1.getChildAt(3).findViewById(R.id.radio_group)).getCheckedRadioButtonId());
// System.out.println("Cheked Radio button at 04"+((RadioGroup)listView1.getChildAt(4).findViewById(R.id.radio_group)).getCheckedRadioButtonId());
// System.out.println("Cheked Radio button at 05"+((RadioGroup)listView1.getChildAt(5).findViewById(R.id.radio_group)).getCheckedRadioButtonId());
// System.out.println("Cheked Radio button at 06"+((RadioGroup)listView1.getChildAt(6).findViewById(R.id.radio_group)).getCheckedRadioButtonId());
// System.out.println("Cheked Radio button at 07"+((RadioGroup)listView1.getChildAt(7).findViewById(R.id.radio_group)).getCheckedRadioButtonId());
//System.out.println(((Question)(listView1.getItemAtPosition(0))).radio_group.toString());
}
});
}
}
我想所有的數據將得到onclicklister btnSumbit的...
全部數據表示什麼? – Riser
你想得到什麼數據?您的列表視圖數據已存儲在陣列中。 –
我想獲取checkRadiobutton ID和編輯文本中的文本。但無法找到方法。 –