大家好, 我正在尝试创建一个应用,一次在每个问题的前面显示一些问题和EditText字段,以获取答案。我当前的代码在同一行中为我提供了所有问题,无论数组列表的大小如何,都可以重复,请查看附件图片,但是我希望每行中的每个问题都可以,请有人帮忙。
public class MainActivity extends AppCompatActivity {
private AdapterListView adapter;
private Button btnStart, btnSubmit;
private ListView questListView;
Questions ques;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnStart = (Button) findViewById(R.id.start);
btnStart.setVisibility(View.VISIBLE);
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setQuestions();
}
});
}//End of create()
public void setQuestions() {
ArrayList<Questions> qArrList = new ArrayList<>();
for (int i = 0; i < 5; i++) {
ques = new Questions();
ques.setQuestion("");
qArrList.add(ques);
}
adapter = new AdapterListView(getBaseContext(), R.layout.item_listview, qArrList);
questListView.setAdapter(adapter);
btnSubmit.setVisibility(View.VISIBLE);
}
}//End of Class
适配器
public class AdapterListView extends ArrayAdapter<Questions> {
Context mContext;
LayoutInflater inflater;
private ArrayList<Questions> questionsArrayList;
private Questions quesObject;
private ArrayList<String> quesList;
public AdapterListView(Context context, int resource, ArrayList<Questions> questionsArrayList) {
super(context, resource);
this.mContext = context;
this.setQuestionsArrayList(questionsArrayList);
this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
int flag = 0;
final ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.item_listview, null);
holder = new ViewHolder();
holder.questionTextView = (TextView) convertView.findViewById(R.id.question);
holder.editText = (EditText) convertView.findViewById(R.id.ans_edit_text);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
quesObject = getQuestionsArrayList().get(position);
quesList = new ArrayList<String>();
quesList.add("3 + 3");
quesList.add("4 + 3");
quesList.add("3 + 3");
quesList.add("6 + 3");
quesList.add("8 + 3");
holder.questionTextView.setText("Q " + position + ": \t" + quesList);
holder.editText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
getQuestionsArrayList().get(position).setQuestion(holder.editText.getText().toString());
}
});
return convertView;
}
@Override
public int getCount() {
return getQuestionsArrayList().size();
}
public ArrayList<Questions> getQuestionsArrayList() {
return questionsArrayList;
}
public void setQuestionsArrayList(ArrayList<Questions> questionsArrayList) {
this.questionsArrayList = questionsArrayList;
}
static class ViewHolder {
TextView questionTextView;
EditText editText;
}
}
对象
public class Questions {
String question;
public Questions() {
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
}
答案 0 :(得分:0)
在适配器的getView()中,您可以通过以下代码行在文本视图中设置整个问题列表-
holder.questionTextView.setText("Q " + position + ": \t" + quesList);
相反,您只需将一个问题显示为-
holder.questionTextView.setText("Q " + position + ": \t" + quesList.get(position));
谢谢。