类型不匹配:无法从String转换为List <cmd> </cmd>

时间:2014-02-05 17:04:07

标签: java android string android-adapter listadapter

我遇到了以下错误:

  

类型不匹配:无法从String转换为List

来自以下代码的this.videos = videos;

public void setVideos(String videos) {
    this.videos = videos;
}

我已经尝试过遵循Eclipse的两个建议:

  • 将视频类型更改为字符串
  • 将视频类型更改为列表

但是,似乎都没有解决问题,并且都会导致额外的错误。

我现在还不确定我能做些什么来解决这个问题。

Cmd.java

public class Cmd implements ListAdapter {

    private String success;
    private String cmd;
    List<Cmd> videos;
    private String video;
    private String numberofvideos;
    private String videoname;
    private String videourl;
    private LayoutInflater mInflater;
    Button fav_up_btn1;
    Button fav_dwn_btn1;
    Context my_context;   

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_item_user_video,
                    parent, false);
        }

        ImageView thumb = (ImageView) convertView
                .findViewById(R.id.userVideoThumbImageView);
        TextView title = (TextView) convertView
                .findViewById(R.id.userVideoTitleTextView);
        TextView uploader = (TextView) convertView
                .findViewById(R.id.userVideouploaderTextView);        
        TextView viewCount = (TextView) convertView
                .findViewById(R.id.userVideoviewsTextView);
        uploader.setText(videos.get(position).getTitle());
        viewCount.setText(videos.get(position).getviewCount() + " views");

        // Get a single video from our list
        final Cmd video = videos.get(position);

        // Set the image for the list item


        // Set the title for the list item
        title.setText(video.getTitle());
        uploader.setText("by " + video.getUploader() + " |  ");

        return convertView;
    }

    public String getUploader() {
        // TODO Auto-generated method stub
        return null;
    }

    public String getviewCount() {
        // TODO Auto-generated method stub
        return null;
    }

    public CharSequence getTitle() {
        // TODO Auto-generated method stub
        return null;
    }

    public String getCmd() {
        return cmd;
    }

    public void setCmd(String cmd) {
        this.cmd = cmd;
    }
    public String getSuccess() {
        return success;
    }

    public void setSuccess(String success) {
        this.success = success;
    }

    public String getNumberOfVideos() {
        return numberofvideos;
    }
    public void setNumberOfVideos(String numberofvideos) {
        this.numberofvideos = numberofvideos;
    }
    public List<Cmd> getVideos() {
        return videos;
    }
    public void setVideos(String videos) {
        this.videos = videos;
    }
    public String getVideo() {
        return video;
    }
    public void setVideo(String video) {
        this.video = video;
    }
    public String getVideoName() {
        return videoname;
    }

    public void setVideoName(String videoname) {
        this.videoname = videoname;
    }
    public String getVideoURL() {
        return videourl;
    }

    public void setVideoURL(String videourl) {
        this.videourl = videourl;
    }

    @Override
    public int getCount() {
        return videos.size();
    }

    @Override
    public Object getItem(int position) {
        return videos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }   
}

SAXXMLHandler.java

public class SAXXMLHandler extends DefaultHandler {
    private List<Cmd> videos;
    private String tempVal;
    // to maintain context
    private Cmd cmd;

    public SAXXMLHandler() {
        videos = new ArrayList<Cmd>();
    }

    public List<Cmd> getResponse() {
        return videos;
    }

    // Event Handlers
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        // reset
        tempVal = "";
        if (qName.equalsIgnoreCase("cmd")) {
            // create a new instance of cmd
            cmd = new Cmd();

        }
    }

    public void characters(char[] ch, int start, int length)
            throws SAXException {
        tempVal = new String(ch, start, length);
    }

    public void endElement(String uri, String localName, String qName)
            throws SAXException {

        if (qName.equalsIgnoreCase("videos")) {
            // add it to the list
            videos.add(cmd);
        } else if (qName.equalsIgnoreCase("success")) {
            cmd.setSuccess(tempVal);
        } else if (qName.equalsIgnoreCase("numberofvideos")) {
            cmd.setNumberOfVideos(tempVal);
        } else if (qName.equalsIgnoreCase("videos")) {
            cmd.setVideos(tempVal);
        } else if (qName.equalsIgnoreCase("video")) {
            cmd.setVideo(tempVal);
        } else if (qName.equalsIgnoreCase("videoname")) {
            cmd.setVideoName(tempVal);
        } else if (qName.equalsIgnoreCase("videourl")) {
            cmd.setVideoURL(tempVal);

        }
    }
}

2 个答案:

答案 0 :(得分:1)

setVideos(String videos)应定义为setVideos(List<Cmd> videos)

答案 1 :(得分:1)

更新变量List<Cmd> videos的setter方法,如下所示:

public void setVideos(List<Cmd> videos) {
        this.videos = videos;
    }