自定义ArrayList未显示正确的输出

时间:2014-05-30 10:05:04

标签: android

我有一个包含xml数据的arraylist。正确加载了xml数据,但它没有在listview上显示正确的o / p.o / p就像com.example.questions.QuestionArrayList@b4dd8e30 我的代码是

public class Test extends Activity 
{
    private TextView question;
    private RadioButton rdooption1;
    private RadioButton rdooption2;
    private RadioButton rdooption3;
    private RadioButton rdooption4;
    private static final String FILENAME = "xmlFileName.xml";
    private FileInputStream fin;

    QuestionArrayList currentquestion = null;
    List<QuestionArrayList> questions = null;
    private ListView details;   
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.question_layout);
        details=(ListView)findViewById(R.id.listView1);
    }
    private void parseXML(XmlPullParser xpp) throws XmlPullParserException, IOException 
    {
    questions= new ArrayList<QuestionArrayList>();
    currentquestion = new QuestionArrayList();
    while(xpp.next()!=XmlPullParser.END_DOCUMENT )
    {
    if (xpp.getEventType() != XmlPullParser.START_TAG) 
    {
    continue;
    }
    String name = xpp.getName();
    if(name.equals("Question"))
    {                               
    currentquestion.setQuestion_type(xpp.getAttributeValue(null, "id"));
    currentquestion.setQuestion_type(xpp.getAttributeValue(null, "type"));
    currentquestion.setQuestion_name(xpp.nextText());
    }
    if(currentquestion!=null)
    {
    questions.add(currentquestion);
    }
    }
    ArrayAdapter<QuestionArrayList> arrayadpater = new ArrayAdapter<QuestionArrayList>(this,android.R.layout.simple_list_item_1,questions);
    details.setAdapter(arrayadpater);
    }
    }

2 个答案:

答案 0 :(得分:1)

ArrayAdapter将使用构造函数中传递的toString的{​​{1}}方法

来自official documentation

Object

请看第二段,它说如果您要传递自定义A concrete BaseAdapter that is backed by an array of arbitrary objects. By default this class expects that the provided resource id references a single TextView. If you want to use a more complex layout, use the constructors that also takes a field id. That field id should reference a TextView in the larger layout resource. However the TextView is referenced, it will be filled with the toString() of each object in the array. You can add lists or arrays of custom objects. Override the toString() method of your objects to determine what text will be displayed for the item in the list. ,则应覆盖自定义Object类中的toString方法。

答案 1 :(得分:0)

我得到了我自己问题的简单回答 我们必须在类中重写java的tostring()方法。我在这里编写简单的例子

public class QuestionArrayList 
{
public String question_name;
public String option_one;
public String getQuestion_name() 
    {
        return question_name;
    }
    public void setQuestion_name(String question_name) 
    {
        this.question_name = question_name;
    }
public String getOption_one() {
        return option_one;
    }
    public void setOption_one(String option_one) {
        this.option_one = option_one;
    }
@Override
    public String toString() 
    {
        return "question_name:"+ question_name+ "|option_one:"+ option_one;
    }
}

您可以将这些对象简单地分配给列表视图