主類:
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;
}
}
另外有需要云服務器可以了解下創新互聯scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業上云的綜合解決方案,具有“安全穩定、簡單易用、服務可用性高、性價比高”等特點與優勢,專為企業上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
本文題目:添加功能圖標在首頁顯示-創新互聯
轉載注明:http://m.newbst.com/article24/djhhje.html
成都網站建設公司_創新互聯,為您提供App設計、面包屑導航、移動網站建設、域名注冊、虛擬主機、軟件開發
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