-4

这是我的 logcat 错误;

03-20 04:24:42.482: E/AndroidRuntime(920): java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. [in ListView(2131165193, class android.widget.ListView) with Adapter(class com.example.recipestutors.ItemListBaseAdapter)]

我提出了所有可能的方法,但它一次又一次地显示错误 ItemListAdapter.java 我为这些问题添加的是

public class ItemListBaseAdapter extends BaseAdapter {
private static ArrayList<Recipedetails> itemDetailsrrayList;

private Integer[] imgid = {
                R.drawable.vegeterian,
                R.drawable.nonveg,
                R.drawable.pickels,
                R.drawable.soup,
                R.drawable.sweets,
                R.drawable.cakes,
                R.drawable.icecreams,
                R.drawable.chinesevegdishes,
                R.drawable.chinesenonveg,
                R.drawable.chinesenoodles,
                R.drawable.chinesesoup,
                R.drawable.godhumaivegadai,
                R.drawable.soyamorekoozh,
                R.drawable.tomatokulambu,
                R.drawable.tomatoupma,
                R.drawable.vadanavratrispecial,
                R.drawable.eggkurma,
                R.drawable.milagu,
                R.drawable.rasam,
                R.drawable.vegetablekootu,
                R.drawable.avial,
                R.drawable.kothavarangaicurry,
                R.drawable.allepeyfishcurry,
                R.drawable.spicymadraschicken,
                R.drawable.prawns,
                R.drawable.keralachickenstew,
                R.drawable.nilgirichickenkorma,
                R.drawable.pepperchickencurry,
                R.drawable.chettiandchicken,
                R.drawable.prawnmasala,
                R.drawable.beeffry,
                R.drawable.crispyfish,
                R.drawable.amlapickle,
                R.drawable.avakaipickle,
                R.drawable.capsicumpickle,
                R.drawable.lemonpickle,
                R.drawable.onionpickle,
                R.drawable.mangopickle,
                R.drawable.tendermangopickle,
                R.drawable.tomatopickle,
                R.drawable.chillipickles,
                R.drawable.gingerpickle,
                R.drawable.garlicsoup,
                R.drawable.mixedvegsoup,
                R.drawable.chickenmanchowsoup,
                R.drawable.cleartomatosoup,
                R.drawable.sweetcornvegetablesoup,
                R.drawable.prawnandnoodlesoup,
                R.drawable.beetrootsoup,
                R.drawable.capsicumsoup,
                R.drawable.muttonsoup,
                R.drawable.spinachsoup,
                R.drawable.vegcurry,
                R.drawable.mushroommutter,
                R.drawable.alooparatha,
                R.drawable.daltadka,
                R.drawable.aloomethi,
                R.drawable.rajma,
                R.drawable.paneerbhujri,
                R.drawable.alooghobi,
                R.drawable.aloosabzi,
                R.drawable.kadhaipaneer,
                R.drawable.chickenwithbellpeppers,
                R.drawable.butterchicken,
                R.drawable.fishkorma,
                R.drawable.prawnfry,
                R.drawable.tandoorichicken,
                R.drawable.maccherjholfish,
                R.drawable.shamikabab,
                R.drawable.saagmeat,
                R.drawable.sindhichickenbiryani,
                R.drawable.punjabichickencurry,
                R.drawable.gulabjamun,
                R.drawable.basundi,
                R.drawable.peda,
                R.drawable.badhamhalwa,
                R.drawable.coconutburfi,
                R.drawable.kulfi,
                R.drawable.ladoo,
                R.drawable.mysorepak,
                R.drawable.ricekheer,
                R.drawable.badam,
                R.drawable.cauliflowersoup,
                R.drawable.cucumbersoup,
                R.drawable.tomatosoup,
                R.drawable.mulligatawnysoup,
                R.drawable.greenpeassoup,
                R.drawable.northindianmuttonsoup,
                R.drawable.chickennoodlesoup,
                R.drawable.spicybeansoup,
                R.drawable.eggdropsoup,
                R.drawable.springvegsoup,
                R.drawable.mushroomandseitan,
                R.drawable.tofuwith3spices,
                R.drawable.creamyvegpie,
                R.drawable.grilledvegmedley,
                R.drawable.bakedsquash,
                R.drawable.africanchickenstew,
                R.drawable.westafricanchicken,
                R.drawable.africanyamsoup,
                R.drawable.chickenchilliroast,
                R.drawable.chilliblackbeans,
                R.drawable.williecake,
                R.drawable.pumpkingingercupcakes,
                R.drawable.doublelayerpumpkincheesecake,
                R.drawable.carrotcake,
                R.drawable.sexcake,
                R.drawable.chocolatechipicecream,
                R.drawable.cookieicecream,
                R.drawable.cinnamonicecream,
                R.drawable.appleicecream,
                R.drawable.walnut,
                R.drawable.chineseaubergins,
                R.drawable.spicycucumber,
                R.drawable.chinesemushrooms,
                R.drawable.chinesegobi,
                R.drawable.spicytofu,
                R.drawable.chinesetilchicken,
                R.drawable.chickenmoongali,
                R.drawable.sweetsourchicken,
                R.drawable.cherrychicken,
                R.drawable.mymasalachicken,
                R.drawable.coldseasamenoodles,
                R.drawable.sobanoodles,
                R.drawable.noodleswithtofu,
                R.drawable.chinesevegandbeef,
                R.drawable.noodleswithpepper,
                R.drawable.jhingasoup,
                R.drawable.masalachickensoup,
                R.drawable.cabbagesoup,
                R.drawable.chinesepumpkinsoup,
                R.drawable.baconsoup,


                };



private LayoutInflater l_Inflater;

public ItemListBaseAdapter(Context context, ArrayList<Recipedetails> results) {
        itemDetailsrrayList = results;
        l_Inflater = LayoutInflater.from(context);
}

public int getCount() {
        return itemDetailsrrayList.size();
}

public Object getItem(int position) {
        return itemDetailsrrayList.get(position);
}

public long getItemId(int position) {
        return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
                convertView = l_Inflater.inflate(R.layout.item_details_view, null);
                holder = new ViewHolder();
                holder.txt_itemName = (TextView) convertView.findViewById(R.id.name);
                holder.txt_itemDescription = (TextView) convertView.findViewById(R.id.itemDescription);
                holder.itemImage = (ImageView) convertView.findViewById(R.id.photo);

                convertView.setTag(holder);
        } else {
                holder = (ViewHolder) convertView.getTag();
        }

