在做项目的时候,刚开始经常会看到要单独写adapter,可是不怎么会用也不怎么明白。就在网上找了一个例子,写了一下。希望能有帮助。
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text"
android:text="文字"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Add"
android:text="添加"/>
</LinearLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/list">
</ListView>
</LinearLayout>
addcontent.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"
android:orientation="vertical">
<!--android:visibility="gone"设置为隐藏-->
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@null"
android:gravity="top"
android:id="@+id/ettext"
android:hint="有了记事本,我再也不会忘记事情了"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="保存"/>
<Button
android:id="@+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="取消"/>
</LinearLayout>
</LinearLayout>
cell.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"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/list_content"
android:text="tv"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/list_time"
android:text="tv"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
select.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"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/s_tv"
android:layout_weight="1"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除"
android:id="@+id/s_delete"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="返回"
android:id="@+id/s_back"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
MainActivity.java
package com.example.admin_pc.adapter;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MainActivity extends Activity {
private Button textbtn,AddBtn;
private ListView lv;
private MyAdapter adapter;
private NotesDB notesDB;
private SQLiteDatabase dbReader;
private SQLiteDatabase dbWriter;
private Cursor cursor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
public void initView() {
lv = (ListView) findViewById(R.id.list);
textbtn = (Button) findViewById(R.id.text);
AddBtn = (Button) findViewById(R.id.Add);
notesDB = new NotesDB(this);
dbReader = notesDB.getReadableDatabase();
dbWriter = notesDB.getWritableDatabase();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
cursor.moveToPosition(position);
Intent i = new Intent(MainActivity.this, SelectActivity.class);
i.putExtra(NotesDB.ID,
cursor.getInt(cursor.getColumnIndex(NotesDB.ID)));
i.putExtra(NotesDB.CONTENT, cursor.getString(cursor
.getColumnIndex(NotesDB.CONTENT)));
i.putExtra(NotesDB.TIME,
cursor.getString(cursor.getColumnIndex(NotesDB.TIME)));
startActivity(i);
}
});
textbtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, AddContent.class);
startActivity(i);
}
});
AddBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
addDB();
selectDB();
}
});
}
public void selectDB() {
cursor = dbReader.query(NotesDB.TABLE_NAME, null, null, null, null,
null, null);
adapter = new MyAdapter(this, cursor);
lv.setAdapter(adapter);
}
@Override
protected void onResume() {
super.onResume();
selectDB();
}
public void addDB() {
ContentValues cv = new ContentValues();
cv.put(NotesDB.TIME, getTime());
cv.put(NotesDB.CONTENT, "Add添加内容");
dbWriter.insert(NotesDB.TABLE_NAME, null, cv);
}
private String getTime() {
SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
Date curDate = new Date();
String str = format.format(curDate);
return str;
}
}
SelectActivity.java
package com.example.admin_pc.adapter;
import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
/**
* Created by Admin-PC on 2015/3/2.
*
*/
public class SelectActivity extends Activity implements View.OnClickListener {
private Button s_delete, s_back;
private TextView s_tv;
private NotesDB notesDB;
private SQLiteDatabase dbWriter;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.select);
// System.out.println(getIntent().getIntExtra(NotesDB.ID, 0));
s_delete = (Button) findViewById(R.id.s_delete);
s_back = (Button) findViewById(R.id.s_back);
s_tv = (TextView) findViewById(R.id.s_tv);
notesDB = new NotesDB(this);
dbWriter = notesDB.getWritableDatabase();
s_back.setOnClickListener(this);
s_delete.setOnClickListener(this);
s_tv.setText(getIntent().getStringExtra(NotesDB.CONTENT));
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.s_delete:
deleteDate();
finish();
break;
case R.id.s_back:
finish();
break;
}
}
public void deleteDate() {
dbWriter.delete(NotesDB.TABLE_NAME,
"_id=" + getIntent().getIntExtra(NotesDB.ID, 0), null);
}
}
AddContent.java
package com.example.admin_pc.adapter;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.content.ContentValues;
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 android.widget.ImageView;
import android.widget.VideoView;
/**
* Created by Admin-PC on 2015/3/2.
*
*/
public class AddContent extends Activity implements OnClickListener {
private String val;
private Button savebtn, deletebtn;
private EditText ettext;
private NotesDB notesDB;
private SQLiteDatabase dbWriter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addcontent);
val = getIntent().getStringExtra("flag");
savebtn = (Button) findViewById(R.id.save);
deletebtn = (Button) findViewById(R.id.delete);
ettext = (EditText) findViewById(R.id.ettext);
savebtn.setOnClickListener(this);
deletebtn.setOnClickListener(this);
notesDB = new NotesDB(this);
dbWriter = notesDB.getWritableDatabase();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.save:
addDB();
finish();
break;
case R.id.delete:
finish();
break;
}
}
public void addDB() {
ContentValues cv = new ContentValues();
cv.put(NotesDB.CONTENT, ettext.getText().toString());
cv.put(NotesDB.TIME, getTime());
dbWriter.insert(NotesDB.TABLE_NAME, null, cv);
}
private String getTime() {
SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
Date curDate = new Date();
String str = format.format(curDate);
return str;
}
}
MyAdapter.java
package com.example.admin_pc.adapter;
import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
* Created by Admin-PC on 2015/3/3.
*
*/
public class MyAdapter extends BaseAdapter {
private Context context;
private Cursor cursor;
private LinearLayout layout;
public MyAdapter(Context context, Cursor cursor) {
this.context = context;
this.cursor = cursor;
}
@Override
public int getCount() {
return cursor.getCount();
}
@Override
public Object getItem(int position) {
return cursor.getPosition();
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
layout = (LinearLayout) inflater.inflate(R.layout.cell, null);
TextView contenttv = (TextView) layout.findViewById(R.id.list_content);
TextView timetv = (TextView) layout.findViewById(R.id.list_time);
cursor.moveToPosition(position);
String content = cursor.getString(cursor.getColumnIndex("content"));
String time = cursor.getString(cursor.getColumnIndex("time"));
contenttv.setText(content);
timetv.setText(time);
return layout;
}
}
NotesDB.java
package com.example.admin_pc.adapter;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by Admin-PC on 2015/3/2.
*
*/
public class NotesDB extends SQLiteOpenHelper {
public static final String TABLE_NAME = "notes";
public static final String CONTENT = "content";
public static final String ID = "_id";
public static final String TIME = "time";
public NotesDB(Context context) {
super(context, "notes", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + " (" + ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT," + CONTENT
+ " TEXT NOT NULL,"+ TIME + " TEXT NOT NULL)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}