PhoneNote
Posted 窗外的风景
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PhoneNote相关的知识,希望对你有一定的参考价值。
package com.example.phonenote;
import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Add extends Activity{
EditText edt;
Button btn;
OpenHelper openHelper;
SQLiteDatabase db=null;
public static final String DB_NAME = "DBTest";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add);
edt = (EditText)findViewById(R.id.editText);
openHelper = new OpenHelper(this, DB_NAME, null, 1);
btn=(Button)findViewById(R.id.btn_finish);
btn.setOnClickListener(btnFinishListener);
}
View.OnClickListener btnFinishListener = new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
InsertTb();
}
};
public void InsertTb() {
// TODO Auto-generated method stub
int flag = -1;
db = openHelper.getWritableDatabase();
String contentString = edt.getText().toString();
String sql = "insert into TestUsers(note)values(‘"+contentString+"‘)";
try {
db.execSQL(sql);
} catch (Exception e) {
// TODO: handle exception
Log.i("err", "insert failed");
flag = 0;
Toast.makeText(Add.this, "添加失败", Toast.LENGTH_SHORT).show();
}
db.close();
if (flag==-1) {
Toast.makeText(Add.this, "添加成功", Toast.LENGTH_SHORT).show();
}
}
public void onBackPressed(){
Intent intent = new Intent();
intent.setClass(Add.this, MainActivity.class);
startActivity(intent);
Add.this.finish();
}
}
package com.example.phonenote;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class Content extends Activity{
TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content);
tv = (TextView)findViewById(R.id.textView1);
Intent intent=getIntent();
String str=intent.getStringExtra("data");
tv.setText(str);
}
public void onBackPressed(){
Content.this.finish();
}
}
package com.example.phonenote;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
public class MainActivity extends Activity {
Button btnInsert;
ListView lvShowContent;
OpenHelper openHelper;
SQLiteDatabase db=null;
public static final String DB_NAME = "DBTest";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
openHelper = new OpenHelper(this, DB_NAME, null, 1);
btnInsert=(Button)findViewById(R.id.btn_add);
btnInsert.setOnClickListener(btnInsertListener);
lvShowContent=(ListView)findViewById(R.id.lv_showContent);
final List<String> data = new ArrayList<String>();
db = openHelper.getWritableDatabase();
String sql = "select note from TestUsers";
Cursor cursor = db.rawQuery(sql, null);//获取游标
int noteColumn = cursor.getColumnIndex("note");
for(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()){
String content = cursor.getString(noteColumn);
data.add(content);
}
cursor.close();
db.close();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,data);
lvShowContent.setAdapter(adapter);
lvShowContent.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
String s=data.get(arg2);
Intent intent = new Intent();
intent.setClass(MainActivity.this, Content.class);
intent.putExtra("data", s);
startActivityForResult(intent, 1);
}
});
}
View.OnClickListener btnInsertListener = new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
InsertTb();
}
};
public void InsertTb() {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(MainActivity.this, Add.class);
startActivity(intent);
MainActivity.this.finish();
}
}
package com.example.phonenote;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class OpenHelper extends SQLiteOpenHelper{
String sql = "create table if not exists TestUsers"+"(id int primary key,note varchar)";
public OpenHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/cg"
android:orientation="vertical"
tools:context=".MainActivity" >
<ListView
android:id="@+id/lv_showContent"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1.25" >
</ListView>
<Button
android:id="@+id/btn_add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/add" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/cg"
android:orientation="vertical" >
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.89"
android:ems="10"
android:hint="@string/no_data" >
<requestFocus />
</EditText>
<Button
android:id="@+id/btn_finish"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/finish" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@drawable/cg"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/no_data"
android:textSize="20sp" />
</LinearLayout>
以上是关于PhoneNote的主要内容,如果未能解决你的问题,请参考以下文章