編輯:關於Android編程
現在分析androidendorqcomopensourcemmapp2srccomcafmradioStationListActivity.java
protectedvoid onCreate(Bundle savedInstanceState)方法裡
綁定FMRadioService服務
bindService((newIntent()).setClass(this, FMRadioService.class), osc, 0);
實例化ListView對象
mStationList= (ListView) findViewById(R.id.station_list);
上下文菜單監聽事件
mStationList.setOnCreateContextMenuListener(this);
ListView監聽每個Item事件
mStationList.setOnItemClickListener(newOnItemClickListener()
調用mService.tune((int)((fFreq * 1000)));方法調頻
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.station_list); bindService((new Intent()).setClass(this, FMRadioService.class), osc, 0); mStationList = (ListView) findViewById(R.id.station_list); // mPresetList = new PresetList(StationList); mStationList.setOnCreateContextMenuListener(this); mStationList.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) { String freq = ((HashMap) mAdapter.getItem(arg2)) .get(freq); Float fFreq = Float.parseFloat(freq); if (mService != null) { try {mService.tune((int) ((fFreq * 1000))); finish(); } catch (RemoteException e) { e.printStackTrace(); } } else { Log.d(LOGTAG, mService is null........); } } }); }
上下文菜單
public void onCreateContextMenu(ContextMenumenu, View v,ContextMenuInfo menuInfo)
添加從命名和刪除功能
menu.add(0,CONTEXT_MENU_RENAME, 0, getString(R.string.preset_rename));
menu.add(0,CONTEXT_MENU_DELETE, 0, getString(R.string.preset_delete));
標題
menu.setHeaderTitle(getString(R.string.station_name)+getNameFromId(mItemId));
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { // TODO Auto-generated method stub AdapterContextMenuInfo mi = (AdapterContextMenuInfo) menuInfo; menu.add(0, CONTEXT_MENU_RENAME, 0, getString(R.string.preset_rename)); menu.add(0, CONTEXT_MENU_DELETE, 0, getString(R.string.preset_delete)); mItemId = mi.position; menu.setHeaderTitle(getString(R.string.station_name)+getNameFromId(mItemId)); }
protected void onPrepareDialog(int id, Dialog dialog, Bundle b) { // TODO Auto-generated method stub // super.onPrepareDialog(id, dialog); // After change system language, current function will be executed before // onResume , so execute load to ensure adapter is not null. load(); switch (id) { case DIALOG_RENAME_ID: mRenameDialog.setTitle(getString(R.string.station_name)+getNameFromId(mItemId)); final EditText editText = (EditText) mRenameDialog .findViewById(R.id.name); editText.setText(getNameFromId(mItemId)); Button bOk = (Button) mRenameDialog.findViewById(R.id.save); bOk.setOnClickListener(new View.OnClickListener() {@Override public void onClick(View v) { String rename = editText.getText().toString(); if (TextUtils.isEmpty(rename) || TextUtils.isEmpty(rename.trim())) { Context context = getApplicationContext(); Toast toast = Toast.makeText(context, getString(R.string.station_name_empty), Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL, 0, 0); toast.show(); } else if (stationNameExist(rename)) { Context context = getApplicationContext(); Toast toast = Toast.makeText(context, getString(R.string.station_name_exist, rename), Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL, 0, 0); toast.show(); } else { saveStationName(mItemId,rename); mRenameDialog.dismiss(); } } });Button bCancel = (Button) mRenameDialog.findViewById(R.id.cancel); bCancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub mRenameDialog.dismiss(); } }); break; case DIALOG_DELETE_ID: mDeleteDialog.setTitle(getString(R.string.station_list_delete_station, getNameFromId(mItemId))); TextView prompt = (TextView) mDeleteDialog.findViewById(R.id.prompt); prompt.setText(getString(R.string.station_list_delete_station_prompt,getNameFromId(mItemId))); Button bDelete = (Button) mDeleteDialog.findViewById(R.id.delete); bDelete.setOnClickListener(new View.OnClickListener() {@Override public void onClick(View v) { deleteStation(mItemId); mDeleteDialog.dismiss(); } }); Button bCancelDelete = (Button) mDeleteDialog.findViewById(R.id.cancel); bCancelDelete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mDeleteDialog.dismiss(); } }); break; } }
判斷電台名字是否存在
privateboolean stationNameExist(String name)
private boolean stationNameExist(String name) { for (HashMapitem : list) { if (item != null && name.equals(item.get(name))) { return true; } } return false; }
重命名保存方法
saveStationName(mItemId,rename);
mRenameDialog.dismiss();
private void saveStationName(int id, String name) { Integer stationIndex = mIndex.get(new Integer(id)); SharedPreferences sp = getSharedPreferences( FMRadio.SCAN_STATION_PREFS_NAME, 0); SharedPreferences.Editor editor = sp.edit(); editor .putString(FMRadio.STATION_NAME + (stationIndex.intValue()), name); editor.commit(); load(); }
刪除電台方法
deleteStation(mItemId);
mDeleteDialog.dismiss();
private void deleteStation(int id) { SharedPreferences sp = getSharedPreferences( FMRadio.SCAN_STATION_PREFS_NAME, 0); Integer stationIndex = mIndex.get(new Integer(id)); SharedPreferences.Editor editor = sp.edit(); editor.remove(FMRadio.STATION_NAME + (stationIndex)); editor.remove(FMRadio.STATION_FREQUENCY + (stationIndex)); editor.commit(); load(); }
通過調用SharedPreferences保存名字後加載 load();
protected void load() { list.clear(); mIndex.clear(); SharedPreferences sp = getSharedPreferences(FMRadio.SCAN_STATION_PREFS_NAME, 0); int station_number = sp.getInt(FMRadio.NUM_OF_STATIONS, 0); for (int i = 1; i <= station_number; i++) { HashMapitem = new HashMap (); String name = sp.getString(FMRadio.STATION_NAME + i, ); int frequency = sp.getInt(FMRadio.STATION_FREQUENCY + i, 0); if (!name.equals() && frequency != 0) { item.put(name, name); item.put(freq, String.valueOf(frequency / 1000.0f)); mIndex.put(new Integer(list.size()), new Integer(i)); list.add(item); } } mAdapter = new SimpleAdapter(this, list, R.layout.station_list_item, new String[] { name, freq }, new int[] { R.id.name, R.id.freq }); mStationList.setAdapter(mAdapter); }
方法
privatevoid saveStationName(int id, String name)
editor.putString(FMRadio.STATION_NAME+ (stationIndex.intValue()),
privatevoid deleteStation(int id) 刪除方法,刪除名字和頻率
editor.remove(FMRadio.STATION_NAME +(stationIndex));
editor.remove(FMRadio.STATION_FREQUENCY +(stationIndex));
以id獲取頻率方法
privateint getFrequencyFromId(int id)
private int getFrequencyFromId(int id) { String freq = ((HashMap) mAdapter.getItem(id)) .get(freq); Float fFreq = Float.parseFloat(freq); return (int) ((fFreq * 1000)); }
以Id獲取頻率名字方法
privateString getNameFromId(int id)
private String getNameFromId(int id) { String name = ((HashMap) mAdapter.getItem(id)) .get(name); return name; }
加載方法,清除ListView列表內容和索引id,從SharedPreferences裡獲取頻道個數
使用for循環將xml數據(電台名字和頻率)添加list列表裡使用SimpleAdapter將數據顯示在UI界面裡
protected void load() {
item.put(name, name);
item.put(freq,String.valueOf(frequency / 1000.0f));
mIndex.put(newInteger(list.size()), new Integer(i));
list.add(item);
}
mAdapter= new SimpleAdapter(this, list, R.layout.station_list_item,
new String[] {name, freq }, new int[] { R.id.name,
R.id.freq });
在StationListActivity類的onDestroy() 方法對FMRadioService服務解綁。
unbindService(osc);
protected void onDestroy() { unbindService(osc); mService = null; super.onDestroy(); }
android.support.v7.widget.RecyclerViewandroid.support.v7.widget.LinearLayoutManageran
本文實例講述了Android實現的ListView分組布局改進方法。分享給大家供大家參考,具體如下:由於是在網上轉載的一篇文章,在這裡就不多說廢話了,首先看一下最終的效果
首先上效果圖,類似於360檢測到騷擾電話頁面:布局很簡單,上面是一個RelativeLayout,下面一個Button.功能:(1)彈幕生成後自動從右側往左側滾動(Tra
需求:默認為00:00:00,點擊開始按鈕時清零後開始計時,出現如10:28:34。點擊停止的時候停止計時。問題:使用Calendar DateFormat的方法,不設置