【问题标题】:Type mismatch: cannot convert from String to List<Cmd>类型不匹配:无法从 String 转换为 List<Cmd>
【发布时间】:2014-02-05 17:04:06
【问题描述】:

我收到以下错误:

类型不匹配:无法从字符串转换为列表

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);
       
        }
    }
}

【问题讨论】:

  • 试图将字符串分配给声明为 List 的变量

标签: java android string android-adapter listadapter


【解决方案1】:

更新变量List&lt;Cmd&gt; videos的setter方法如下:

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

【讨论】:

  • 我试过了,问题是它导致“Cmd 类型中的方法 setVideos(List) 不适用于参数 (String) SAXXMLHandler.java”就行了:} else if (qName.equalsIgnoreCase("videos")) { cmd.setVideos(tempVal);
  • 你需要遵循 eclipse 的第二个建议 将视频类型更改为列表。那么是什么导致你在第二次更改后面临额外错误 @ValdimárrOlaf ???
  • 我确实把它改成了 public void setVideos(List videos) { this.videos = videos;正如你所建议的......这导致了一个新错误:“Cmd 类型中的方法 setVideos(List) 不适用于参数 (String) SAXXMLHandler.java”
  • 或许应该如此? } else if (qName.equalsIgnoreCase("videos")) { cmd.setVideos(videos);
  • cmd.setVideos(tempVal);类 SAXX.. temval 是字符串,预计它必须是 List
【解决方案2】:

setVideos(String videos) 应定义为setVideos(List&lt;Cmd&gt; videos)

【讨论】:

  • 我试过了,问题是它导致“Cmd 类型中的方法 setVideos(List) 不适用于参数 (String) SAXXMLHandler.java”就行了:} else if (qName.equalsIgnoreCase("videos")) { cmd.setVideos(tempVal);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-18
  • 2016-04-08
  • 2019-11-24
  • 1970-01-01
  • 1970-01-01
  • 2014-04-07
  • 1970-01-01
相关资源
最近更新 更多