您的位置:首页 > 移动开发 > Android开发

[Learn Android Studio 汉化教程]Reminders实验:第一部分(续)

2016-08-15 10:13 1666 查看
最后,有两个删除方法。第一个针对特定的提醒用 id参数和数据库对象来生成和执行一条删除语句。第二个方法需要数据库生成并执行一条删除语句来移除所有表单里的提醒。

这时,你需要一个手段从数据库提出提醒并放入到ListView。清单5-13演示了必要的逻辑,通过继承之前你看到的特别的Adapter Android类来绑定数据库值到单独的行对象。创建一个新类RemindersSimpleCursorAdapter在com.apress.gerber.reminders包下,并完成处理代码。当解析导入类时,使用android.support.v4.widget.SimpleCursorAdapter
class类。
Listing 5-13. RemindersSimpleCursorAdapter Code
public class RemindersSimpleCursorAdapter extends SimpleCursorAdapter {
public RemindersSimpleCursorAdapter(Context context, int layout, Cursor c, String[]
from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
}
//to use a viewholder, you must override the following two methods and define a ViewHolder class
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return super.newView(context, cursor, parent);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
super.bindView(view, context, cursor);
ViewHolder holder = (ViewHolder) view.getTag();
if (holder == null) {
holder = new ViewHolder();
holder.colImp = cursor.getColumnIndexOrThrow(RemindersDbAdapter.COL_IMPORTANT);
holder.listTab = view.findViewById(R.id.row_tab);
view.setTag(holder);
}
if (cursor.getInt(holder.colImp) > 0) {
holder.listTab.setBackgroundColor(context.getResources().getColor(R.color.orange));
} else {
holder.listTab.setBackgroundColor(context.getResources().getColor(R.color.green));
}
}
static class ViewHolder {
//store the column index
int colImp;
//store the view
View listTab;
}
}

我们用适配器把所有提醒登记到ListView。在运行时中,ListView将重复调用在适配器里的bindView()方法,以单独的示图对象作为用户装载器,且在清单里滚动。填入这些清单条目到示图里是适配器的工作。在这个例子代码里,我们使用了适配器的子类SimpleCursorAdapter。这个类用了一个游标对象,它保存着跟踪表单里的行轨迹。
这里你看到了一个ViewHolder模式的例子。这是一个容易认识的Android模式,每个ViewHolder对象含有一个示图的标签。用数据源(在这个例子里是Cursor)的值,这个对象加载清单里的示图对象。ViewHolder定义为一个内部静态类,有两个实例变量,一个用于索引重要的表单项另一个用于在布局里定义的row_tab示图。
bindView()方法开始于父类在示图里从游标到元素的map值方法的调用。然后检查看(holder)是否附有一个标签还是有必要创建一个新的(holder)。然后bindView()方法用重要列索引和早先定义的row_tab配置容器的实例变量。在容器被发现或配置后,从当前提醒的COL_IMPORTANT常量秋决定row_tab用什么颜色。这个例子用了新的橙色,那个你要加到colors.xml:
<color name="orange">#ffff381a</color>。
早先你用了ArrayAdapter来管理模型和示图之间的关系。SimpleCursorAdapter也是用同样的模式,虽然它的模型是SQLite数据库。将清单5-14更改到你的RemindersDbAdaper和RemindersSimpleCursorAdapter类里。
Listing 5-14. RemindersActivity Code
public class RemindersActivity extends ActionBarActivity {
private ListView mListView;
private RemindersDbAdapter mDbAdapter;
private RemindersSimpleCursorAdapter mCursorAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reminders);
mListView = (ListView) findViewById(R.id.reminders_list_view);
mListView.setDivider(null);
mDbAdapter = new RemindersDbAdapter(this);
mDbAdapter.open();
Cursor cursor = mDbAdapter.fetchAllReminders();
//from columns defined in the db
String[] from = new String[]{
RemindersDbAdapter.COL_CONTENT
};
//to the ids of views in the layout
int[] to = new int[]{
R.id.row_text
};
mCursorAdapter = new RemindersSimpleCursorAdapter(
//context
RemindersActivity.this,
//the layout of the row
R.layout.reminders_row,
//cursor
cursor,
//from columns defined in the db
from,
//to the ids of views in the layout
to,
//flag - not used
0);
// the cursorAdapter (controller) is now updating the listView (view)
//with data from the db (model)
mListView.setAdapter(mCursorAdapter);
}
//Abbreviated for brevity
}