        holder.txt_itemName.setText(itemDetailsrrayList.get(position).getName());
        holder.txt_itemDescription.setText(itemDetailsrrayList.get(position).getItemDescription());
        holder.itemImage.setImageResource(imgid[itemDetailsrrayList.get(position).getImageNumber() - 1]);
        return convertView;
}
public synchronized   void refreshAdapter(ArrayList<Recipedetails> items) {
    //itemDetailsrrayList.clear();
    itemDetailsrrayList = items;
   notifyDataSetChanged();
}

static class ViewHolder {
        TextView txt_itemName;
        TextView txt_itemDescription;
        ImageView itemImage;
}

}

我的 listactivity.java,添加了这些行

公共类 NorthIndian 扩展 Activity { ItemListBaseAdapter _itemListAdapter;

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ArrayList<Recipedetails> image_details = new ArrayList<Recipedetails>();

        _itemListAdapter = new ItemListBaseAdapter(this, image_details);

        final ListView lv1 = (ListView) findViewById(R.id.listV_main);
        lv1.setAdapter(_itemListAdapter);

        lv1.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> a, View v, int position, long id) { 
                Object o = lv1.getItemAtPosition(position);
                Recipedetails obj_itemDetails = (Recipedetails)o;
                Toast.makeText(NorthIndian.this, "You have chosen : " + " " + obj_itemDetails.getName(), Toast.LENGTH_LONG).show();

                switch(obj_itemDetails.getImageNumber())
                {
                    case 1:
                         Intent newActivity = new Intent(NorthIndian.this, 
                                 AndroidTabLayoutActivity4.class);     
                         startActivity(newActivity);
                        break;

                    case 2:
                        Intent new1Activity = new Intent(NorthIndian.this, 
                                AndroidTabLayoutActivity5.class);     
                        startActivity(new1Activity);
                        break;

                    case 5:
                        Intent new2Activity = new Intent(NorthIndian.this, 
                                AndroidTabLayoutActivity6.class);     
                        startActivity(new2Activity);
                        break; 

                    case 4:
                        Intent new3Activity = new Intent(NorthIndian.this, 
                                AndroidTabLayoutActivity7.class);     
                        startActivity(new3Activity);
                        break;      

                   default:
                       Toast.makeText(NorthIndian.this, "Wrong Input", Toast.LENGTH_LONG).show();
                }
            }  
        });

        refreshYourAdapter(GetSearchResults());
 }
 private void refreshYourAdapter(final ArrayList<Recipedetails> items) {
     //this is what I meant. The error clearly states you are not updating the adapter on the UI thread
     runOnUiThread(new Runnable() {
         public void run() {
             _itemListAdapter.refreshAdapter(items);
         }
     });
}


    private ArrayList<Recipedetails> GetSearchResults(){
        ArrayList<Recipedetails> results = new ArrayList<Recipedetails>();

        Recipedetails item_details = new Recipedetails();
        item_details.setName("Vegterian");
        item_details.setItemDescription("Recipes made by raw materials");
        item_details.setImageNumber(1);
        results.add(item_details);

        item_details = new Recipedetails();
        item_details.setName("Non-Vegterian");
        item_details.setItemDescription("Flesh of sweet animals");
        item_details.setImageNumber(2);
        results.add(item_details);

        item_details = new Recipedetails();
        item_details.setName("Sweets");
        item_details.setItemDescription("Tasty sweets made from indians");
        item_details.setImageNumber(5);
        results.add(item_details);

        item_details = new Recipedetails();
        item_details.setName("Soups");
        item_details.setItemDescription("Startup for our food");
        item_details.setImageNumber(4);
        results.add(item_details);




        return results;
    }

}

