showing the object instead of string

元气小坏坏 提交于 2019-12-24 03:06:50

问题


Here i m attaching snap of my problem and code for that. it shows me only the content as object but shows groupname perfectly. the snap for this problem is given in as below link just go throught this image

"http://imageupload.org/?d=4DA941521"(snapshot)

=> I want child data of particular groupname but i m getting groupname instead of child data.

code for this

package com.bestdambikers;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class SectionedDemo extends ListActivity {


    String strUrl = "http://192.168.5.10/ijoomer_development/index.php?option=com_ijoomer&plg_name=jomsocial&pview=user&ptask=field_list&userid=80&sessionid="+ConstantData.session_id+"&tmpl=component";
      DetailBean dBean;
      XmlParser parser;
      ArrayList<Object>  result;
      List<DetailBean> list;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.header_main);

        //dBean = new DetailBean();
        parser = new XmlParser(strUrl, new DetailBean());
        result = parser.ParseUrl("data", "group");
        int r = result.size();

        for(int i=0; i<result.size(); i++)
        {

            dBean = (DetailBean)result.get(i);
            list=Arrays.asList(dBean);
            Collections.shuffle(list);
            adapter.addSection(dBean.group_name,
                new ArrayAdapter<DetailBean>(this,
                        android.R.layout.simple_list_item_1,
                        list));
        }
        setListAdapter(adapter);
    }

    SectionedAdapter adapter=new SectionedAdapter() {
        protected View getHeaderView(String caption, int index,
                View convertView,
                ViewGroup parent) {
            TextView result=(TextView)convertView;

            if (convertView==null) {
                result=(TextView)getLayoutInflater()
                .inflate(R.layout.header,
                        null);
            }

            result.setText(caption);

            return(result);
        }
    };
}

DetailBean.java

public class DetailBean
{
    public String data = null;
    public String code = null;
    public String fields = null;
    public String group_name = null;
    public String field = null;
    public String id = null;
    public String name = null;
    public String value = null;
    public String status = null;
    public String required = null;
    public String type = null;

    public DetailBean()
    {
        this("","","","","","","","","","","");
    }

    public DetailBean(String data,String code,String fields, String group_name,String field, String id,String name,String value,String status,String required,String type)
    {
        this.data = data;
        this.code = code;
        this.fields = fields;
        this.group_name = group_name;
        this.field = field;
        this.id = id;
        this.name = name;
        this.value = value;
        this.status = status;
        this.required = required;
        this.type = type;
    }

}

回答1:


It looks like you are getting a String in your getHeaderView method's caption parameter that is made by detailBeanInstance.toString().

You should override the toString method in DetailBean class to return what you want.

Such an override would be:

@Override
public String toString()
{
    //You should populate this string with the data 
    //you need inside the TextView
    return this.group_name + " " + this.data;
}

Place the method above inside your DetailBean class, so it would look like:

public class DetailBean
{
    // You should use private members, and create getters and setters to them
    public String data = null;
    public String code = null;
    public String fields = null;
    public String group_name = null;
    public String field = null;
    public String id = null;
    public String name = null;
    public String value = null;
    public String status = null;
    public String required = null;
    public String type = null;

    public DetailBean()
    {
        this("", "", "", "", "", "", "", "", "", "", "");
    }

    public DetailBean(String data, String code, String fields, 
            String group_name, String field, String id, String name,
            String value, String status, String required, String type)
    {
        this.data = data;
        this.code = code;
        this.fields = fields;
        this.group_name = group_name;
        this.field = field;
        this.id = id;
        this.name = name;
        this.value = value;
        this.status = status;
        this.required = required;
        this.type = type;
    }

    @Override
    public String toString()
    {
        // You should populate this string with the data
        // you need inside the TextView
        return this.group_name + " " + this.data;
    }
}



回答2:


Your DetailBean is missing a toString() method so that the method from the parent (i.e. Object) is taken, which just prints the classname + its address.




回答3:


A simple solution might be to update DetailBean to include overriding the toString method to retrieve whatever String representation you want for the DetailBean objects.

(Ah. I was beaten by 4 minutes and 2 minutes.)



来源:https://stackoverflow.com/questions/5685084/showing-the-object-instead-of-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!