Android小項目之--ListView與ListAcitivity完善論壇管理效果2(附源碼)
ListAcitivity 類型布局用來(lái)配置應(yīng)用程序,主要為顯示菜單列表、列表明細(xì)項(xiàng)目,假如讓程序繼承 ListActivity ,可以實(shí)現(xiàn)以下的方法:
ListAcitivity 默認(rèn)并不一定要像繼承自 Acitivity一樣,必須使用 seContentView 來(lái)設(shè)置版型 Layout 才能顯示頁(yè)面,ListAcitivity 可以在不必重寫(xiě) Protected void onCreate(Bundle savedInstanceState)的情況下,直接將列表加載至 ListAcitivity 中,非常便利,常用在如投票選項(xiàng)選擇、多個(gè)項(xiàng)目列表?xiàng)l列顯示、文件資源管理等。
今天此程序?qū)U(kuò)展上篇文章遺留下的問(wèn)題,完善整個(gè)實(shí)例的全部功能,需要完成的功能清單如下:
清單如上,下面是實(shí)例運(yùn)行效果圖:
這篇文章將不介紹如何加載數(shù)據(jù)給 ListView 具體操作你可以參考我這篇文章:http://www.cnblogs.com/TerryBlog/archive/2010/06/05/1752325.html,下面正式進(jìn)入代碼片段,由于代碼量相對(duì)比較大,這里就不全部發(fā)出代碼,感興趣的朋友可以到下面附件下載。 問(wèn)題處理:
public?void?setListAdapter?(ListAdapter?adapter)?
Since:?API?Level?1?
Provide?the?cursor?for?the?list?view.?
此方法,參數(shù)為ListAdapter,上篇我們是利用自己加載的 SimpleAdapter 的適配器做數(shù)據(jù)源,本篇因?yàn)槔^承了 ListAcitivity 所以數(shù)據(jù)源我們可以這樣寫(xiě),重寫(xiě) BaseAdapter ,具體看問(wèn)題2;
重寫(xiě) BaseAdapter 代碼如下:
代碼 @Override????public?int?getCount()?{
????????//?TODO?Auto-generated?method?stub
????????return?listitem.size();
????}
????@Override
????public?Object?getItem(int?arg0)?{
????????//?TODO?Auto-generated?method?stub
????????return?listitem.get(arg0);
????}
????@Override
????public?long?getItemId(int?arg0)?{
????????//?TODO?Auto-generated?method?stub
????????return?arg0;
????}
????@Override
????public?View?getView(int?arg0,?View?arg1,?ViewGroup?arg2)?{
????????//?TODO?Auto-generated?method?stub
????????final?ViewHolder?myViewHolder;
????????if(arg1==null)
????????{
????????????
????????????arg1=myInflater.inflate(R.layout.listview_row,?null);
????????????myViewHolder=new?ViewHolder();
????????
????????????ViewHolder.my_TextView=(TextView)arg1.findViewById(R.id.topTextView);
????????????ViewHolder.my_Button=(Button)arg1.findViewById(R.id.Button01);
????????????
????????????arg1.setTag(myViewHolder);
????????????
????????}
????????else
????????{
????????????myViewHolder=(ViewHolder)arg1.getTag();
????????}
????????ViewHolder.my_TextView.setText(listitem.get(arg0).get("itemText").toString());
????????ViewHolder.my_Button.setText(listitem.get(arg0).get("buttonText").toString());
????????ViewHolder.my_Button.setOnClickListener(new?OnClickListener()?{
????????????
????????????@Override
????????????public?void?onClick(View?v)?{
????????????????RelativeLayout?rl?=?(RelativeLayout)v.getParent();
????????????????final?TextView?subView?=?(TextView)rl.getChildAt(1);
????????????????//?TODO?Auto-generated?method?stub
????????????????new?AlertDialog.Builder(myContext)
????????????????.setTitle(R.string.confirm)
????????????????.setMessage(R.string.top)
????????????????.setPositiveButton(R.string.ok,?new?DialogInterface.OnClickListener()?{
????????????????????
????????????????????@Override
????????????????????public?void?onClick(DialogInterface?dialog,?int?which)?{
????????????????????????//?TODO?Auto-generated?method?stub
????????????????????????subView.setTextColor(Color.RED);
????????????????????}
????????????????})
????????????????.setNeutralButton(R.string.cancel,?null)
????????????????.show();
????????????}
????????});
????????
????????ViewHolder.my_TextView.setOnCreateContextMenuListener(new?OnCreateContextMenuListener()?{
????????????
????????????@Override
????????????public?void?onCreateContextMenu(ContextMenu?menu,?View?v,
????????????????????ContextMenuInfo?menuInfo)?{
????????????????//?TODO?Auto-generated?method?stub
????????????????ListViewActivity?lv=(ListViewActivity)myContext;
????????????????lv.myRelative=(RelativeLayout)v.getParent();
????????????????menu.setHeaderIcon(R.drawable.icon);
????????????????menu.setHeaderTitle(ViewHolder.my_TextView.getText());
????????????????menu.add(1,?0,?0,?"高亮");
????????????????menu.add(0,?1,?0,?"置頂");?
????????????}
????????????
????????});
????????
?????????
????????return?arg1;
????}
?
構(gòu)造函數(shù)為引入上篇上下文和數(shù)據(jù);
方法一:獲取數(shù)據(jù)源的總列數(shù);方法二:通過(guò)參數(shù)值獲取每個(gè)集合的項(xiàng);
方法三:獲得當(dāng)前項(xiàng)的索引;
方法四:返回我們需要的View。
一般來(lái)講,我們用 LayoutInflater 做一件事:inflate。inflate這個(gè)方法總共有四種形式,目的都是把xml 表述的layout 轉(zhuǎn)化為View。This class is used to instantiate layout XML file into its corresponding View objects . It is never be used directly -- use getLayoutInflater() or getSystemService(String)getLayoutInflater() or getSystemService(String) to retrieve a standard LayoutInflater instance that is already hooked up to the current context and correctly configured for the device you are running on
A.?Context.public abstract Object getSystemService (String name) :Return the handle to a system-level service by name. The class of the returned object varies by the requested name. 具體參見(jiàn)文檔。
B.種獲得LayoutInflater 的方法
(1)通過(guò)SystemService獲得LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
(2)從給定的context中獲取
C.?LayoutInflater.inflate()
將Layout文件轉(zhuǎn)換為View,顧名思義,專門(mén)供Layout使用的Inflater。雖然Layout也是View的子類,但在android中 如果想將xml中的Layout轉(zhuǎn)換為View放入.java代碼中操作,只能通過(guò)Inflater,而不能通過(guò)findViewById()。
D.findViewById() 有兩種形式
????? R.layout.xx 是引用res/layout/xx.xml的布局文件(inflate方法),R.id.xx是引用布局文件里面的組件,組件的id是xx...(findViewById方法)??纯?/span>R.java配置文件吧,R對(duì)文件分類管理,多寫(xiě)幾個(gè)layout.xml后你會(huì)發(fā)現(xiàn),所有的組件id都能用R.id.xx來(lái)查看,但是組件不在setContentView()里面的layout中就無(wú)法使用,Activity.findViewById()會(huì)出現(xiàn)空指針異常 。
詳細(xì)使用步驟如下:
?
?之后我們?cè)?
public?View?getView(int?arg0,?View?arg1,?ViewGroup?arg2)?{?
方法里面調(diào)用方法參數(shù)View?arg1使用
arg1=myInflater.inflate(R.layout.listview_row,?null);?
?
即可得到相應(yīng)的 View ,得到 View 之后即可以輕松的使用 findViewById() 方法操作控件,并為且設(shè)置對(duì)應(yīng)的監(jiān)聽(tīng)事件。
4.上篇文章我們留下了一個(gè)問(wèn)題,即如何在長(zhǎng)按時(shí)能夠得到長(zhǎng)按的某一項(xiàng),并得到項(xiàng)操作相應(yīng)的業(yè)務(wù)邏輯,本篇將完成此功能。在繼承 ListAcitivity 類里聲明 public RelativeLayout myRelative; 此相對(duì)布局控件來(lái)源于布局XML的布局控件,之后在設(shè)置長(zhǎng)按事件的處理函數(shù)中進(jìn)行如下操作:
ListViewActivity?lv=(ListViewActivity)myContext;????????????????lv.myRelative=(RelativeLayout)v.getParent();
?
上述代碼為,將上下文引入,得到上下文聲明的相對(duì)布局控件,因?yàn)榉椒ǖ膮?shù)View 為相應(yīng)的點(diǎn)擊控件,為了得到布局控件,使用View.getParent() 得到相對(duì)的父控件,這里我用的是 TextView 它放在 RelativeLayout 此控件的下方法,故為getParent(),得到每次點(diǎn)擊項(xiàng)的布局控件后,再重寫(xiě)onContextItemSelected ,代碼如下:
代碼 public?boolean?onContextItemSelected(MenuItem?item)?{????????//?TODO?Auto-generated?method?stub
????????switch?(item.getItemId())?{
????????case?0:
????????????ViewHolder.my_TextView=(TextView)myRelative.getChildAt(1);
????????????ViewHolder.my_TextView.setBackgroundColor(Color.BLUE);
????????????return?true;
????????case?1:
????????????ViewHolder.my_TextView=(TextView)myRelative.getChildAt(1);
????????????Toast.makeText(this,?ViewHolder.my_TextView.getText()+"添加置頂效果",?1000).show();
????????????return?true;
?????????
????????}
????????return?super.onContextItemSelected(item);
????}
?
?
這里有必要解釋一下,這一段實(shí)現(xiàn)菜單的選擇功能,應(yīng)該放在 Activity 代碼類里面,因?yàn)樯厦嫖覀兺ㄟ^(guò)在此類中聲明一個(gè) RelativeLayout 然后當(dāng)點(diǎn)擊時(shí)我們即為它賦值,點(diǎn)擊完某一菜單按鈕后即返回 Activity 頁(yè)面,得到布局,我們?cè)谶@里要實(shí)現(xiàn)的只有通過(guò)操作相對(duì)布局控件里面包含的控件然后進(jìn)行操作即可完全實(shí)現(xiàn) 這一功能,在布局控件里面找控件可用此方法 getChildAt(int index);注意上面條件分支語(yǔ)句不是跳出而是返回一個(gè)真。
到此,此功能己經(jīng)得到實(shí)現(xiàn),在此要感謝以下兩人對(duì)我的幫助:QQ:182041935---QQ120460734
?
源碼下載:/Files/TerryBlog/listViewDemo.rar
?
如果你有什么疑問(wèn)或者建議 你可以? QQ285735942 或 Email:terryyhl@gmail.com
?轉(zhuǎn)載于:https://www.cnblogs.com/TerryBlog/archive/2010/06/07/1753572.html
總結(jié)
以上是生活随笔為你收集整理的Android小項目之--ListView與ListAcitivity完善論壇管理效果2(附源碼)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: MFC 控件布局
- 下一篇: Android 目录