实现收藏夹

原创 2016年05月30日 12:31:23

话不到说,先上代码
布局activity_main.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#f8f8f8" >   
    <TextView
    android:text="网站名称"
    android:id="@+id/TextView01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"></TextView>
    <EditText
    android:text=""
    android:id="@+id/EditTextName"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"></EditText>
    <TextView
    android:text="URL"
    android:id="@+id/TextView02"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"></TextView>
    <EditText
    android:text=""
    android:id="@+id/EditTextURL"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"></EditText>
    <TextView
    android:text="网站描述"
    android:id="@+id/TextView03"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"></TextView>
    <EditText
    android:text=""
    android:id="@+id/EditTextDesc"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:height="100px"></EditText>
    <Button
    android:text="添加"
    android:id="@+id/ButtonAdd"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"></Button>      
</LinearLayout>  

row.xml

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView
    android:id="@+id/text0"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingRight="10px"></TextView>
     <TextView
    android:id="@+id/text1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingRight="10px"></TextView>
     <TextView
    android:id="@+id/text2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingRight="10px"></TextView>
     <TextView
    android:id="@+id/text3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingRight="10px"/>
</LinearLayout>

DBHelper.java

package com.collect.activity;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

import com.collect.tools.DBHelper;
import com.example.point.R;

public class QueryActivity extends ListActivity {
      @Override
      protected void onCreate(Bundle savedInstanceState) {
       // TODO Auto-generated method stub
       super.onCreate(savedInstanceState);
       this.setTitle("浏览收藏信息");
       final DBHelper helpter = new DBHelper(this);
       Cursor c= helpter.query();
       String[] from = {"_id","name","url","desc"};
       int[] to = {R.id.text0,R.id.text1,R.id.text2,R.id.text3};
       SimpleCursorAdapter adapter = 
        new SimpleCursorAdapter(this, R.layout.row, c, from, to);
       ListView listView = getListView();
       listView.setAdapter(adapter);
       final AlertDialog.Builder builder = new AlertDialog.Builder(this);

       listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
              int arg2, long arg3) {
             // TODO Auto-generated method stub
             final long temp = arg3;
             builder.setMessage("真的要删除吗").
             setPositiveButton("是", new DialogInterface.OnClickListener() {

              @Override
              public void onClick(DialogInterface dialog, int which) {
               // TODO Auto-generated method stub
               helpter.del((int)temp);
               /*Cursor c = helpter.query();--y有错误
               String[] from = {"_id","naem","url","desc"};
               int[] to ={R.id.text0,R.id.text1,R.id.text2,R.id.text3};*/
               Cursor c= helpter.query();
               String[] from = {"_id","name","url","desc"};
               int[] to = {R.id.text0,R.id.text1,R.id.text2,R.id.text3};
               SimpleCursorAdapter adapter = 
                new SimpleCursorAdapter(getApplicationContext(), R.layout.row, c, from, to);
               ListView listView = getListView();
               listView.setAdapter(adapter);
              }
             }).setNegativeButton("否", new DialogInterface.OnClickListener() {

              @Override
              public void onClick(DialogInterface dialog, int which) {
               // TODO Auto-generated method stub 
              }
             });
             AlertDialog ad  = builder.create();
             ad.show();
            }   
       });
      }
}

MainActivity.java

package com.collect.activity;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.collect.tools.DBHelper;
import com.example.point.R;

public class MainActivity extends Activity {
     private EditText et1,et2,et3;
     private Button b1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.setTitle("添加收藏信息");

        DBHelper helper = new DBHelper(getApplicationContext());
        SQLiteDatabase db=helper.getWritableDatabase();
        db.close();

        et1 = (EditText)findViewById(R.id.EditTextName);
        et2 = (EditText)findViewById(R.id.EditTextURL);
        et3 = (EditText)findViewById(R.id.EditTextDesc);
        b1 = (Button)findViewById(R.id.ButtonAdd);
        b1.setOnClickListener(new OnClickListener() {   
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                String name = et1.getText().toString();
                String url = et2.getText().toString();
                String desc = et3.getText().toString();
                ContentValues values = new ContentValues();
                values.put("name", name);
                values.put("url", url);
                values.put("desc", desc);
                DBHelper helper = new DBHelper(getApplicationContext());
                helper.insert(values);
                Intent intent = new Intent(MainActivity.this, QueryActivity.class);
                startActivity(intent);   
       }});  
    }
}

