我有一个特殊的问题。我在解析一家餐馆的菜单卡。他们用英语和德语。我有一个类FoodItem:
public class FoodItem {
private int foodClass;
private String foodType;
private String foodName;
private String foodCost;
private String hauptBeilage;
private String salat;
}
现在,我有一个使用Jsoup下载的食物项目的arraylist。我使用String foodType
分隔德语和英语菜单。
我想在开始时列出德语菜单。但是,我也将英文菜单附加到列表中。我应该怎么解决这个问题?
我的downloadThread(Jsoup)是:
public void run()
{
Log.i("downloadThread", "Inside run() - Starting getFoodItems");
getDailyGerman();
getDailyEnglish();
//Sending a message through handler here
}
在我的活动中,我有:
handler = new android.os.Handler() {
@Override
public void handleMessage(Message msg) {
foodItemAdapter.notifyDataSetChanged();
}
};
如果我在getDailyGerman();
之后通过处理程序发送消息,那么我得到illegalstateexception
说适配器的内容已更改,但列表视图未更新。
我的适配器代码:
public FoodItemAdapter(Context context, int textViewResourceId, ArrayList<FoodItem> FoodItemArg) {
super(context, textViewResourceId, FoodItemArg);
FoodItemAdapter.foodItems = FoodItemArg;
this.setNotifyOnChange(false);
// if(FoodItemAdapter.foodItems == null)
// Log.i("Adapter", "Problem Inside Adapter Constructor");
}
//=========================public methods============================
public static ArrayList<FoodItem> getDailyEnglishFoodItems()
{
ArrayList<FoodItem> returnList = new ArrayList<FoodItem>();
for(FoodItem eachItem : FoodItemAdapter.foodItems)
{
if(eachItem.getFoodClass() == 1)
{
Log.i("Adapter" , "Adding English Daily Food : " + eachItem.getFoodName());
returnList.add(eachItem);
}
}
return returnList;
}
public static ArrayList<FoodItem> getDailyGermanFoodItems()
{
ArrayList<FoodItem> returnList = new ArrayList<FoodItem>();
for(FoodItem eachItem : FoodItemAdapter.foodItems)
{
if(eachItem.getFoodClass() == 2)
{
Log.i("Adapter" , "Adding German Daily Food : " + eachItem.getFoodName());
returnList.add(eachItem);
}
}
return returnList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
/*
* Describes each view in the list view.
* Get the question and find the question text, timestamp and the votes.
* Show them in the textview which is a part of the listview.
*/
View v = convertView;
FoodItem foodItem =(FoodItem) FoodItemAdapter.foodItems.get(position);
if(foodItem == null)
{
Log.i("Adapter", "Null Food Item");
}
int colorPos = 0;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.fooditem_row, null);
colorPos = position % colors.length;
}
请帮助我,因为我被困在这一点上3天。感谢。
答案 0 :(得分:0)
一旦我添加了项目并调用了,我就遇到了同样的问题 UI中的notifyDataSetChanged() 线程问题解决了
答案 1 :(得分:0)
根据我对您的问题的理解,您希望将英语项目放在列表顶部,然后是德语项目。你可以使用Collection.sort方法和使用特定的比较器来完成手头的任务。
例如:
final List<FoodItem> combinedList = getDailyGermanFoodItems();
combinedList.addAll(getDailyEnglishFoodItems());
Collections.sort(compinedList, new FoodItemComparator());
//then you call the handler to update the adapter and the listView
handler.post(new Runnable(){
public void run(){
FoodItemAdapter adapter = new FoodItemAdapter(activity.this, layout, combinedList);
listView.setAdapter(adapter);
}});
其中FoodItemComparator
:
public class FoodItemComparatorimplements Comparator<FoodItem>{
public int compare(FoodItem item1, item2) {
String foodType1 = item1.getFoodType();
String foodType2 = item2.getFoodType();
if (foodType1.equals(foodType2))
return 0;
if (foodType1.equals("English"))
return 1;
if (foodType2.equals("English))
return -1;
return foodType1.compareTo(foodType2);
}
}
假设foodType值仅保证为德语/英语。 此外,您必须在FoodItem类中包含一个getter函数,以便比较器可以访问它:
Class FoodItem
.......
public String getFoodType(){
return foodType;
}
修改强> 如果要单独显示每个列表,则将两个列表存储在活动对象中,然后在用户选择语言时使用(英语/德语):
FoodItemAdapter adapter = new FoodItemAdapter(activity.this, layout, germanList);
listView.setAdapter(adapter);