和 southindina.java

ublic class SouthIndian extends Activity {
ItemListBaseAdapter _itemListAdapter;
 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ArrayList<Recipedetails> image_details = new ArrayList<Recipedetails>();

        _itemListAdapter = new ItemListBaseAdapter(this, image_details);

        final ListView lv1 = (ListView) findViewById(R.id.listV_main);
        lv1.setAdapter(_itemListAdapter);

        lv1.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                        Object o = lv1.getItemAtPosition(position);
                        Recipedetails obj_itemDetails = (Recipedetails)o;
                        Toast.makeText(SouthIndian.this, "You have chosen : " + " " +      obj_itemDetails.getName(), Toast.LENGTH_LONG).show();

                        switch(obj_itemDetails.getImageNumber())
                        {
                            case 1:
                                 Intent newActivity = new Intent(SouthIndian.this,
                                                 AndroidTabLayoutActivity.class);    
                     startActivity(newActivity);
                                break;

                            case 2:
                                Intent new1Activity = new Intent(SouthIndian.this,
                                                AndroidTabLayoutActivity1.class);    
                            startActivity(new1Activity);
                                break;

                            case 3:
                                Intent new2Activity = new Intent(SouthIndian.this,
                                                AndroidTabLayoutActivity2.class);    
                            startActivity(new2Activity);
                                break;

                            case 4:
                                Intent new3Activity = new Intent(SouthIndian.this,
                                                AndroidTabLayoutActivity3.class);    
                            startActivity(new3Activity);
                                break;        

                           default:
                                   Toast.makeText(SouthIndian.this, "Wrong Input", Toast.LENGTH_LONG).show();
                        }

                }
        });

        refreshYourAdapter(GetSearchResults());
    }

    // THIS IS WHAT IT SHOULD LOOK LIKE MORE OR LESS. THIS IS AS MUCH AS I CAN HELP

    private void refreshYourAdapter(final ArrayList<Recipedetails> items) {
         //this is what I meant. The error clearly states you are not updating the adapter on the UI thread
         runOnUiThread(new Runnable() {
             public void run() {
                 _itemListAdapter.refreshAdapter(items);
             }
         });
    }

    private ArrayList<Recipedetails> GetSearchResults(){
        ArrayList<Recipedetails> results = new ArrayList<Recipedetails>();

        Recipedetails item_details = new Recipedetails();
        item_details.setName("Vegterian");
        item_details.setItemDescription("Recipes made by raw materials");
            item_details.setImageNumber(1);
        results.add(item_details);

        item_details = new Recipedetails();
        item_details.setName("Non-Vegterian");
        item_details.setItemDescription("Flesh of sweet animals");
        item_details.setImageNumber(2);
        results.add(item_details);

        item_details = new Recipedetails();
        item_details.setName("Pickels");
        item_details.setItemDescription("Touchable dish  by Homemade");
        item_details.setImageNumber(3);
        results.add(item_details);

        item_details = new Recipedetails();
        item_details.setName("Soups");
        item_details.setItemDescription("Startup for our food");
            item_details.setImageNumber(4);
        results.add(item_details);

        return results;
    }

}

在我的应用程序中添加所有链接后,同样的问题一次又一次地出现

请在此处查看我的代码以进一步说明http://pastebin.com/DXdvB5HC

AndroidTabLayoutActivity

public class AndroidTabLayoutActivity extends TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tabmain);

        TabHost tabHost = getTabHost();

        // Tab for Photos
        TabSpec photospec = tabHost.newTabSpec("Manual");
        photospec.setIndicator("Manual", getResources().getDrawable(R.drawable.icon_photos_tab));
        Intent photosIntent = new Intent(this, SouthIndianvegrecipes1.class);
        photospec.setContent(photosIntent);


        // Tab for Videos
        TabSpec videospec = tabHost.newTabSpec("Videos");
        videospec.setIndicator("Videos", getResources().getDrawable(R.drawable.icon_videos_tab));
        Intent videosIntent = new Intent(this, SouthIndianvegrecipes.class);
        videospec.setContent(videosIntent);

        // Adding all TabSpec to TabHost
        tabHost.addTab(photospec); // Adding photos tab
         // Adding songs tab 
        tabHost.addTab(videospec); // Adding videos tab
    }
}
4