QueryActivity.java

package com.collect.activity;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

import com.collect.tools.DBHelper;
import com.example.point.R;

public class QueryActivity extends ListActivity {
      @Override
      protected void onCreate(Bundle savedInstanceState) {
       // TODO Auto-generated method stub
       super.onCreate(savedInstanceState);
       this.setTitle("浏览收藏信息");
       final DBHelper helpter = new DBHelper(this);
       Cursor c= helpter.query();
       String[] from = {"_id","name","url","desc"};
       int[] to = {R.id.text0,R.id.text1,R.id.text2,R.id.text3};
       SimpleCursorAdapter adapter = 
        new SimpleCursorAdapter(this, R.layout.row, c, from, to);
       ListView listView = getListView();
       listView.setAdapter(adapter);
       final AlertDialog.Builder builder = new AlertDialog.Builder(this);

       listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
              int arg2, long arg3) {
             // TODO Auto-generated method stub
             final long temp = arg3;
             builder.setMessage("真的要删除吗").
             setPositiveButton("是", new DialogInterface.OnClickListener() {

              @Override
              public void onClick(DialogInterface dialog, int which) {
               // TODO Auto-generated method stub
               helpter.del((int)temp);
               /*Cursor c = helpter.query();--y有错误
               String[] from = {"_id","naem","url","desc"};
               int[] to ={R.id.text0,R.id.text1,R.id.text2,R.id.text3};*/
               Cursor c= helpter.query();
               String[] from = {"_id","name","url","desc"};
               int[] to = {R.id.text0,R.id.text1,R.id.text2,R.id.text3};
               SimpleCursorAdapter adapter = 
                new SimpleCursorAdapter(getApplicationContext(), R.layout.row, c, from, to);
               ListView listView = getListView();
               listView.setAdapter(adapter);
              }
             }).setNegativeButton("否", new DialogInterface.OnClickListener() {

              @Override
              public void onClick(DialogInterface dialog, int which) {
               // TODO Auto-generated method stub 
              }
             });
             AlertDialog ad  = builder.create();
             ad.show();
            }   
       });
      }
}

项目文件目录
这里写图片描述
效果图
这里写图片描述
这里写图片描述
这里写图片描述

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

全站ajax.net实现的在线99收藏夹,大家共享!

====================================================== 注:本文源代码点此下载 =============================...

web开发设为首页、添加到收藏夹实现方法

web开发设为首页、添加到收藏夹实现方法

sqlite之增删改查--简单的实现iOS收藏夹功能

sqlite实现简单收藏功能

js 实现 单击加入收藏夹

  • 2010年03月30日 15:00
  • 255B
  • 下载

将猎豹浏览器的收藏夹转移到Firefox的书签

本人之前一直使用Chrome,后来因为常常重装系统,Chrome的同步功能不能直接使用。这个很蛋疼,就换用了猎豹浏览器,用了很长时间,但是近期一直在做前端发开,Firefox的开发者功能当然不用说是最...

js 实现 进入页面弹出收藏夹

  • 2010年03月30日 14:39
  • 2KB
  • 下载

android实现收藏夹

  • 2015年01月13日 22:38
  • 1.72MB
  • 下载

配置Windows(Win7)导航面板(Navigation Pane)隐藏/显示收藏夹、库、网络等

Windows浏览器左侧的导航面板默认项目比较多,使用起来很不方便,通常我希望打开窗口时,左侧的导航面板只显示“库”和“计算机”,这里介绍一下隐藏(或者是关闭)导航面板的中各类项目的方法:首先,下列所...

教你如何拷贝IE浏览器的网址收藏夹

教你如何拷贝IE浏览器的网址收藏夹 很多人在网上冲浪的时候,浏览到好的网址都喜欢收藏,一般收藏到IE浏览器的收藏夹中,日积月累,浏览器的收藏夹里就有很多很有用...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:实现收藏夹
举报原因:
原因补充:

(最多只允许输入30个字)