xlistview上拉和加载重新刷新+数据库添加字段+webview
2017-08-24 14:54
417 查看
在Androidstudiode上进行简单的数据展示和刷新.
1.MainActvty主界面的内容
1.MainActvty主界面的内容
public class MainActivity extends AppCompatActivity implements XListView.IXListViewListener { private XListView xlist; private List<Bean.DataBean> list; int id=1; private String s; String path = "http://ic.snssdk.com/2/article/v25/stream/?category=news_tech&count=20&bd_latitude=4.9E-324&bd_longitude=4.9E-324&bd_loc_time=1457695555&loc_mode=5&lac=4527&cid=28883&iid=3835029558&device_id=12211880440&ac=wifi&channel=baidu&aid=13&app_name=news_article&version_code=460&device_platform=android&device_type=iToolsAVM&os_api="+id; private MyAdapter myAdapter; private Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); // 刷新数据 myAdapter.notifyDataSetChanged(); xlist.stopRefresh(); xlist.stopLoadMore(); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 初始化ID xlist = (XListView) findViewById(R.id.xlist); // 调用请求数据 indate(); // 刷新和下拉和监听 xlist.setPullRefreshEnable(true); xlist.setPullLoadEnable(true); xlist.setXListViewListener(this); // 点击某一新闻,通过webview加载详情页 xlist.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { Intent intent=new Intent(MainActivity.this,Main2Activity.class); intent.putExtra("url",list.get(position-1).getArticle_url()); startActivity(intent); return false; } }); } @Override public void onRefresh() { handler.postDelayed(new Runnable() { @Override public void run() { // 4000 实现上拉加载下一页,数据累加,url中os_api=1表示第一页 id++; Gson gson = new Gson(); Bean bean = gson.fromJson(s, Bean.class); list = bean.getData(); myAdapter = new MyAdapter(); xlist.setAdapter(myAdapter); list.addAll(list); } },2000); handler.sendEmptyMessage(1); } @Override public void onLoadMore() { handler.postDelayed(new Runnable() { @Override public void run() { //实现下拉刷新,数据重新加载 list.addAll(list); } },2000); handler.sendEmptyMessage(1); } //异步加载请求数据 public void indate() { new AsyncTask<String,Void,String>(){ private ByteArrayOutputStream outputStream; @Override protected String doInBackground(String... params) { try { URL url = new URL(params[0]); HttpURLConnection connetion= (HttpURLConnection) url.openConnection(); connetion.setRequestMethod("GET"); connetion.setConnectTimeout(3000); int responseCode = connetion.getResponseCode(); if(responseCode==200){ InputStream inputStream = connetion.getInputStream(); outputStream = new ByteArrayOutputStream(); int len; byte[] by=new byte[1024]; while ((len=inputStream.read(by))!=-1){ outputStream.write(by,0,len); } s = outputStream.toString(); } } catch (Exception e) { e.printStackTrace(); } return s; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); Gson gson = new Gson(); Bean bean = gson.fromJson(s, Bean.class); list = bean.getData(); // 添加数据库的foreach for (Bean.DataBean list2:list) { String title = list2.getTitle(); String suo = list2.getSource(); // dao包 Dao dao = new Dao(MainActivity.this); // 添加的方法 Boolean add = dao.add(title, suo); Toast.makeText(MainActivity.this,add+"",Toast.LENGTH_LONG).show(); } // 适配器,关联 myAdapter = new MyAdapter(); xlist.setAdapter(myAdapter); } }.execute(path); } // xlistview适配器 class MyAdapter extends BaseAdapter{ @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { convertView=View.inflate(MainActivity.this,R.layout.item,null); ImageView img= (ImageView) convertView.findViewById(R.id.img); TextView tv1= (TextView) convertView.findViewById(R.id.tv1); TextView tv2= (TextView) convertView.findViewById(R.id.tv2); //因为这网址的图片有问题所以下标必须是死值,否者就不能运行,打上注释是因为webview得是活值,那这就会报错 // ImageLoader.getInstance().displayImage(list.get(0).getImage_list().get(0).getUrl(),img); tv1.setText(list.get(position).getTitle()); tv2.setText(list.get(position).getSource()); return convertView; } } }
2.main.xml
<com.bawei.util.XListView android:id="@+id/xlist" android:layout_width="match_parent" android:layout_height="match_parent"></com.bawei.util.XListView>
3.item.xml
<ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/tv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="123" android:textSize="20dp"/> <TextView android:id="@+id/tv2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:text="123" android:layout_marginTop="10dp" android:textSize="15dp"/> </LinearLayout>
4.MainActvty2也就是webviewde接受值
public class Main2Activity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); // 初始化ID WebView web= (WebView) findViewById(R.id.web); // 接受值 Intent intent = getIntent(); String url = intent.getStringExtra("url"); // webview web.loadUrl(url); } }
5.webview.xml
<WebView android:id="@+id/web" android:layout_width="match_parent" android:layout_height="match_parent"></WebView>
6.数据库别忘了继承
public class SQLile extends SQLiteOpenHelper { public SQLile(Context context) { // 创建数据库 super(context,"dd.db",null,1); } @Override public void onCreate(SQLiteDatabase db) { //建表 db.execSQL("create table user(id integer primary key autoincrement," + "title varchar(50),suo varchar(50))"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
7.dao包添加方法
public class Dao { private final SQLiteDatabase database; //上下文 public Dao(Context context){ SQLile sqLile = new SQLile(context); // 具体方法 database = sqLile.getWritableDatabase(); } // 添加的方法 public Boolean add(String title,String suo){ ContentValues values = new ContentValues(); values.put("title",title); values.put("suo",suo); long insert = database.insert("user", null, values); if(insert!=-1){ return true; }else { return false; } } }
8.其他的数据库
8.1 修改
public Boolean update(String name,String nname) {
ContentValues values = new ContentValues();
values.put("name", name); int update = sd.update("user", values, "name = ?", new String[] { nname }); if (update != -1) { return true; } else { return false; } }
8.2 删除:
public Boolean delete(String name) {
int delete = sd.delete("user", "name = ?", new String[] {name} );
if (delete != -1) { return true; }else{ return false; } }
8.3 条件查询
public List<User> findone(String id){ List<User> list = new ArrayList<User>(); Cursor cursor = sd.query("user", null, "id = ?", new String[]{id}, null, null, null); while(cursor.moveToNext()){ String name = cursor.getString(cursor.getColumnIndex("name")); String age = cursor.getString(cursor.getColumnIndex("age")); User user = new User(); user.setName(name); user.setAge(age); list.add(user); } return list; }
8.4 查询全部:
public List<User> findall(){ List<User> list = new ArrayList<User>(); Cursor cursor = sd.query("user", null, null, null, null, null, null); while(cursor.moveToNext()){ String name = cursor.getString(cursor.getColumnIndex("name")); String age = cursor.getString(cursor.getColumnIndex("age")); User user = new User(); user.setName(name); user.setAge(age); list.add(user); } return list; } }
相关文章推荐
- listview、scollview、webview的下拉刷新,上拉加载。效果特别好,带刷新时间和图片
- RecyclerView使用详解一代替ListView(点击事件,添加头布局,上拉刷新下拉加载)
- android webview js交互之自定义错误加载界面(重新刷新)
- 打造Android集合控件数据绑定(支持添加监听,支持AbsListView与RecycleView,支持异步加载等)(一)基础篇
- 关于MVC使用Code-First代码优先来先建实体类中间添加新字段不需要重新建立数据库的方法
- webview加载页面及前进、后退、刷新操作
- 在ScollView中添加自定义的listView或者WebView 出现抢占焦点的问题
- Android WebView在屏幕旋转后,不重新加载URL及禁止横屏的方法
- 给WebView添加漂亮的加载进度条
- 使用PullToRefreshScrollView实现无线轮播和ListView同时刷新加载
- 黑马北京新闻项目连载(6)--->Gson解析、刷新加载、WebView展现
- React Native ListView上拉刷新,下拉加载刷新,并添加网络无数据时的缺省图
- 在ListView中添加item为WebView
- Android webview旋转屏幕导致页面重新加载问题解决办法
- WebView内使用post加载url并添加header
- webview添加网页加载进度条
- 实现应用WebView组件加载使用HTML代码添加的帮助信息
- Android Webview添加网页加载进度条实例详解
- WebView和ListView的交互使用,异步加载的问题
- PullRefreshListView添加addFooterView 添加emptyView 禁止上滑 禁止下滑 自动刷新( 动态刷新) 自动加载更多(动态加载更多)