如何在网格视图的第n个位置更改图像资源,需要根据收到的消息进行更改我该怎么做?

时间:2016-11-18 07:05:22

标签: android android-studio

我需要根据收到的消息更改图像资源。我尝试在数据库中进行更改它将工作但是要查看更改活动要刷新,刷新时间连接断开连接。对此有何解决方案?我可以在receive()上获得图像的位置..任何人都可以提供解决方案吗?

public void onDataReceived(String message,String ip){

        / /--------------------------//


                try
                {
                    sqlitedatabase = openOrCreateDatabase(DatabaseName, Context.MODE_PRIVATE, null);
                    Cursor allrows = sqlitedatabase.rawQuery("SELECT * FROM "+UserConstruct.image_list.TableName+" WHERE "+UserConstruct.image_list.Device_ID+ "=="+fxddid+"", null);         

                    if (allrows.moveToFirst()) {
                        do
                        {

                            String iconname1 = allrows.getString(1);
                            String imgidpos1 = allrows.getString(0);
                            iconnamefxd=iconname1;
                            imgidpos=Integer.parseInt(imgidpos1);

                        } while (allrows.moveToNext());
                    }
                    allrows.close();
                    db.close();
                }
                catch (Exception e)
                {
                    Toast.makeText(getApplicationContext(), "",
                            Toast.LENGTH_LONG);
                }

                Toast.makeText(getApplicationContext(), iconnamefxd+"and"+imgidpos, Toast.LENGTH_SHORT).show();

                    String on="NO";
                    TextView ststs=(TextView)findViewById(R.id.txtonof);
                    String a="00";


                    if(iconnamefxd.equals("0"))

                    {
                        sqlitedatabase = openOrCreateDatabase(DatabaseName, Context.MODE_PRIVATE, null);
                      String query="UPDATE "+UserConstruct.image_list.TableName+" SET "+UserConstruct.image_list.Image_Name_fxd+"='00' WHERE "+UserConstruct.image_list.Id+" = "+imgidpos+"";
                        sqlitedatabase.execSQL(query);

                      call();

                        Toast.makeText(getBaseContext(), "clear", Toast.LENGTH_LONG).show();





                    }
                   else if(iconnamefxd.equals("00"))
                    {
                        sqlitedatabase = openOrCreateDatabase(DatabaseName, Context.MODE_PRIVATE, null);
                        String query="UPDATE "+UserConstruct.image_list.TableName+" SET "+UserConstruct.image_list.Image_Name_fxd+"='0' WHERE "+UserConstruct.image_list.Id+" = "+imgidpos+"";
                        sqlitedatabase.execSQL(query);

                        call();

                    }


                }
                else if
                {-----------------}





 class DrawerListAdapter extends BaseAdapter
    {

        Context mContext;
        ArrayList<NavItem> mNavItems;

        public DrawerListAdapter(Context context, ArrayList<NavItem> navItems)
        {
            mContext = context;
            mNavItems = navItems;
        }

        @Override
        public int getCount() {
            return mNavItems.size();
        }

        @Override
        public Object getItem(int position) {
            return mNavItems.get(position);

        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view;

            if (convertView == null) {
                LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = inflater.inflate(R.layout.itemgridlayout, null);
            }
            else {
                view = convertView;
            }

            TextView letterText = (TextView) view.findViewById(R.id.item_title);
            TextView dvcid=(TextView)view.findViewById(R.id.txtid);
            ImageView iconView = (ImageView) view.findViewById(R.id.item_img);
            TextView sttus=(TextView)findViewById(R.id.txtonof);


            //ImageView iconView = (ImageView) view.findViewById(R.id.icon);
            dvcid.setText(mNavItems.get(position).DvceId);

            letterText.setText(mNavItems.get(position).IconName);
            String fx=mNavItems.get(position).Icon;
            if(fx.equals("0"))
            {iconView.setImageResource(R.drawable.off1);}
            else if(fx.equals("1"))
            {--------------}



  public void call()
    {
        try {

       ArrayList<NavItem> mNavItems = new ArrayList<NavItem>();
        getTableValues(mNavItems);
        try {

            final DrawerListAdapter im = new DrawerListAdapter(this, mNavItems);
            im.notifyDataSetChanged();
            gridview.invalidateViews();
        }catch (NullPointerException e){e.printStackTrace();}

2 个答案:

答案 0 :(得分:0)

您可以将消息参数传递给适配器,然后根据它设置资源。更改资源后调用notifyDataSetChanged。

答案 1 :(得分:0)

执行以下代码

 NavItem navItem=(NavItem)adapter.getItem(position); 
 navIte,.setImageId(newId);
 adapter.notifyDatasetChanged();