您好,登錄后才能下訂單哦!
主類:
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;
}
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。