0

我有一个特殊的问题。我正在解析一家餐馆的菜单卡。他们有英语和德语的。我有一个类 FoodItem 作为:

public class FoodItem {

private int foodClass;
    private String foodType;
    private String foodName;
    private String foodCost;
    private String hauptBeilage;
    private String salat;
}

现在,我有一个使用 Jsoup 下载的 fooditems 的数组列表。我使用 . 分隔德语和英语菜单String foodType

我想在开始时列出德语菜单。但是,我也将英文菜单附加到列表中。我应该如何解决这个问题?

我的下载线程(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 天。谢谢。

4

2 回答 2

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);
于 2013-02-01T11:44:08.623 回答
0

添加项目并在 UI 线程问题中调用 notifyDataSetChanged() 后,我遇到了同样的问题

于 2013-01-31T16:53:17.730 回答