我遇到了问题,我无法解决。我想创建民意调查申请。我创建了2个类: - 包含用户名称,问题和选择的ArrayList的民意调查 - 选择包含选择和其ID。
我想在主页上显示带有不同选项的问题列表(ArrayList)。我设法显示这个列表,但我有Poll类的ArrayList有问题,因为只显示第一个项目,但我想显示所有选择列表。
POLL CLASS
public class Poll{
String question;
String forename;
ArrayList<Choice> list_choices;
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
public String getForename() {
return forename;
}
public void setForename(String forename) {
this.forename = forename;
}
public ArrayList<Choice> getListChoice() {
return list_choices;
}
public void setListChoice(ArrayList<Choice> list_choices) {
this.list_choices = list_choices;
}
}
选择课程
public class Choice {
int id;
String choice;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getChoice() {
return choice;
}
public void setChoice(String choice) {
this.choice = choice;
}
}
投票适配器
public class CustomListAdapter extends BaseAdapter{
ArrayList<Poll> listPolls;
LayoutInflater layoutInflater;
Context context;
public CustomListAdapter(Context context,ArrayList<Poll> listPolls) {
this.listPolls = listPolls;
this.context = context;
layoutInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return listPolls.size();
}
@Override
public Object getItem(int arg0) {
return listPolls.get(arg0);
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
ViewHolder holder;
if (arg1==null)
{
arg1= layoutInflater.inflate(R.layout.row_list, null);
holder = new ViewHolder();
holder.ForenameView = (TextView) arg1.findViewById(R.id.forename);
holder.QuestionView = (TextView) arg1.findViewById(R.id.question);
holder.ChoiceView = (ListView) arg1.findViewById(R.id.choice);
//tagguer notre objet pour pouvoir le récupérer à la prochaine mise à jour graphique.
arg1.setTag(holder);
}
else
{
holder = (ViewHolder) arg1.getTag();
}
holder.ForenameView.setText(listPolls.get(arg0).getForename());
holder.QuestionView.setText(listPolls.get(arg0).getQuestion());
holder.ChoiceView.setAdapter(new CustomListChoiceAdapter(context, listPolls.get(arg0).getListChoice()));
return arg1;
}
static class ViewHolder {
TextView ForenameView;
TextView QuestionView;
ListView ChoiceView;
}
}
选择适配器
public class CustomListChoiceAdapter extends BaseAdapter {
ArrayList<Choice> listChoices;
LayoutInflater layoutInflater;
public CustomListChoiceAdapter(Context context,ArrayList<Choice> listChoices) {
this.listChoices = listChoices;
layoutInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return listChoices.size();
}
@Override
public Object getItem(int arg0) {
return listChoices.get(arg0);
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
ViewHolder holder;
if (arg1==null)
{
arg1= layoutInflater.inflate(R.layout.row_list_choice, null);
holder = new ViewHolder();
holder.ChoiceView = (TextView) arg1.findViewById(R.id.choice_text);
//holder.IdView = (TextView) arg1.findViewById(R.id.question);
//tagguer notre objet pour pouvoir le récupérer à la prochaine mise à jour graphique.
arg1.setTag(holder);
}
else
{
holder = (ViewHolder) arg1.getTag();
}
holder.ChoiceView.setText(listChoices.get(arg0).getChoice());
notifyDataSetChanged();
//holder.IdView.setText(listChoices.get(arg0).getId());
return arg1;
}
static class ViewHolder {
TextView ChoiceView;
//TextView IdView;
}
}
XML主页
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:orientation="vertical"
android:id="@+id/LinearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@drawable/customborder"
>
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#000000"
>
</ListView>
</RelativeLayout>
</LinearLayout>
XML轮询列表
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/forename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="Medium Text"
android:textColor="@color/blue"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/question"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:text="TextView" />
<ListView
android:id="@+id/choice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#000000"
>
</ListView>
</LinearLayout>
</LinearLayout>
XML选择列表
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/choice_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="Medium Text"
android:textColor="@color/pink"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</LinearLayout>
显示ArrayList的活动
public class debug extends Activity {
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.debugg);
//Récuperation de ListView crée
lv = (ListView) findViewById(R.id.listView1);
ArrayList<Choice> list_choice = new ArrayList<Choice>();
Choice choix = new Choice();
choix.setChoice("choix 1");
list_choice.add(choix);
choix = new Choice();
choix.setChoice("choix 2");
list_choice.add(choix);
ArrayList<Poll> resultats = new ArrayList<Poll>();
Poll newsData = new Poll();
newsData.setListChoice(list_choice);
newsData.setForename("Paul");
newsData.setQuestion("Question 1");
resultats.add(newsData);
newsData = new Poll();
newsData.setListChoice(list_choice);
newsData.setForename("Laetitia");
newsData.setQuestion("Question 2");
resultats.add(newsData);
lv.setAdapter(new CustomListAdapter(this, resultats));
}
}
答案 0 :(得分:0)
Man,一个和原始解决方案是:
设置您的父ListView
项WRAP_CONTENT
高度。接下来,设置包含的ListView
项目的固定高度。然后在getView()
方法设置中,您所包含的ListView
的高度等于parent_array_list.size() * child_list_view_item_height
。它可能对你有帮助。
代码示例:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) (listPolls.size() * TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, child_list_view_item_height, displayMetrics)));
holder.ChoiceView.setLayoutParams(params);