SimpleCursorAdapter
从数据库中读取信息放到listview中的主要代码
xml:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_home"
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"
tools:context="com.example.guoqing.diary.home"
>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@android:drawable/ic_input_add"
android:id="@+id/imageButton"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true" />
<ListView
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_above="@+id/imageButton"
android:id="@+id/listview" />
</RelativeLayout>
layout.xml
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/title1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:textColor="#1D1D1C"
android:textSize="20sp"/>
<TextView
android:id="@+id/time1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:textColor="#B4B4B9"
android:textSize="14sp"/>
</LinearLayout>
</LinearLayout>
java:
public ListView listView;
private static final String DATABASE_NAME = "Mydb";
private static final String TABLE_NAME = "diary1";
private SQLiteDatabase sqlDB;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
sqlDB = openOrCreateDatabase(DATABASE_NAME, SQLiteDatabase.CREATE_IF_NECESSARY,null);
try {
sqlDB.execSQL("create table " + TABLE_NAME + " (_id text,title text ,weather text ,time text, context text)");
}catch(Exception e){
}
final Cursor c = sqlDB.query(TABLE_NAME, null, null, null, null, null, null);
final SimpleCursorAdapter listAdapter = new SimpleCursorAdapter(this,
R.layout.listviewlayout,
c,
new String[]{"title", "time"},
new int[]{R.id.title1, R.id.time1},
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(listAdapter);
listView.invalidateViews();
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, final long id) {
AlertDialog.Builder builder = new AlertDialog.Builder(home.this);
builder.setTitle("Prompt");
builder.setMessage("Do you want to delete?");
builder.setNegativeButton("Cancel",new DialogInterface.OnClickListener(){
public void onClick (DialogInterface arg0,int arg1)
{
}
});
builder.setPositiveButton("OK",new DialogInterface.OnClickListener(){
public void onClick(DialogInterface arg0,int arg1)
{
c.moveToPosition(position);
String title_name = c.getString(1);
sqlDB.delete(TABLE_NAME,"title=?",new String[]{title_name});
Intent i=new Intent(getApplicationContext(),home.class);
startActivity(i);
}
});
builder.show();
return true;
}
});
以上
新人练手,请大佬指教