編輯:關於Android編程
前言:因為找了N多網上的資源都沒有好的解決方案,別人都是只給思路沒給具體源碼,真TMD糾結,干嘛求別人,自己動手才是真,最痛恨那些所謂大牛的作風,給了點點代碼就讓別人去想,你讓我們這種小白情何於堪!!!!!!此例是基於listview來實現本地sqlite實現的!
二話不說,程序猿求的是有圖有真相有源碼!大家下載後有什麼問題可以找到本人;QQ508181017
核心代碼如下
1、數據庫操作類
package com.icq.demo.db; import java.util.ArrayList; import java.util.List; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import com.icq.demo.R; import com.icq.demo.beans.Item; public class BaseDBDao { private static String dbname=icq_db; private static GYMDatabase mdb; private static SQLiteDatabase db; private static String apppackage=com.icq.demo; private static int dbRec=R.raw.icq_db; /** * 獲取省 * @param context * @return */ public static List- getProvinces(Context context){ mdb = new GYMDatabase(context, dbname, apppackage, dbRec); mdb.openDatabase(); db = mdb.getDatabase(); List
- list = new ArrayList
- (); try { String sql = select * from province; Cursor cursor = db.rawQuery(sql, null); cursor.moveToFirst(); for (int i = 0; i < cursor.getCount(); i++) { String code = cursor.getString(cursor .getColumnIndex(ProvinceID)); String name = cursor.getString(cursor .getColumnIndex(ProvinceName)); Item myListItem = new Item(); myListItem.setName(name); myListItem.setId(code); list.add(myListItem); cursor.moveToNext(); } } catch (Exception e) { e.printStackTrace(); return null; } mdb.closeDatabase(); db.close(); return list; } /** * 根據省級id獲取城市列表 * @param pid * @param context * @return */ public static List
- getCitys(String pid,Context context) { mdb = new GYMDatabase(context, dbname, apppackage, dbRec); mdb.openDatabase(); db = mdb.getDatabase(); List
- list = new ArrayList
- (); try { String sql = select * from city where ProvinceID=' + pid + '; Cursor cursor = db.rawQuery(sql, null); cursor.moveToFirst(); for (int i = 0; i < cursor.getCount(); i++) { String code = cursor.getString(cursor.getColumnIndex(CityID)); String name = cursor.getString(cursor.getColumnIndex(CityName)); Item myListItem = new Item(); myListItem.setName(name); myListItem.setId(code); list.add(myListItem); cursor.moveToNext(); } } catch (Exception e) { e.printStackTrace(); } mdb.closeDatabase(); db.close(); return list; } /** * 根據城市id獲取城鎮 * @param cid * @param context * @return */ public static List
- getCountrys(String cid,Context context) { mdb = new GYMDatabase(context, dbname, apppackage, dbRec); mdb.openDatabase(); db = mdb.getDatabase(); List
- list = new ArrayList
- (); try { String sql = select * from district where CityID=' + cid + '; Cursor cursor = db.rawQuery(sql, null); cursor.moveToFirst(); for (int i = 0; i < cursor.getCount(); i++) { String code = cursor.getString(cursor.getColumnIndex(DistrictID)); String name = cursor.getString(cursor.getColumnIndex(DistrictName)); Item myListItem = new Item(); myListItem.setName(name); myListItem.setId(code); list.add(myListItem); cursor.moveToNext(); } } catch (Exception e) { e.printStackTrace(); } mdb.closeDatabase(); db.close(); return list; } }
public class ListAdapter extends BaseAdapter { private Context context; private List- myList; private LayoutInflater mInflater; private int selectedPosition = -1;// 選中的位置 public ListAdapter(Context context, List
- myList,int selected) { this.context = context; this.myList = myList; this.mInflater = LayoutInflater.from(this.context); selectedPosition=selected; } public int getCount() { return myList.size(); } public Object getItem(int position) { return myList.get(position); } public long getItemId(int position) { return position; } public void setSelectedPosition(int position) { selectedPosition = position; } public View getView(final int position, View convertView, final ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { holder = new ViewHolder(); convertView = mInflater.inflate(R.layout.block_list_item, null); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } final View cView=convertView; final ViewHolder cHolder=holder; holder.iLayout=(LinearLayout) convertView.findViewById(R.id.ll_root); holder.name = (TextView) convertView.findViewById(R.id.tv_name); holder.img = (ImageView) convertView.findViewById(R.id.iv_right); holder.name.setText(myList.get(position).getName()); System.out.println(Item值為:+selectedPosition); System.out.println(自帶的為:+position); if (selectedPosition == position) { holder.iLayout.setSelected(true); holder.iLayout.setPressed(true); holder.iLayout.setBackgroundColor(Color.RED); } else { holder.iLayout.setSelected(false); holder.iLayout.setPressed(false); holder.iLayout.setBackgroundColor(Color.TRANSPARENT); } return convertView; } class ButtonView { int layoutViewId; ButtonView(int tId) { layoutViewId = tId; } } public final class ViewHolder { public TextView name; public ImageView img; public LinearLayout iLayout; } }
public class ListViewShowActivity extends Activity{ private Context mContext; private ListView mLv_province,mLv_city,mLv_district; private ListAdapter mProvinceAdapter,mCityAdapter,mDistrictAdapter; private List- provinces,cities,districts; private String pId,cId,dId; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_listview); mContext=this; initView(); initData(); initOnItemClick(); } private void initOnItemClick() { mLv_province.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView adapter, View view, int position, long id) { mProvinceAdapter.setSelectedPosition(position); mProvinceAdapter.notifyDataSetInvalidated(); Item proItem=(Item) mProvinceAdapter.getItem(position); pId=proItem.getId(); //二級動 cities=BaseDBDao.getCitys(pId, mContext); mCityAdapter=new ListAdapter(mContext, cities,0); mCityAdapter.notifyDataSetChanged(); mLv_city.setAdapter(mCityAdapter); //三級動 districts=BaseDBDao.getCountrys(cities.get(0).getId(), mContext); mDistrictAdapter=new ListAdapter(mContext, districts,0); mDistrictAdapter.notifyDataSetChanged(); mLv_district.setAdapter(mDistrictAdapter); } }); mLv_city.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView adapter, View view, int position, long id) { mCityAdapter.setSelectedPosition(position); mCityAdapter.notifyDataSetInvalidated(); Item proItem=(Item) mCityAdapter.getItem(position); cId=proItem.getId(); districts=BaseDBDao.getCountrys(cId, mContext); mDistrictAdapter=new ListAdapter(mContext, districts,0); mDistrictAdapter.notifyDataSetChanged(); mLv_district.setAdapter(mDistrictAdapter); } }); mLv_district.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView adapter, View view, int position, long id) { mDistrictAdapter.setSelectedPosition(position); mDistrictAdapter.notifyDataSetInvalidated(); Item proItem=(Item) mDistrictAdapter.getItem(position); dId=proItem.getId(); } }); } private void initData() { provinces=BaseDBDao.getProvinces(mContext); mProvinceAdapter=new ListAdapter(mContext, provinces,0); mLv_province.setAdapter(mProvinceAdapter); cities=BaseDBDao.getCitys(provinces.get(0).getId(), mContext); mCityAdapter=new ListAdapter(mContext, cities,0); mLv_city.setAdapter(mCityAdapter); districts=BaseDBDao.getCitys(cities.get(0).getId(), mContext); mDistrictAdapter=new ListAdapter(mContext, districts,0); mLv_district.setAdapter(mDistrictAdapter); } private void initView() { mLv_province=(ListView) findViewById(R.id.list_province); mLv_city=(ListView) findViewById(R.id.list_city); mLv_district=(ListView) findViewById(R.id.list_district); } }
一、閒話: Android原生的進度條可以根據不同的主題有不同的視覺效果,但任何一種主題下的進度條和應用程序的視覺配合起來都顯得格格不入,所以多數時候我們需要自定義Pro
天天看高清影視片庫無法連接,不知道為什麼老是連接不上,下面是我總結了一下為什麼會這樣的問題,看能不能在裡面找到你們所需要的,幫你們解決天天看高清影視出現的問
RecyclerView出現已經有一段時間了,相信大家肯定不陌生了,大家可以通過導入support-v7對其進行使用。 據官方的介紹,該控件用於在有限的窗口中展示大量數據
怎樣才能寫出優秀的Android App,是每一個程序員追求的目標。那麼怎麼才能寫出一個優秀的App呢?相信很多初學者也會有這種迷茫。一句話來回答這個問題:細節很重要。今