列表视图中小部件评级栏的问题
我的应用程序涉及在线解析一个包含酒店数据(名称、地址、类别)的 XML 文件,但是当我尝试管理为 1 星级、2 星级酒店类别定义的星级栏时……我没有成功。 首先看一下我的 XML 文件:
<hotels count="2">
<hotel>
<name>el mouradi</name>
<place>hammamet</place>
<category>5</category>
</hotel>
<hotel>
<name>caralton</name>
<place>tunis</place>
<category>4</category>
</hotel>
</hotels>
这是我在 Main.java 中的类。我可以将 Xml 文件数据作为表导入,然后在列表视图中以字符串形式查看数据,不会出现问题:
package com.hotel.tn;
import java.util.ArrayList;
import java.util.HashMap;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import com.pxr.tutorial.xmltest.R;
import android.app.ListActivity;
import android.os.Bundle;
//import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
//import android.widget.RatingBar;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class Main extends ListActivity {
private ListView list;
/** Called when the activity is first created.
* @param stars */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
list = getListView();
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
String xml = XMLfunctions.getXML();
Document doc = XMLfunctions.XMLfromString(xml);
int numResults = XMLfunctions.numResults(doc);
if((numResults <= 0)){
Toast.makeText(Main.this, "pas de resultas", Toast.LENGTH_LONG).show();
finish();
}
NodeList nodes = doc.getElementsByTagName("hotel");
for (int i = 0; i < nodes.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element)nodes.item(i);
map.put("cat", XMLfunctions.getValue(e, "categorie"));
map.put("nom",XMLfunctions.getValue(e, "nom"));
map.put("adr",XMLfunctions.getValue(e, "adr"));
mylist.add(map);
}
ListAdapter adapter = new SimpleAdapter(this,mylist,R.layout.main,
new String[]{"nom","adr","cat"},
new int[] { R.id.item_title, R.id.item_subtitle,R.id.item_su});
list.setAdapter(adapter);
}}
这是 Listeadapter.java。
Eclipse 向我显示以下错误:“the method get int type in the list not apply for the argument string”在这一行 rating.setRating (List.get ("cat")
。
如果我尝试将 List.get("cat")
行中的 "cat"
替换为整数 我可以运行该应用程序,但评级栏不会改变 可能出了什么问题。 ?
package com.hotel.tn;
//import android.app.ListActivity;
import java.util.List;
import java.util.Map;
import com.pxr.tutorial.xmltest.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RatingBar;
import android.widget.SimpleAdapter;
public class Listeadapter extends SimpleAdapter {
private LayoutInflater mInflater;
public Listeadapter (Context context, List<? extends Map<String, ?>> data,
int resource, String[] from, int[] to)
{
super (context, data, resource, from, to);
mInflater = LayoutInflater.from (context);
}
public Object getItem (int position)
{
return getItem (position);
}
@Override
public View getView (int position, View convertView, ViewGroup parent){
if (convertView == null){
// holder = new ViewHolder();
convertView = mInflater.inflate (R.layout.main, null);
// holder.
RatingBar rating = (RatingBar)convertView.findViewById(R.id.rating);
rating.setRating(List.get(cat));/*or rating.setRating(2); dont work **/
rating.setTag (position);
// convertView.setTag(holder);
}
return super.getView (position, convertView, parent);
} }
My application involves parsing an XML file online that contains data on hotels (name, address, category) but when I try to manage the star rating bar defined for the category of hotels 1 star, 2 star ... I am not successful.
First look at my XML file:
<hotels count="2">
<hotel>
<name>el mouradi</name>
<place>hammamet</place>
<category>5</category>
</hotel>
<hotel>
<name>caralton</name>
<place>tunis</place>
<category>4</category>
</hotel>
</hotels>
This is my class in Main.java. I could import the Xml file data as a table, then view the data as a string in the list view without a problem:
package com.hotel.tn;
import java.util.ArrayList;
import java.util.HashMap;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import com.pxr.tutorial.xmltest.R;
import android.app.ListActivity;
import android.os.Bundle;
//import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
//import android.widget.RatingBar;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class Main extends ListActivity {
private ListView list;
/** Called when the activity is first created.
* @param stars */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
list = getListView();
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
String xml = XMLfunctions.getXML();
Document doc = XMLfunctions.XMLfromString(xml);
int numResults = XMLfunctions.numResults(doc);
if((numResults <= 0)){
Toast.makeText(Main.this, "pas de resultas", Toast.LENGTH_LONG).show();
finish();
}
NodeList nodes = doc.getElementsByTagName("hotel");
for (int i = 0; i < nodes.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element)nodes.item(i);
map.put("cat", XMLfunctions.getValue(e, "categorie"));
map.put("nom",XMLfunctions.getValue(e, "nom"));
map.put("adr",XMLfunctions.getValue(e, "adr"));
mylist.add(map);
}
ListAdapter adapter = new SimpleAdapter(this,mylist,R.layout.main,
new String[]{"nom","adr","cat"},
new int[] { R.id.item_title, R.id.item_subtitle,R.id.item_su});
list.setAdapter(adapter);
}}
This is Listeadapter.java.
Eclipse shows me the following error: "the method get int type in the list not applicable for the argument string" at this line rating.setRating (List.get ("cat")
.
If I try to replace "cat"
with an integer in the line List.get("cat")
I could run the application but the rating bar doesn't change. What could be wrong?
package com.hotel.tn;
//import android.app.ListActivity;
import java.util.List;
import java.util.Map;
import com.pxr.tutorial.xmltest.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RatingBar;
import android.widget.SimpleAdapter;
public class Listeadapter extends SimpleAdapter {
private LayoutInflater mInflater;
public Listeadapter (Context context, List<? extends Map<String, ?>> data,
int resource, String[] from, int[] to)
{
super (context, data, resource, from, to);
mInflater = LayoutInflater.from (context);
}
public Object getItem (int position)
{
return getItem (position);
}
@Override
public View getView (int position, View convertView, ViewGroup parent){
if (convertView == null){
// holder = new ViewHolder();
convertView = mInflater.inflate (R.layout.main, null);
// holder.
RatingBar rating = (RatingBar)convertView.findViewById(R.id.rating);
rating.setRating(List.get(cat));/*or rating.setRating(2); dont work **/
rating.setTag (position);
// convertView.setTag(holder);
}
return super.getView (position, convertView, parent);
} }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论