我刚开始学习Android编程。 我有一个问题。如何创建自定义适配器以填充自定义ListView项目。
ListView商品代码:
item.xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/titleView"
... />
<TextView
android:id="@+id/dateView"
... />
</RelativeLayout>
DataBase Helper:
public class DataBase {
public static final String DB_NAME = "appdb";
public static final int DB_VERSION = 1;
static final String TABLE_NAME = "passtab";
final String LOG_TAG = "myLogs";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_TITLE = "title_enter";
public static final String COLUMN_LOGIN = "login_enter";
public static final String COLUMN_PASSWORD = "password_enter";
public static final String COLUMN_URL = "link_enter";
public static final String COLUMN_COMMENT = "comment_enter";
public static final String COLUMN_DATE = "date_enter";
public String results;
private DBHelper phdb;
private static Context pcontext;
private SQLiteDatabase pdb;
private static final String DB_CREATE =
"create table " + TABLE_NAME + "(" +
COLUMN_ID + " integer primary key autoincrement, " +
COLUMN_TITLE + " VARCHAR(255), " +
COLUMN_LOGIN + " VARCHAR(255), " +
COLUMN_PASSWORD + " VARCHAR(255), " +
COLUMN_URL + " VARCHAR(255), " +
COLUMN_COMMENT + " text, " +
COLUMN_DATE + " VARCHAR(255)" +
");";
public class DBHelper extends SQLiteOpenHelper{
public DBHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(DB_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
public DataBase(Context c) {
pcontext = c;
}
public DataBase open() throws SQLiteException {
phdb = new DBHelper(pcontext);
pdb = phdb.getWritableDatabase();
return this;
}
public SQLiteDatabase getDatabase() {
return pdb;
}
public void close() {
pdb.close();
}
...
public List<String> getData() {
String[] columns = new String[] { COLUMN_TITLE };
Cursor c = pdb.query(TABLE_NAME, columns, null, null, null, null,
null);
List<String> results = new ArrayList<String>();
int iCM = c.getColumnIndex(COLUMN_TITLE);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
results.add(c.getString(iCM));
}
return results;
}}
MainActivity:
public class MainScreen extends FragmentActivity implements OnClickListener {
ListView listViewMain;
SQLiteDatabase db;
DataBase DB;
DBHelper dbH;
Cursor cursor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_screen);
...
listViewMain = (ListView) findViewById(R.id.listViewMain);
DBHelper dbc = new DBHelper(MainScreen.this);
dbc.open();
String[] cs = new String[] { DataBase.COLUMN_TITLE };
Log.d(LOG_TAG, cs.toString());
listViewMain.setAdapter(new MySimpleAdapter<String>(this,
android.R.layout.item, cs));
dbc.close();
}
public class MySimpleAdapter extends SimpleAdapter {
public MySimpleAdapter(Context context,
List<? extends Map<String, ?>> data, int resource,
String[] from, int[] to) {
super(context, data, resource, from, to);
}
如何编写适配器并将其用于ListView?
答案 0 :(得分:0)
onCreate
方法:
List<String> results = DBHelper.getData();
listViewMain = (ListView) findViewById(R.id.listViewMain);
ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, results);
listViewMain.setListAdapter(adapter);