将XML本地数据放入RecyclerView

时间:2015-07-10 04:28:31

标签: android xml-parsing android-recyclerview

我想将一些数据从本地XML数据传递给RecylerView,但我从整个作品中得到了一些问题。请检查并帮助我。
这是我的MainActivity.java

public class MainActivity extends Activity {

private List<Tour> TourList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    RecyclerView recList = (RecyclerView) findViewById(R.id.cardList);
    recList.setHasFixedSize(true);
    LinearLayoutManager llm = new LinearLayoutManager(this);
    llm.setOrientation(LinearLayoutManager.VERTICAL);
    recList.setLayoutManager(llm);

    ToursPullParser parser = new ToursPullParser();
    List<Tour> tours = parser.parseXML(this);

    TourAdapter ca = new TourAdapter(tours);
    recList.setAdapter(ca);

}

public static class TourViewHolder extends RecyclerView.ViewHolder {
    protected TextView myTitle;
    protected TextView vDesc;
    protected TextView vLike;
    protected ImageView vPic;
    protected ImageView vPicLike;
    protected Button vTag;
    protected Button vFakeTag;

    public TourViewHolder(View v) {
        super(v);
        myTitle =  (TextView) v.findViewById(R.id.txtTitle);
        vDesc = (TextView)  v.findViewById(R.id.txtDesc);
        vLike = (TextView)  v.findViewById(R.id.txtLike);
        vPic = (ImageView)  v.findViewById(R.id.imgTitle);
        vPicLike = (ImageView)  v.findViewById(R.id.imgLike);
        vFakeTag = (Button)  v.findViewById(R.id.btnFakeTag);
        vTag = (Button)  v.findViewById(R.id.btnTag);
    }
}
}  

适配器在这里:

public class TourAdapter extends RecyclerView.Adapter<TourAdapter.TourViewHolder> {

ToursPullParser parser;
private List<Tour> TourList;

public TourAdapter( List<Tour> tours) {
}



@Override
public int getItemCount() {
    return TourList.size();
}

@Override
public void onBindViewHolder(TourViewHolder tourViewHolder, int i) {
    Tour tour = TourList.get(i);
    TourViewHolder.myTitle.setText(tour.getTitle());
    TourViewHolder.vDesc.setText(tour.getDescription());
    TourViewHolder.vLike.setText((int) tour.getLike());
    TourViewHolder.vTag.setText(tour.getTag());
}

@Override
public TourViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View itemView = LayoutInflater.
            from(viewGroup.getContext()).
            inflate(R.layout.drawer, viewGroup, false);

    return new TourViewHolder(itemView);
}

public static class TourViewHolder extends RecyclerView.ViewHolder {
    protected static TextView myTitle;
    protected static TextView vDesc;
    protected static TextView vLike;
    protected static ImageView vPic;
    protected static ImageView vPicLike;
    protected static Button vTag;
    protected static Button vFakeTag;

    public TourViewHolder(View v) {
        super(v);
        myTitle =  (TextView) v.findViewById(R.id.txtTitle);
        vDesc = (TextView)  v.findViewById(R.id.txtDesc);
        vLike = (TextView)  v.findViewById(R.id.txtLike);
        vPic = (ImageView)  v.findViewById(R.id.imgTitle);
        vPicLike = (ImageView)  v.findViewById(R.id.imgLike);
        vFakeTag = (Button)  v.findViewById(R.id.btnFakeTag);
        vTag = (Button)  v.findViewById(R.id.btnTag);
    }
}
}  

自定义XML PullParser:

public class ToursPullParser {

private static final String LOGTAG = "EXPLORECA";

private static final String TOUR_ID = "tourID";
private static final String TOUR_TITLE = "tourTitle";
private static final String TOUR_DESC = "tourDesc";
private static final String TOUR_LIKE = "tourLike";
private static final String TOUR_IMAGE = "tourPic";
private static final String TOUR_TAG = "tourTag";

private Tour currentTour  = null;
private String currentTag = null;
List<Tour> tours = new ArrayList<Tour>();

public List<Tour> parseXML(Context context) {

    try {
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser xpp = factory.newPullParser();

        InputStream stream = context.getResources().openRawResource(R.raw.tour);
        xpp.setInput(stream, null);

        int eventType = xpp.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            if (eventType == XmlPullParser.START_TAG) {
                handleStartTag(xpp.getName());
            } else if (eventType == XmlPullParser.END_TAG) {
                currentTag = null;
            } else if (eventType == XmlPullParser.TEXT) {
                handleText(xpp.getText());
            }
            eventType = xpp.next();
        }

    } catch (NotFoundException e) {
        Log.d(LOGTAG, e.getMessage());
    } catch (XmlPullParserException e) {
        Log.d(LOGTAG, e.getMessage());
    } catch (IOException e) {
        Log.d(LOGTAG, e.getMessage());
    }

    return tours;
}

private void handleText(String text) {
    String xmlText = text;
    if (currentTour != null && currentTag != null) {
        if (currentTag.equals(TOUR_ID)) {
            Integer id = Integer.parseInt(xmlText);
            currentTour.setId(id);
        } 
        else if (currentTag.equals(TOUR_TITLE)) {
            currentTour.setTitle(xmlText);
        }
        else if (currentTag.equals(TOUR_DESC)) {
            currentTour.setDescription(xmlText);
        }
        else if (currentTag.equals(TOUR_IMAGE)) {
            currentTour.setImage(xmlText);
        }
        else if (currentTag.equals(TOUR_LIKE)) {
            double like = Double.parseDouble(xmlText);
            currentTour.setLike(like);
        }else if (currentTag.equals(TOUR_TAG)) {
            currentTour.setTag(xmlText);
        }
    }
}

private void handleStartTag(String name) {
    if (name.equals("tour")) {
        currentTour = new Tour();
        tours.add(currentTour);
    }
    else {
        currentTag = name;
    }
}
}

最后是一个名为Tour.java的类:

public class Tour {
private int id;
private static String title;
private static String description;
private static String tag;
private static double like;
private static String image;

public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public static String getTitle() {
    return title;
}
public void setTitle(String title) {
    this.title = title;
}
public static String getDescription() {
    return description;
}
public void setDescription(String description) {
    this.description = description;
}
public static String getTag() {
    return tag;
}
public void setTag(String tag) {
    this.tag = tag;
}
public static String getImage() {
    return image;
}
public void setImage(String image) {
    this.image = image;
}
public static double getLike() {
    return like;
}
public void setLike(Double like) {
    this.like = like;
}

}    
关于xml的一切都很好,不用担心它们 谢谢大家

1 个答案:

答案 0 :(得分:0)

我在认真思考2小时后自己解决了这个问题,这是解决方案:
MainActivity.class我添加:

private List<Tour> myTourList() {
    ToursPullParser parser = new ToursPullParser();
    ArrayList<Tour> tour = (ArrayList<Tour>) parser.parseXML(this);
    return tour;
}  

并在TourAdapter.class我这样做了:

private ArrayList<Tour> tourList;
public TourAdapter(List<Tour> myTourList) {
    this.tourList = (ArrayList<Tour>) myTourList;
}

@Override
public int getItemCount() {
    return tourList.size();
}

@Override
public void onBindViewHolder(TourViewHolder tourViewHolder, int i) {
    Tour tour = tourList.get(i);
    TourViewHolder.myTitle.setText(tour.getTitle());
    TourViewHolder.vDesc.setText(tour.getDescription());
    TourViewHolder.vTag.setText(tour.getTag());
}