当前位置:  编程技术>移动开发
本页文章导读:
    ▪童贞apk纠结过的技术点<2&gt        处女apk纠结过的技术点<2>                                                          android  json 解析 android自带json解析类,和java的使用有那么点区别,主要在.........
    ▪ ListView中加下加FootView和点击按钮动态加载listview        ListView中加上加FootView和点击按钮动态加载listview package hbq.android.myapp.listview; import android.app.ListActivity; import android.os.Bundle; import android.util.Log; import android.view.Gravity; import android.view.View; import and.........
    ▪ listView 不同ltem展示不同的数据       listView 不同ltem显示不同的数据 请下载附件 ......

[1]童贞apk纠结过的技术点<2&gt
    来源: 互联网  发布时间: 2014-02-18
处女apk纠结过的技术点<2>

                                                         android  json 解析

android自带json解析类,和java的使用有那么点区别,主要在实例化的时候 看代码

public static JSONArray jsonAnalyze(String result) {  
  JSONObject jsonObj;
  try {
   jsonObj = new JSONObject(result);//直接实例化
   String suc = jsonObj.getString("success");
   Log.i("success", suc);

   int count = jsonObj.getInt("totalCount");
   Log.i("totalCount", String.valueOf(count));
   JSONArray jobjData = jsonObj.getJSONArray("rows");    //解析获取数组
   return jobjData;
  } catch (JSONException e1) {
   e1.printStackTrace();
   return null;
  }
 }


    
[2] ListView中加下加FootView和点击按钮动态加载listview
    来源: 互联网  发布时间: 2014-02-18
ListView中加上加FootView和点击按钮动态加载listview
package hbq.android.myapp.listview;


import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.LinearLayout.LayoutParams;
/**
 * 
 * @author huangbq
 *
 */
public class ButtonLoadingActivity extends ListActivity    {
	private static final String TAG = "ButtonLoadingActivity";
       
    private listViewAdapter adapter = new listViewAdapter();
    private ListView listView ;
    /**
	 * 设置布局显示的属性
	 */
    private LayoutParams mLayoutParams =new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
    /**
	 * 设置布局显示目标最大化属性
	 */
    private LayoutParams FFlayoutParams =new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT);
    
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
             Log.i(TAG, "onCreate(Bundle savedInstanceState)>>>>>>>>>>>>>>>" );
              LinearLayout layout = new LinearLayout(this);
              layout.setOrientation(LinearLayout.HORIZONTAL);
             
              Button button = new Button(this);
              button.setText("点击加载下10条...");
              button.setGravity(Gravity.CENTER_VERTICAL);
               
              layout.addView(button,FFlayoutParams);
              layout.setGravity(Gravity.CENTER);
              LinearLayout loadingLayout = new LinearLayout(this);
              loadingLayout.addView(layout,mLayoutParams);
              loadingLayout.setGravity(Gravity.CENTER);
               
               
              listView = getListView();
               
              listView.addFooterView(loadingLayout);
               
              button.setOnClickListener(new Button.OnClickListener()   
              {         @Override      
                 public void onClick(View v)  
              {         
                  adapter.count += 10;  
                  Log.i(TAG, "setOnClickListener:" +  adapter.count);
                  adapter.notifyDataSetChanged();
				  int currentPage=adapter.count/10;
				  Toast.makeText(getApplicationContext(), "第"+currentPage+"页", Toast.LENGTH_LONG).show();
                  }      
              });  
             
          setListAdapter(adapter);  
     }  

    class listViewAdapter extends BaseAdapter {
        int count = 10; /* starting amount */

        public int getCount() { return count; }
        public Object getItem(int pos) { return pos; }
        public long getItemId(int pos) { return pos; }

        public View getView(int pos, View v, ViewGroup p) {
			Log.i(TAG, "getView>>>pos:" + pos);
			TextView view;
			if (v==null) {
				view = new TextView(ButtonLoadingActivity.this);
			}
			else {
				view=(TextView)v;
			}
			view.setText("ListItem " + pos);
			view.setTextSize(20f);
			view.setGravity(Gravity.CENTER);
			view.setHeight(60);
			return view;
         }
     }



}

 2.用OnScrollListener来监听是不是最后一条项目来加载

 

package hbq.android.myapp.listview;

import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AbsListView.OnScrollListener;
import android.widget.LinearLayout.LayoutParams;
/**
 * 
 * @author huangbq
 *
 */
public class MainActivity extends ListActivity implements OnScrollListener {
	
