温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

添加功能图标在首页显示

发布时间:2020-08-14 18:17:34 来源:网络 阅读:366 作者:鹭明 栏目:开发技术

添加功能图标在首页显示




主类:


public class MainActivity extends AppCompatActivity

{

    private List<MyData> mList =new ArrayList<MyData>();

    private SharedPreferences sp;

    private MyAdapter myAdapter;







    @Override

    protected void onCreate ( Bundle savedInstanceState )

    {

        super.onCreate ( savedInstanceState );

        setContentView ( R.layout.activity_main );

        initListView ( );

        initData();


    }




    //上下文菜单

    @Override

    public void onCreateContextMenu ( ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo )

    {

        menu.add ( "删除" );


    }





    @Override

    public boolean onContextItemSelected ( MenuItem item )

    {


        AdapterView.AdapterContextMenuInfo menuInfo = ( AdapterView.AdapterContextMenuInfo ) item.getMenuInfo ( );

        int position = menuInfo.position;

        MyData myData = mList.get ( position );

        String name = myData.getName ( );

        mList.remove ( position );

        SharedPreferences.Editor edit = sp.edit ( );

        edit.putInt ( name, 0);

        edit.commit ();

        myAdapter.notifyDataSetChanged ();

        return true;


    }




    private void initData ( )

    {


        sp =  getSharedPreferences("data",0);

        mList.clear ();

        for (int i=0;i<Constant.TT.length;i++)

        {


            int anInt = sp.getInt ("0"+i,0 );

            if(anInt!=0)

            {


                mList.add ( new MyData ("0"+i,anInt ) );

            }

        }

        myAdapter.notifyDataSetChanged ();


    }




    private void initListView ( )

    {

        ListView mListView = ( ListView ) findViewById ( R.id.lv );

        myAdapter =  new MyAdapter();

        mListView.setAdapter ( myAdapter );

        registerForContextMenu (mListView);


    }


    




    class MyAdapter extends BaseAdapter

    {





        @Override

        public int getCount ( )

        {

            return mList.size ();

        }


        @Override

        public Object getItem ( int position )

        {

            return null;

        }


        @Override

        public long getItemId ( int position )

        {

            return 0;

        }


        @SuppressLint ( "ViewHolder" )

        @Override

        public View getView ( int position, View convertView, ViewGroup parent )

        {


            View layout = getLayoutInflater ( ).inflate ( R.layout.mainlistview, null );


            ImageView img = ( ImageView ) layout.findViewById ( R.id.mlv_img );

            TextView tv = ( TextView ) layout.findViewById ( R.id.mlv_tv );

            if(mList.size ()>position)

            {


                MyData mydata = mList.get ( position );

                int imgId=mydata.getImgId ();

                if ( imgId!= 0)

                {


                    img.setVisibility ( View.VISIBLE );

                    tv.setVisibility ( View.VISIBLE );

                    img.setImageResource (  imgId );

                    tv.setText ( mydata.getName () );

                }else

                {

                    img.setVisibility ( View.GONE );

                    tv.setVisibility ( View.GONE );

                }


            }




            return layout;

        }


    }





    @Override

    protected void onActivityResult ( int requestCode, int resultCode, Intent data )

    {



        initData();

        super.onActivityResult ( requestCode, resultCode, data );



    }




    public void btnClick ( View v )

    {

        Intent intent = new Intent ( this, SecondActivity.class );

        startActivityForResult ( intent, Constant.requestCode );

    }



}


接口:

interface Constant

{

    public final static int requestCode=01;


    public final static int[] TT={R.drawable.w1,R.drawable.w2,R.drawable.w3,

            R.drawable.w4,R.drawable.w5, R.drawable.w6,

            R.drawable.w7,R.drawable.w8,R.drawable.w9,

            R.drawable.w10, R.drawable.w11};


    public final static String[] SS={"01","02","03","04","05","06","07","08","09","10","11"};


}


添加功能界面类:

public class SecondActivity extends AppCompatActivity

{



    private SharedPreferences sp;


    @Override

    protected void onCreate(Bundle savedInstanceState)

    {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_second);

        initGridView();


    }


    //找GridView控件

    private void initGridView ( )

    {


         sp =  getSharedPreferences ( "data", 0 );

        GridView gv = ( GridView ) findViewById ( R.id.second_gv );

        MyGridAdapter myGridAdapter = new MyGridAdapter();

        gv.setAdapter ( myGridAdapter );



    }



    class MyGridAdapter extends BaseAdapter

    {


        @Override

        public int getCount ( )

        {


            return Constant.TT.length;

        }


        @Override

        public Object getItem ( int position )

        {


            return null;

        }


        @Override

        public long getItemId ( int position )

        {


            return 0;

        }


        @SuppressLint ( "ViewHolder" )

        @Override

        public View getView ( final int position, View convertView, ViewGroup parent )

        {



            View layout = getLayoutInflater ().inflate ( R.layout.secondgridview,null );

            ImageView img = ( ImageView ) layout.findViewById ( R.id.sgv_img );

            CheckBox cb = ( CheckBox ) layout.findViewById ( R.id.sgv_cb );

            img.setImageResource ( Constant.TT[position] );

            final int p=position;

            cb.setOnClickListener ( new View.OnClickListener ( )

            {

                @Override

                public void onClick ( View v )

                {


                    SharedPreferences.Editor edit = sp.edit ( );

                    edit.putInt ( "0"+p ,Constant.TT[p]);

                    edit.commit ();


                }

            } );


            return layout;

        }



    }



    //返回首页页面

    public void btnBack(View v)

    {


        Intent intent = new Intent ( );

        setResult ( Constant.requestCode,intent );

        finish ();


    }


}



数据保存对象类:

public class MyData

{

    private String name;

    private int imgId;



    public MyData ( String name , int imgId )

    {



        this.name = name;

        this.imgId = imgId;

    }



    public String getName ( )

    {


        return name;

    }



    public int getImgId ( )

    {


        return imgId;

    }


    public void setName ( String name )

    {


        this.name = name;

    }



    public void setImgId ( int imgId )

    {


        this.imgId = imgId;

    }

}


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI