-1
有時列表視圖有時會重複前5個值,有時會有10個值,但代碼不會顯示任何錯誤或警告。而另一個問題是,如果我選擇一個單選按鈕它改變用相同的ID(但不同的位置)單選按鈕全部Android ListView以無序方式顯示列表項
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import org.json.JSONObject;
import java.util.ArrayList;
/**
* Created by Anu Martin on 3/1/2016.
*/
public class QuestionAdapter extends BaseAdapter{
ArrayList<JSONObject> arrayList;
LayoutInflater inflater;
Context context;
public static final String KEY_QUESTION_TYPE="type"
,KEY_QUESTION="Q"
,KEY_QESTION_OPTION_YES="OPTYES"
,KEY_QUESTION_OPTION_NO="OPTNO"
,KEY_QUESTION_OPTION1="OPT1"
,KEY_QUESTION_OPTION2="OPT2"
,KEY_QUESTION_OPTION3="OPT3"
,KEY_QUESTION_OPTION4="OPT4"
,KEY_QUESTION_EXPLANATION="EXPL"
,KEY_ARRAY_QUESTION="A";
public static final int QUESTION_TYPE_YESORNO=15
,QUESTION_TYPE_MULTIPLE_CHOICE=20
,QUESTION_TYPE_SHORT_ANSWER=25;
public QuestionAdapter(Context context,ArrayList<JSONObject> arrayList){
this.arrayList=arrayList;
this.context=context;
inflater=LayoutInflater.from(this.context);
}
@Override
public int getCount() {
return arrayList.size();
}
@Override
public Object getItem(int position) {
return arrayList.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.e("getView",position+"");
try {
JSONObject jsonObject = this.arrayList.get(position);
Log.d("getView",jsonObject.toString());
int questionType=jsonObject.getInt(KEY_QUESTION_TYPE);
switch (questionType){
case QUESTION_TYPE_YESORNO:{
ViewHolderYesOrNo mViewHolder;
if(convertView==null){
convertView=this.inflater.inflate(R.layout.row_question_yesorno,null);
mViewHolder=new ViewHolderYesOrNo(convertView);
convertView.setTag(mViewHolder);
}else{
mViewHolder=(ViewHolderYesOrNo)convertView.getTag();
}
mViewHolder.question.setText(jsonObject.getString(KEY_QUESTION));
mViewHolder.type.setText(jsonObject.getInt(KEY_QUESTION_TYPE)+"");
if(jsonObject.getString(KEY_QUESTION_EXPLANATION)!=null) {
mViewHolder.explanation.setText(jsonObject.getString(KEY_QUESTION_EXPLANATION));
mViewHolder.explanation.setVisibility(View.VISIBLE);
}
}break;
case QUESTION_TYPE_MULTIPLE_CHOICE:{
final ViewHolderMultipleChoice mViewHolder;
if(convertView==null){
convertView=this.inflater.inflate(R.layout.row_question_multiple_choice,null);
mViewHolder=new ViewHolderMultipleChoice(convertView);
convertView.setTag(mViewHolder);
}else{
mViewHolder=(ViewHolderMultipleChoice)convertView.getTag();
}
mViewHolder.question.setText(jsonObject.getString(KEY_QUESTION));
mViewHolder.type.setText(jsonObject.getInt(KEY_QUESTION_TYPE)+"");
mViewHolder.explanation.setText("");
mViewHolder.option1.setText(jsonObject.getString(KEY_QUESTION_OPTION1));
mViewHolder.option2.setText(jsonObject.getString(KEY_QUESTION_OPTION2));
mViewHolder.option3.setText(jsonObject.getString(KEY_QUESTION_OPTION3));
mViewHolder.option4.setText(jsonObject.getString(KEY_QUESTION_OPTION4));
if(jsonObject.getString(KEY_QUESTION_EXPLANATION)!=null) {
mViewHolder.explanation.setText(jsonObject.getString(KEY_QUESTION_EXPLANATION));
mViewHolder.explanation.setVisibility(View.VISIBLE);
}
}break;
case QUESTION_TYPE_SHORT_ANSWER:{
ViewHolderShortAnswer mViewHolder;
if(convertView==null){
convertView=this.inflater.inflate(R.layout.row_question_short_answer,null);
mViewHolder=new ViewHolderShortAnswer(convertView);
convertView.setTag(mViewHolder);
}else{
mViewHolder=(ViewHolderShortAnswer)convertView.getTag();
}
mViewHolder.question.setText(jsonObject.getString(KEY_QUESTION));
mViewHolder.type.setText(jsonObject.getInt(KEY_QUESTION_TYPE)+"");
if(jsonObject.getString(KEY_QUESTION_EXPLANATION)!=null) {
mViewHolder.explanation.setText(jsonObject.getString(KEY_QUESTION_EXPLANATION));
mViewHolder.explanation.setVisibility(View.VISIBLE);
}
}break;
default:{
if(convertView==null){
convertView=this.inflater.inflate(R.layout.row_question_not_available,null);
}
}
}
}catch (Exception e){
}
return convertView;
}
static class ViewHolderYesOrNo{
public TextView question,type,explanation;
public RadioGroup radioGroup;
public RadioButton yes,no;
public ViewHolderYesOrNo(View view){
question=(TextView) view.findViewById(R.id.rowQuestionYesNo);
type=(TextView)view.findViewById(R.id.rowQuestionType);
explanation=(TextView)view.findViewById(R.id.rowQuestionExplanation);
radioGroup=(RadioGroup)view.findViewById(R.id.rowOptionRadioGroup);
yes=(RadioButton)view.findViewById(R.id.rowOptionYes);
no=(RadioButton)view.findViewById(R.id.rowOptionNo);
}
}
static class ViewHolderMultipleChoice{
public TextView question,type,explanation;
public RadioGroup radioGroup;
public RadioButton option1,option2,option3,option4;
public ViewHolderMultipleChoice(View view){
question=(TextView) view.findViewById(R.id.rowQuestionYesNo);
type=(TextView)view.findViewById(R.id.rowQuestionType);
explanation=(TextView)view.findViewById(R.id.rowQuestionExplanation);
radioGroup=(RadioGroup)view.findViewById(R.id.rowOptionRadioGroup);
option1=(RadioButton)view.findViewById(R.id.rowOptionOne);
option2=(RadioButton)view.findViewById(R.id.rowOptionTwo);
option3=(RadioButton)view.findViewById(R.id.rowOptionThree);
option4=(RadioButton)view.findViewById(R.id.rowOptionFour);
}
}
static class ViewHolderShortAnswer{
public TextView question,type,explanation;
public ViewHolderShortAnswer(View view){
question=(TextView) view.findViewById(R.id.rowQuestionYesNo);
type=(TextView)view.findViewById(R.id.rowQuestionType);
explanation=(TextView)view.findViewById(R.id.rowQuestionExplanation);
}
}
}
感謝您的幫助。
感謝您的幫助。但解釋視圖已經在行xml中設置了可見性,並且它工作正常。問題是滾動列表視圖的列表順序改變它自己和複製列表項。 點擊列表項時自動觸發所有具有相同ID的列表項目 –
我可能不夠清楚。我會更新答案並澄清導致問題的原因。 –
我以任何方式解決了這個問題,謝謝@MariusKaunietis,由於隱形視圖的問題, –