gpt4 book ai didi

java - Android Listview Inside viewpager 始终显示所有页面中最后一个 viewpager 页面数组列表数据

转载 作者:行者123 更新时间:2023-12-01 10:34:17 26 4
gpt4 key购买 nike

它是一个 View 页面,其中包含具有特定问题答案的 ListView 。

Listview(Arraylist)一页一页地都有所有数据,我已经通过PrintLn在logcat上检查了它。所以问题只是在于,在所有 viewpager 页面中,listview 在 vi​​ewpager 的所有页面中显示最后一页答案(数据)。

我也尝试过holder.adapter.notifydatasetchanged();但它不起作用。

Here is the screenshot of my screen, showing the Question and below is the list of answers

这是我的代码如下,

    @Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
View convertView = null;

convertView=inflater.inflate(resource, null);
holder=new ViewHolder();

holder.lvCaseAnswers = (ListView)convertView.findViewById(R.id.lvCaseAnswers);
holder.lvCaseAnswers.setTag(position);
holder.txtCaseChallengePostedBy=(TextView)convertView.findViewById(R.id.txtCaseChallengePostedBy);
holder.txtCaseChallengeQuestion = (TextView)convertView.findViewById(R.id.txtCaseChallengeQuestion);
holder.txtCaseChallengeCategory = (TextView)convertView.findViewById(R.id.txtCaseChallengeCategory);
holder.btnCaseChallengeSubmit=(Button)convertView.findViewById(R.id.btnCaseChallengeSubmit);
//btnCaseChallengeTry=(Button)convertView.findViewById(R.id.btnCaseChallengeTry);

//get data from list
System.out.println("que_id: "+listItems.get(position).getQue_id());
dbconn.open();
c=dbconn.getQuestionById(listItems.get(position).getQue_id());
if(c.moveToFirst())
{
//set data from database
Toast.makeText(context, "data:"+c.getString(0), 0).show();
holder.txtCaseChallengePostedBy.setText(" Posted By "+c.getString(2));
holder.txtCaseChallengeQuestion.setText(c.getString(4));
String category = c.getString(3);
category = category.substring(0,1).toUpperCase() + category.substring(1).toLowerCase();
holder.txtCaseChallengeCategory.setText(category);

holder.listanswers.add(c.getString(5));
holder.listanswers.add(c.getString(6));
holder.listanswers.add(c.getString(7));
holder.listanswers.add(c.getString(8));
holder.listanswers.add(c.getString(9));
System.out.println("list item name:"+holder.listanswers.get(0));
holder.adapter = new ArrayAdapter<String>(context, R.layout.raw_answers, holder.listanswers){

@Override
public View getView(int position,
View view, ViewGroup parent) {
// TODO Auto-generated method stub
Integer txtPosition = (Integer) parent.getTag();
LayoutInflater inflater=LayoutInflater.from(context);
view = inflater.inflate(R.layout.raw_answers, null);
TextView txt=(TextView)view.findViewById(R.id.txtRawAnswers);

CheckedTextView text = (CheckedTextView) view.findViewById(R.id.txtRawAnswers);
text.setText(holder.listanswers.get(position));
if(holder.listOptions.get(position).equals(holder.correct_ans))
{
text.setTextColor(Color.GREEN);
text.setCompoundDrawablesWithIntrinsicBounds(R.drawable.custom_radio_btn, 0, R.drawable.ic_true, 0);
}
else
{
text.setTextColor(Color.RED);
text.setCompoundDrawablesWithIntrinsicBounds(R.drawable.custom_radio_btn, 0, R.drawable.ic_false, 0);
}
return view;
}
};
holder.adapter.notifyDataSetChanged();
holder.lvCaseAnswers.setAdapter(holder.adapter);
holder.lvCaseAnswers.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
Constants.setListViewHeightBasedOnChildren(holder.lvCaseAnswers);

holder.correct_ans=c.getString(10);
holder.answer=c.getString(11);
holder.txtCaseChallengeUserAnswer.setText(holder.answer);

for(int i=0;i<holder.listOptions.size();i++)
{
if(holder.listOptions.get(i).equals(holder.answer))
{
holder.lvCaseAnswers.setItemChecked(i, true);
}
else
{
holder.lvCaseAnswers.setItemChecked(i, false);
}
}

holder.lvCaseAnswers.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
holder.answer=holder.listOptions.get(position);
}
});
holder.adapter.notifyDataSetChanged();
holder.lvCaseAnswers.setEnabled(false);
holder.btnCaseChallengeSubmit.setVisibility(View.GONE);


}
dbconn.close();
// Add viewpager_item.xml to ViewPager
((ViewPager) container).addView(convertView);

return convertView;
}

最佳答案

您没有在viewpager中使用Fragment。将 viewpager 与 fragment 一起使用。它会起作用的。

关于java - Android Listview Inside viewpager 始终显示所有页面中最后一个 viewpager 页面数组列表数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34875849/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com