Listview和适配器与不同的对象

时间:2014-07-02 06:54:51

标签: android listview android-fragments

我有以下问题让我发疯,我有1个json文件,它有2种不同的数据,我需要显示这个 一个listview上的数据与每个标题分离我也需要在适配器中执行此操作,在到达之前我还需要合并一个对象中的数据以将其传递给适配器。

数据示例

<LiveSales>
    <Car_Sales_Response>
    <Name>Name</Name>
    <AwName>Abbrv Name</AwName>
    <Number>1</Number>
    <StartTime>2014-02-12T19:10:00+11:00</StartTime>
    <Code>e3029fc698fe35b61sdfkjhs</Code>
    <ChannelName>Online</ChannelName>
</LiveSales>

<NowSales>
    <ArrayOfSales xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Car Id="987123" SportId="1" CompId="109" Name="New Name" MatchDate="2014-07-05T00:00:00+10:00" StartTime="2014-07-05T13:47:00+10:00" TypeName="Racing"  DisplayPriority="0" Markets="2"></Car>
    <Car Id="982323" SportId="1" CompId="102" Name="New Name 2" MatchDate="2014-07-05T00:00:00+10:00" StartTime="2014-07-05T13:47:00+10:00" TypeName="Racing"  DisplayPriority="0" Markets="2"></Car>
    </ArrayOfSales>
</NowSales>

这是我的适配器,我还没有解决它。

public class CarAdapter extends ArrayAdapter<Object> {

    public CarAdapter ( Context context , int textViewResourceId, ArrayList<Object> items) {
        super(context, textViewResourceId, items);
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        // Create holder
        ViewHolder viewHolder = null;

        if (view == null) {
            view = mInflater.inflate(R.layout.row, null);
            viewHolder = new ViewHolder();

            viewHolder.date = (TextView) view.findViewById(R.id.textViewDate);
            viewHolder.names = (TextView) view.findViewById(R.id.textViewName);
            // more here 
        } else {
             viewHolder = (ViewHolder) view.getTag();
        }

        // Here I'm stuck 
        Object a = (Object) getItem(position);

        return view;

    }
}

我如何解析数据,两个网址都是相同的

public static ArrayList<LiveSale> getUpcomingMatches() {
        ArrayList<LiveSale> items = null;

    try {
        Document doc = App.getXMlDocument("URL");

        items = CarSales.parse(doc);
        return items;

    } catch (Exception e) { 
        e.printStackTrace();
    }

    return items;
}

public static ArrayList<NowSale> getLiveMatches() {

        ArrayList<NowSale> items = null;

        try {
            Document doc = App.getXMlDocument("URL");
            items = parse(doc);
            return items;

        } catch (Exception e) { 
            e.printStackTrace();
        }

        return items;
    }

PS我正在使用片段。

1 个答案:

答案 0 :(得分:2)

Object a = (Object) getItem(position);
if(a instanceof LiveSale) {
//DO SOMETHING
}
if(a instanceof NowSale) {
//DO SOMETHING ELSE
}