2 回答 2

1

用这个替换你现有的课程

public class SouthIndian extends Activity {
ItemListBaseAdapter _itemListAdapter;
ArrayList<Recipedetails> results = new ArrayList<Recipedetails>();           
Recipedetails item_details = new Recipedetails();
ArrayList<Recipedetails> image_details = new ArrayList<Recipedetails>();
ListView lv1;
 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        lv1 = (ListView) findViewById(R.id.listV_main);
        lv1.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> a, View v, int position, long id) {

                        Recipedetails item_details = new Recipedetails();
                        item_details = results.get(position);
                        //Object o = lv1.getItemAtPosition(position);

                      //  Recipedetails obj_itemDetails = (Recipedetails)o;
                        //Toast.makeText(SouthIndian.this, "You have chosen : " + " " +      obj_itemDetails.getName(), Toast.LENGTH_LONG).show();

                        Toast.makeText(SouthIndian.this, "You have chosen : " + " " +      item_details.getName(), Toast.LENGTH_LONG).show();

                        switch(item_details.getImageNumber())
                        {
                            case 1:
                                 Intent newActivity = new Intent(SouthIndian.this,AndroidTabLayoutActivity.class);    
                                 startActivity(newActivity);
                                break;

                            case 2:
                                Intent new1Activity = new Intent(SouthIndian.this,AndroidTabLayoutActivity1.class);    
                                startActivity(new1Activity);
                                break;

                            case 3:
                                Intent new2Activity = new Intent(SouthIndian.this,AndroidTabLayoutActivity2.class);    
                                startActivity(new2Activity);
                                break;

                            case 4:
                                Intent new3Activity = new Intent(SouthIndian.this,AndroidTabLayoutActivity3.class);    
                                startActivity(new3Activity);
                                break;        

                           default:
                                   Toast.makeText(SouthIndian.this, "Wrong Input", Toast.LENGTH_LONG).show();
                        }

                }
        });


    }

    // THIS IS WHAT IT SHOULD LOOK LIKE MORE OR LESS. THIS IS AS MUCH AS I CAN HELP

    private void refreshYourAdapter(final ArrayList<Recipedetails> items) {
         //this is what I meant. The error clearly states you are not updating the adapter on the UI thread
         runOnUiThread(new Runnable() {
             public void run() {
                 _itemListAdapter.refreshAdapter(items);
             }
         });
    }

    private ArrayList<Recipedetails> GetSearchResults(){
        /*ArrayList<Recipedetails> results = new ArrayList<Recipedetails>();           
        Recipedetails item_details = new Recipedetails();*/

        item_details.setName("Vegterian");
        item_details.setItemDescription("Recipes made by raw materials");
            item_details.setImageNumber(1);
        results.add(item_details);

        item_details = new Recipedetails();
        item_details.setName("Non-Vegterian");
        item_details.setItemDescription("Flesh of sweet animals");
        item_details.setImageNumber(2);
        results.add(item_details);

        item_details = new Recipedetails();
        item_details.setName("Pickels");
        item_details.setItemDescription("Touchable dish  by Homemade");
        item_details.setImageNumber(3);
        results.add(item_details);

        item_details = new Recipedetails();
        item_details.setName("Soups");
        item_details.setItemDescription("Startup for our food");
            item_details.setImageNumber(4);
        results.add(item_details);

        return results;
    }

    @Override
    protected void onResume() {
        super.onResume();
        _itemListAdapter = new ItemListBaseAdapter(this, image_details);
        refreshYourAdapter(GetSearchResults());
        lv1.setAdapter(_itemListAdapter);

    }

}

有关通知数据的更多详细信息,请参见此处

于 2013-03-20T07:03:44.397 回答
0

首先,我不确定是否要在将适配器onCreate()设置到列表后立即更改适配器。据我了解,您可以从一开始就使用正确的数据创建适配器。例如,仅用于测试您可以通过以下方式进行:

new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                refreshYourAdapter(GetSearchResults());
            }
        }, 1000);

我建议尝试以下更改(但是,我无法完全运行您的代码):

ItemListBaseAdapter.java:

...
public synchronized void refreshYourAdapter(ArrayList<Recipedetails> items) {
    itemDetailsrrayList = items;
    notifyDataSetChanged();
}
...
于 2013-03-20T05:24:05.953 回答