package com.example.new_app;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.util.MonthDisplayHelper;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SimpleAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
Button bt1,bt2,bt3,bt4,gd;
mydb mdb;
ListView listView1;
SQLiteDatabase db;
ContentValues cv;
Handler hh;
ProgressBar zzz;
TextView title;
ArrayAdapter<String> ad;
ArrayList<String> ls;
int num;
//设置一个最大的数据条数,超过即不再加载
private int MaxDateNum;
// 最后可见条目的索引
private int lastVisibleIndex;
ArrayList<HashMap<String,String>> ll;
SimpleAdapter smad2;
android.support.v4.widget.SimpleCursorAdapter cu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt1=(Button)this.findViewById(R.id.button1);
bt2=(Button)this.findViewById(R.id.button2);
bt3=(Button)this.findViewById(R.id.button3);
bt4=(Button)this.findViewById(R.id.button4);
gd=(Button)this.findViewById(R.id.gd);
zzz=(ProgressBar)this.findViewById(R.id.zzz);
title=(TextView)this.findViewById(R.id.tltle);
listView1=(ListView)this.findViewById(R.id.listView1);
mdb= new mydb(this, "e2e.db", null, 1);
db=mdb.getWritableDatabase();
gd.setVisibility(View.INVISIBLE);
zzz.setVisibility(View.INVISIBLE);
num=0;
MaxDateNum=50;
//Cursor c=db.query(mdb.table_name, null, null, null, null, null, null);
String limit="10";
Cursor c=db.query(mdb.table_name, null, null, null, null, null, null, limit);
// ls=new ArrayList<String>();
ll=new ArrayList<HashMap<String,String>>();
hh= new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.arg1==2){
title.setText("翻页调用成功"+num);
gd.setVisibility(View.INVISIBLE);
zzz.setVisibility(View.INVISIBLE);
smad2.notifyDataSetChanged();
}
}
};
while(c.moveToNext()){
int id=c.getInt(c.getColumnIndex("_id"));
String tiele=c.getString(c.getColumnIndex("title"));
String content=c.getString(c.getColumnIndex("content"));
HashMap<String, String> h= new HashMap<String, String>();
h.put("title",id+tiele);
h.put("content",content);
h.put("id",String.valueOf(id));
ll.add(h);
Log.d("DB","id:"+id+" title:"+tiele+"content"+content );
}
String[] from= new String[]{"title","content","id"};
int[] to=new int[]{R.id.textView11,R.id.textView22,R.id.textView00};
smad2=new SimpleAdapter(this, ll, R.layout.l, from, to);
listView1.setAdapter(smad2);
//String[] from= {"title","content"};
//SimpleAdapter smad= new SimpleAdapter(this,ll,R.layout.l, new String[]{"title","content"}, new int[]{R.id.textView11,R.id.textView22});
// listView1.setAdapter(smad);
// ad = new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,ls );
//listView1.set
//listView1.setAdapter(ad);
cv = new ContentValues();
bt1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
cv.put("title", "哇咔咔"+Math.round(Math.random()));
cv.put("content", "哇咔咔222");
db.insert(mdb.table_name, null, cv);
}
});
bt2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
cv.put("title", "哇咔咔");
cv.put("content", "哇咔咔3333");
db.update(mdb.table_name, cv, null, null);
}
});
bt3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
db.delete(mdb.table_name, null, null);
}
});
bt4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Cursor c=db.query(mdb.table_name, null, null, null, null, null, null);
c.moveToFirst();
ll.clear();
while(c.moveToNext()){
int id=c.getInt(c.getColumnIndex("_id"));
String tiele=c.getString(c.getColumnIndex("title"));
String content=c.getString(c.getColumnIndex("content"));
HashMap<String, String> h= new HashMap<String, String>();
h.put("title",tiele);
h.put("content",content);
h.put("id",String.valueOf(id));
ll.add(h);
Log.d("DB","id:"+id+" title:"+tiele+"content"+content );
}
//SimpleAdapter smad=new SimpleAdapter(this, ll, R.layout.l, new String[]{"title","content"},new int[]{R.id.textView11,R.id.textView22});
smad2.notifyDataSetChanged();
}
});
//Toast.makeText(getBaseContext(), "OK"+"john"+"30",Toast.LENGTH_SHORT).show();
listView1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
HashMap<String, String> asd=ll.get(arg2);
Toast.makeText(getBaseContext(), "你选择了"+arg2+asd.get("title"), Toast.LENGTH_LONG).show();
//ll.get(arg2);
}
});
gd.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
gd.setVisibility(View.INVISIBLE);
zzz.setVisibility(View.VISIBLE);
Thread tt=new Thread(new Runnable() {
@Override
public void run() {
try {
uu();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.d("T", "出错、..");
} //执行的代码
}
});
tt.start(); //开始执行
}
});
listView1.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
// 滑到底部后自动加载,判断listview已经停止滚动并且最后可视的条目等于adapter的条目
//当不滚动时
if(scrollState == OnScrollListener.SCROLL_STATE_IDLE){
Log.d("T", "开始滚了。。。");
System.out.println(listView1.getLastVisiblePosition()+"==="+listView1.getCount());
//判断滚动到底部
if(listView1.getLastVisiblePosition()==(listView1.getCount()-1)){
//然后 经行一些业务操作
Log.d("T", "应该加载了..");
gd.setVisibility(View.VISIBLE);
}
}
// if (scrollState == OnScrollListener.SCROLL_STATE_IDLE
// && lastVisibleIndex == mSimpleAdapter.getCount()) {
// 当滑到底部时自动加载
// pg.setVisibility(View.VISIBLE);
// bt.setVisibility(View.GONE);
// handler.postDelayed(new Runnable() {
//
// @Override
// public void run() {
// loadMoreDate();
// bt.setVisibility(View.VISIBLE);
// pg.setVisibility(View.GONE);
// mSimpleAdapter.notifyDataSetChanged();
// }
//
// }, 2000);
// }
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
// 计算最后可见条目的索引
lastVisibleIndex = firstVisibleItem + visibleItemCount - 1;
// Log.d("T", "firstVisibleItem"+firstVisibleItem+"visibleItemCount"+visibleItemCount+"totalItemCount"+totalItemCount);
// 所有的条目已经和最大条数相等,则移除底部的View
if (totalItemCount == MaxDateNum + 1) {
// lv.removeFooterView(moreView);
Toast.makeText(getBaseContext(), "数据全部加载完成,没有更多数据!", Toast.LENGTH_LONG).show();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
protected void uu() throws InterruptedException {
for(int l=1;l<=8;l++){
HashMap<String, String> h= new HashMap<String, String>();
h.put("title","新加"+l);
h.put("content","新加"+l);
h.put("id",String.valueOf(l));
ll.add(h);
}
num++;
Thread.sleep(2000);
Message m2=Message.obtain();
m2.arg1=2;
hh.sendMessage(m2);
}
}