添加功能图标在首页显示-创新互联
主类:
public class MainActivity extends AppCompatActivity
{
private List
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 { 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; } } 另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
文章题目:添加功能图标在首页显示-创新互联
文章网址:http://hbruida.cn/article/djhhje.html