	private static final String TAG = "MainActivity";
	private listViewAdapter adapter = new listViewAdapter();
	ListView listView ;
	private int lastItem = 0;
	LinearLayout loadingLayout;
	/**
	 * 设置布局显示属性
	 */
    private LayoutParams mLayoutParams =new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
    /**
	 * 设置布局显示目标最大化属性
	 */
    private LayoutParams FFlayoutParams =new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT);
    
	private ProgressBar progressBar;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Log.i(TAG, "onCreate(Bundle savedInstanceState)>>>>>>>>>>>>>>>" );
	    //线性布局
		LinearLayout layout = new LinearLayout(this);
	   //设置布局 水平方向
		layout.setOrientation(LinearLayout.HORIZONTAL);
		 //进度条
		progressBar = new ProgressBar(this);
		 //进度条显示位置
		progressBar.setPadding(0, 0, 15, 0);
		//把进度条加入到layout中
		layout.addView(progressBar, mLayoutParams);
		//文本内容
		TextView textView = new TextView(this);
		textView.setText("加载中...");
		textView.setGravity(Gravity.CENTER_VERTICAL);
		//把文本加入到layout中
		layout.addView(textView, FFlayoutParams);
		//设置layout的重力方向,即对齐方式是
		layout.setGravity(Gravity.CENTER);
		
		//设置ListView的页脚layout
		loadingLayout = new LinearLayout(this);
		loadingLayout.addView(layout, mLayoutParams);
		loadingLayout.setGravity(Gravity.CENTER);
		loadingLayout.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (adapter.count<=41) {
					adapter.count += 10;
					adapter.notifyDataSetChanged();

				}else{
					listView.removeFooterView(loadingLayout); 
				}
			}
		});
		
		//得到一个ListView用来显示条目
		listView = getListView();
		//添加到脚页显示
		listView.addFooterView(loadingLayout);
		//给ListView添加适配器
		setListAdapter(adapter);
		//给ListView注册滚动监听
		listView.setOnScrollListener(this);
	}
	@Override
	public void onScroll(AbsListView v, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		Log.i(TAG , "Scroll>>>first: " + firstVisibleItem + ", visible: " + visibleItemCount + ", total: " + totalItemCount);
		lastItem = firstVisibleItem + visibleItemCount - 1;
		Log.i(TAG , "Scroll>>>lastItem:" + lastItem);
		//显示50条ListItem,即0-49,因为onScroll是在“滑动”执行过之后才触发,所以用adapter.count<=41作条件
		/*if (adapter.count<=41) {
			if (firstVisibleItem+visibleItemCount==totalItemCount) {
				adapter.count += 10;
				adapter.notifyDataSetChanged();
				listView.setSelection(lastItem);
				int currentPage=adapter.count/10;
				Toast.makeText(getApplicationContext(), "第"+currentPage+"页", Toast.LENGTH_LONG).show();
			}
		}
		else {
			 listView.removeFooterView(loadingLayout); 
		}
		*/

		
	}
	@Override
	public void onScrollStateChanged(AbsListView v, int state) {
		if (lastItem == adapter.count && state == OnScrollListener.SCROLL_STATE_IDLE) {
		/*	Log.i(TAG,"ScrollStateChanged>>>state:"+state+"lastItem:" + lastItem);
			//显示50条ListItem,即0-49,因为onScrollStateChanged是在“拖动滑动”执行过之后才触发,所以用adapter.count<=41作条件
			if (adapter.count<=41) {
				adapter.count += 10;
				adapter.notifyDataSetChanged();

			}else{
				listView.removeFooterView(loadingLayout); 
			}
*/

		}
	}
  /**
   * 要用用于生成显示数据
   * @author huangbq
   *
   */
	class listViewAdapter extends BaseAdapter {
		int count = 10;

		public int getCount() {
			Log.i(TAG, "getCount>>>count:" + count);
			return count;
		}

		public Object getItem(int pos) {
			Log.i(TAG, "getItem>>>pos:" + pos);
			return pos;
		}

		public long getItemId(int pos) {
			Log.i(TAG, "getItemId>>>ItemId:" + pos);
			return pos;
		}

		public View getView(int pos, View v, ViewGroup p) {
			Log.i(TAG, "getView>>>pos:" + pos);
			TextView view;
			if (v==null) {
				view = new TextView(MainActivity.this);
			}
			else {
				view=(TextView)v;
			}
			view.setText("ListItem " + pos);
			view.setTextSize(20f);
			view.setGravity(Gravity.CENTER);
			view.setHeight(60);
			return view;
		}
	}
}

 


    
[3] listView 不同ltem展示不同的数据
    来源: 互联网  发布时间: 2014-02-18
listView 不同ltem显示不同的数据

请下载附件


    
最新技术文章:
▪Android开发之登录验证实例教程
▪Android开发之注册登录方法示例
▪Android获取手机SIM卡运营商信息的方法
▪Android实现将已发送的短信写入短信数据库的...
▪Android发送短信功能代码
▪Android根据电话号码获得联系人头像实例代码
▪Android中GPS定位的用法实例
▪Android实现退出时关闭所有Activity的方法
▪Android实现文件的分割和组装
▪Android录音应用实例教程
▪Android双击返回键退出程序的实现方法
▪Android实现侦听电池状态显示、电量及充电动...
▪Android获取当前已连接的wifi信号强度的方法
▪Android实现动态显示或隐藏密码输入框的内容
▪根据USER-AGENT判断手机类型并跳转到相应的app...
▪Android Touch事件分发过程详解
▪Android中实现为TextView添加多个可点击的文本
▪Android程序设计之AIDL实例详解
▪Android显式启动与隐式启动Activity的区别介绍
▪Android按钮单击事件的四种常用写法总结
▪Android消息处理机制Looper和Handler详解
▪Android实现Back功能代码片段总结
▪Android实用的代码片段 常用代码总结
▪Android实现弹出键盘的方法
▪Android中通过view方式获取当前Activity的屏幕截...
▪Android提高之自定义Menu(TabMenu)实现方法
▪Android提高之多方向抽屉实现方法
unix/linux知识 iis7站长之家
▪Android提高之MediaPlayer播放网络视频的实现方法...
▪Android提高之手游转电视游戏的模拟操控
 


站内导航:


特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

©2012-2021,,E-mail:www_#163.com(请将#改为@)

浙ICP备11055608号-3