Java Integer.parseInt返回错误:Integer类型中的方法parseInt(String)不适用于参数(R.String)
我必须构建一系列我已经构建的结构: 类(项)的构建方式如下:(如下所示) 现在我的问题是,我试图将我从另一个地方(本例中是一个列表)得到的字符串号解析为int 但我得到了这个错误: Integer类型中的parseInt(String)方法不适用于 参数(R.string) 这是一段代码: (它说错误在(“Integer.parseInt”): 虽然很长,但并不复杂 非常感谢 编辑: 项目列表只是一个列表列表:Java Integer.parseInt返回错误:Integer类型中的方法parseInt(String)不适用于参数(R.String),java,android,list,arraylist,int,Java,Android,List,Arraylist,Int,我必须构建一系列我已经构建的结构: 类(项)的构建方式如下:(如下所示) 现在我的问题是,我试图将我从另一个地方(本例中是一个列表)得到的字符串号解析为int 但我得到了这个错误: Integer类型中的parseInt(String)方法不适用于 参数(R.string) 这是一段代码: (它说错误在(“Integer.parseInt”): 虽然很长,但并不复杂 非常感谢 编辑: 项目列表只是一个列表列表: List<List<string>> items; 守则:
List<List<string>> items;
守则:
public List<List<String>> Download()
{
String data = null;
//String res = "";
try {
client = new DefaultHttpClient();// Reference to the Internet
httppost = new HttpPost(URL);
HttpResponse response = client.execute(httppost);
HttpEntity entity = response.getEntity();// get the content of the
// message
InputStream webs = entity.getContent();
BufferedReader in = new BufferedReader(new InputStreamReader(webs,"iso-8859-1"));
StringBuffer sb = new StringBuffer("");
String l = " ";
// String nl=System.getProperty("line.separator");
while ((l = in.readLine()) != null) {
sb.append(l + "\n");
}
data = sb.toString();
webs.close();
List<List<String>> all= new ArrayList<List<String>>();
all.add(new ArrayList<String>());
all.add(new ArrayList<String>());
all.add(new ArrayList<String>());
all.add(new ArrayList<String>());
all.add(new ArrayList<String>());
all.add(new ArrayList<String>());
all.add(new ArrayList<String>());
try {
JSONObject json = new JSONObject(data);
JSONArray jArray = json.getJSONArray("item");
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
//res += json_data.getString("title")+"\n";
all.get(0).add(json_data.getString("id"));
}
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
//res += json_data.getString("title")+"\n";
all.get(0).add(json_data.getString("title"));
}
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
//res += json_data.getString("title")+"\n";
all.get(0).add(json_data.getString("desc"));
}
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
//res += json_data.getString("title")+"\n";
all.get(0).add(json_data.getString("pub"));
}
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
//res += json_data.getString("title")+"\n";
all.get(0).add(json_data.getString("p"));
}
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
//res += json_data.getString("title")+"\n";
all.get(0).add(json_data.getString("n"));
}
return all;
} catch (JSONException e) {
}
//return news;
} catch (Exception e) {
int x=3;
// TODO: handle exception
}
return null;
}
公共列表下载()
{
字符串数据=null;
//字符串res=“”;
试一试{
client=new DefaultHttpClient();//对Internet的引用
httppost=新的httppost(URL);
HttpResponse response=client.execute(httppost);
HttpEntity entity=response.getEntity();//获取
//信息
InputStream webs=entity.getContent();
BufferedReader in=新的BufferedReader(新的InputStreamReader(Web,“iso-8859-1”);
StringBuffer sb=新的StringBuffer(“”);
字符串l=“”;
//字符串nl=System.getProperty(“line.separator”);
而((l=in.readLine())!=null){
sb.追加(l+“\n”);
}
data=sb.toString();
webs.close();
List all=new ArrayList();
添加(新的ArrayList());
添加(新的ArrayList());
添加(新的ArrayList());
添加(新的ArrayList());
添加(新的ArrayList());
添加(新的ArrayList());
添加(新的ArrayList());
试一试{
JSONObject json=新的JSONObject(数据);
JSONArray jArray=json.getJSONArray(“项”);
for(int i=0;i
您使用的类型,string
,显然不是java.lang.string
,因此不能将其作为参数赋给Integer.parseInt(java.lang.string)
您使用的类型,string
,显然不是java.lang.string
,因此您不能将其作为参数赋给Integer.parseInt(java.lang.string)
更改代码,
使用字符串代替字符串
private int id;
private String title;
private String desc;
private String pub;
private int p;
private int n;
还要更改列表
List<List<String>> items;
列表项;
更改您的代码,
使用字符串代替字符串
private int id;
private String title;
private String desc;
private String pub;
private int p;
private int n;
还要更改列表
List<List<String>> items;
列表项;
在不更改代码的情况下执行相同操作的另一种方法是,您应该在parseInt()中调用getString(),以便getString()将返回java.lang.String,如下所示:
getString(R.string.value);
这样做可以节省用字符串替换字符串的大量工作。
如果打开R类,您将看到它只包含引用项目已编译资源的数字
选择权归你:)
干杯在不更改代码的情况下执行相同操作的另一种方法是,您应该在parseInt()中调用getString(),以便getString()将返回java.lang.String,如下所示:
getString(R.string.value);
这样做可以节省用字符串替换字符串的大量工作。
如果打开R类,您将看到它只包含引用项目已编译资源的数字
选择权归你:)
干杯请在列表中添加元素的位置显示您的项目列表。我正在将它们从带有httpRequest的JSON字符串解析为“项目”。然后我应该把它们排列在我自己结构的列表中。你能给我看一下代码吗?请检查你正在列表中添加的字符串号。请显示你的项目列表,你在列表中添加元素的位置。我将它们从带有httpRequest的JSON字符串解析为“项目”。然后我应该把它们排列在我自己的结构列表中。你能给我看一下代码吗请检查一下字符串号