ListView -- BaseAdapter的使用

ListView -- BaseAdapter的使用_第1张图片

通过继承实现一个BaseAdapter类,实现上述的ListView。

1.首先设置一个布局文件sim_record.xml,其中只包含了一个ListView组件。



     
 	


 

2.因为每一个item需要显示的是标题和内容。所以初始化俩个List用以存储item的标题和内容。

       List SimInforTitle = new ArrayList();
        List SimInforSummary = new ArrayList();

 

首先初始化SimInforTitle,即需要显示列表中的标题:Card ID / Data/ Sim State 等。。

	private List initSimInforTitle(List SimInforTitle){  //SimInforTitle 
    	Log.d(TAG," initSimInforTitle()");
    	SimInforTitle.add(getString(R.string.siminfor_title_cardid));
    	SimInforTitle.add(getString(R.string.siminfor_title_date));
    	SimInforTitle.add(getString(R.string.siminfor_title_simstate));
    	SimInforTitle.add(getString(R.string.siminfor_title_simoperator));
    	SimInforTitle.add(getString(R.string.siminfor_title_simcountry));
    	SimInforTitle.add(getString(R.string.siminfor_title_datastate));
    	SimInforTitle.add(getString(R.string.siminfor_title_networktype));
    	return SimInforTitle;
	
    }

 

然后初始化SimInforSummary,从card_id从数据库中读取该卡的具体信息,即需要显示列表中的具体内容:

SimInforSummary = mSimRecordResolver.querySimInfor(card_id); 

 

	public List querySimInfor(String card_id){
		String where = "card_id='"+card_id+"'";   // where = '123456'
		Cursor mCursor = mContentResolver.query(Uri.parse(CONTENT_URI), null, where, null, null);
	    	List SimInforSummart = new ArrayList();
		if(mCursor.moveToFirst()){
			//mMap.put("id", Integer.toString(mCursor.getInt(ID)));
			SimInforSummart.add(mCursor.getString(ID_CARD_ID));
			SimInforSummart.add(mCursor.getString(ID_DATE));
			SimInforSummart.add(convertSimState(mCursor.getString(ID_SIM_STATE)));
			SimInforSummart.add(mCursor.getString(ID_SIM_OPEARTOR));
			SimInforSummart.add(mCursor.getString(ID_SIM_COUNTRY));
			SimInforSummart.add(convertDataState(mCursor.getString(ID_DATA_STATE)));
			SimInforSummart.add(convertNetworkType(mCursor.getString(ID_NETWORK_TYPE)));
					
			Log.d(TAG," displayList() id="+Integer.toString(mCursor.getInt(ID)));
			Log.d(TAG," displayList() cardId="+mCursor.getString(ID_CARD_ID));
			Log.d(TAG," displayList() ID_DATE=" +mCursor.getString(ID_DATE));
			Log.d(TAG," displayList() ID_SIM_STATE="+ convertSimState(mCursor.getString(ID_SIM_STATE)));
			Log.d(TAG," displayList() ID_SIM_OPEARTOR="+ mCursor.getString(ID_SIM_OPEARTOR));
			Log.d(TAG," displayList() ID_SIM_COUNTRY="+ mCursor.getString(ID_SIM_COUNTRY));
			Log.d(TAG," displayList() ID_DATA_STATE="+ convertDataState(mCursor.getString(ID_DATA_STATE)));
			Log.d(TAG," displayList() ID_NETWORK_TYPE="+ convertNetworkType(mCursor.getString(ID_NETWORK_TYPE)));	
			return SimInforSummart;
		}
		else{
			
			Log.d(TAG," querySimInfor()**where = " +where+ "  .No Sim information");
			return null;
		}
	}

 

3.初始化ArrayList> 类mList。

将每一个map值(即每一个item列表)存储在mList中,而map的值是通过SimInforTitle和SimInforSummary获取。

最后为ListView设置适配器(mBaseAdapter),即显示出item列表内容。

    public void displaySimInfo(String card_id,List SimInforTitle,List SimInforSummary){
    	Log.d(TAG," displaySimInfo() ");
    	ListView mListView = (ListView)findViewById(R.id.mlistview);
    	List> mList = new ArrayList> ();
    	mList.clear();
    	SimInforSummary = mSimRecordResolver.querySimInfor(card_id); //mMapDataBase stored the sim card information
    	
    	Log.d(TAG," displaySimInfo() -- SimInforSummary.size() = "+SimInforSummary.size());
    	for(int i=0; i mMap = new HashMap();
    		mMap.put("title", SimInforTitle.get(i));
    		mMap.put("summary", SimInforSummary.get(i));
    		mList.add(mMap);
    	}
    	MyBaseAdapter mBaseAdapter = new  MyBaseAdapter(this);
    	mListView.setAdapter(mBaseAdapter);
    }



 

4.继承实现BaseAdapter类

最重要的是覆写BaseAdapter类中的getView()方法。

首先将sim_infor_listview.xml布局文件转换为组件view:convertView = mInflater.inflate(R.layout.sim_infor_listview, null);

然后通过 ArrayList>类mList将每一个item内容给设置上去。

 

 private class MyBaseAdapter extends BaseAdapter{
    	private LayoutInflater mInflater;
    	
		public MyBaseAdapter(Context context) {
			Log.d(TAG," MyBaseAdapter()");
			this.mInflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			Log.d(TAG," MyBaseAdapter() -- getCount()=" + mList.size());
			return  mList.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			Log.d(TAG," MyBaseAdapter() -- getItem()");
			return null;
		}

		@Override
		public long getItemId(int itemId) {  //return the list item id (0-6)
			// TODO Auto-generated method stub
			Log.d(TAG," MyBaseAdapter() -- getItemId()="+itemId);
			return itemId;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			Log.d(TAG,"MyBaseAdapter -- getView()  position=" +position + "; convertView = "+ convertView +"; parent=" +parent);
			ViewHolder mViewHolder =null;
			if(convertView == null){
				
				convertView = mInflater.inflate(R.layout.sim_infor_listview, null);
				mViewHolder = new ViewHolder();
				mViewHolder.setTitle((TextView)convertView.findViewById(R.id.text_title));
				mViewHolder.setSummary((TextView)convertView.findViewById(R.id.text_summary));
				convertView.setTag(mViewHolder);
			}
			else{
				mViewHolder=(ViewHolder)convertView.getTag();
			}
			
			mViewHolder.title.setText(mList.get(position).get("title").toString());
			mViewHolder.summary.setText(mList.get(position).get("summary").toString());
			return convertView;
		}
    	
    }


 

 private  class ViewHolder{
 
     	public TextView title;
    	public TextView summary;
		public TextView getTitle() {
			return title;
		}
		public void setTitle(TextView title) {
			this.title = title;
		}
		public TextView getSummary() {
			return summary;
		}
		public void setSummary(TextView summary) {
			this.summary = summary;
		}
    }


sim_info_listview.xml布局文件,即每一个item列表的布局


    
   	
   	


 

 

 

 

你可能感兴趣的:(Android-app)