这时候如果你运行app,你还是看不到清单里有任何东西;屏幕完全是空的,因为你最后的修改在例子的数据部分插入了数据库功能。按Ctrl+K | Cmd+K并提交的修改,提交信息:Adds SQLite database persistence
for reminders and a new color for important reminders。聪明点的话,你可能想弄清楚怎么用新的RemindersDbAdaper把例子里的条目加回来。这将在下章描述,你可以继续看下去并检查下作业了。


Summary

小结
致此,你有了个成熟的Android应用。在这章,你学会了怎样设置第一个Android项目并用Git控制代码。你也探索了怎么编辑Android布局,用设计或文本模式。你也看到建立一个在动作条里的溢出菜单。这章的最后探索了ListView和Adapter,以及绑定数据到内建的SQLite数据库。在接下来的章节里,增加创建和编辑提醒的功能,你将完成这个app。

最后贴心的送上源代码:

RemindersActivity.java

import android.database.Cursor;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class RemindersActivity extends ActionBarActivity {

private ListView mListView;
private RemindersDbAdapter mDbAdapter;
private RemindersSimpleCursorAdapter mCursorAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reminders);

mListView = (ListView) findViewById(R.id.reminders_list_view);
mListView.setDivider(null);
mDbAdapter = new RemindersDbAdapter(this);
mDbAdapter.open();
Cursor cursor = mDbAdapter.fetchAllReminders();
//from columns defined in the db
String[] from = new String[]{
RemindersDbAdapter.COL_CONTENT
};
//to the ids of views in the layout
int[] to = new int[]{
R.id.row_text
};
mCursorAdapter = new RemindersSimpleCursorAdapter(
//context
RemindersActivity.this,
//the layout of the row
R.layout.reminders_row,
//cursor
cursor,
//from columns defined in the db
from,
//to the ids of views in the layout
to,
//flag - not used
0);
// the cursorAdapter (controller) is now updating the listView (view)
//with data from the db (model)
mListView.setAdapter(mCursorAdapter);

//Abbreviated for brevity

// ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
// this,
// R.layout.reminders_row,
// R.id.row_text,
// new String[]{"1Record", "2Record", "3Record", "4Record"});
// mListView.setAdapter(arrayAdapter);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_new:
//create new Reminder
Log.d(getLocalClassName(), "create new Reminder");
return true;
case R.id.action_exit:
finish();
return true;
default:
return false;
}
}

}

RemindersSimpleCursorAdapter.java

import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.View;
import android.view.ViewGroup;

/**
* 权兴权意
* Created by car on 2016/8/15.
*/
public class RemindersSimpleCursorAdapter extends SimpleCursorAdapter{
public RemindersSimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return super.newView(context, cursor, parent);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
super.bindView(view, context, cursor);
ViewHolder holder = (ViewHolder) view.getTag();
if(holder == null){
holder = new ViewHolder();
holder.collmp = cursor.getColumnIndexOrThrow(RemindersDbAdapter.COL_IMPORTANT);
holder.listTab = view.findViewById(R.id.row_tab);
view.setTag(holder);
}
if(cursor.getInt(holder.collmp) > 0){
holder.listTab.setBackgroundColor(context.getResources().getColor(R.color.orange));
}else{
holder.listTab.setBackgroundColor(context.getResources().getColor(R.color.green));
}
}

static class ViewHolder{
int collmp;//column index
View listTab;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: