すべてのノードのセットを持つ「ノード」と呼ばれるSQLiteテーブルがあります(各ノードには複数のプロパティがあります)。
アクティビティでは、SimpleCursorAdapterの拡張機能を使用して、SQLクエリに基づいてNodesテーブルのサブセットを表示し、(クエリからの)結果のテーブルにカーソルを渡します。
{id_long, name_string, description_string, nodetype_enum_toString...}
1, "home", "home_desc", "cool"
2, "item1", "item1_desc", "warm"
3, "item2", "item2_desc", "hot"
4, "item3", "item3_desc", "warm"
5, "item4", "item4_desc", "hot"
ListActivityの各リストアイテムを変更して、アイテムがクール、ウォーム、ホットのいずれであるかに基づいて異なる色を表示するようにします。私は小さな(空の)ビューを持っていますが、それは常にデフォルトで白(「else」の部分)になっています。
public View getView(int position, View convertView, ViewGroup parent) {
convertView = super.getView(position, convertView, parent);
if (convertView == null) convertView = View.inflate(mContext, R.layout.viewstack_item, null);
View nodetype_view = (View) convertView.findViewById(R.id.nodetype_view);
mGenCursor_cur = mDataHelper_db.getAll();
mGenCursor_cur.moveToPosition(position);
Log.i("test", "node type key " + mGenCursor_cur.getColumnIndex(DataHelper.NODES_TYPE_KEY));
String type_nt = mGenCursor_cur.getString(mGenCursor_cur.getColumnIndex(DataHelper.NODES_TYPE_KEY));
String name_str = mGenCursor_cur.getString(mGenCursor_cur.getColumnIndex(DataHelper.NODES_NAME_KEY));
Log.i("getView", title_str + "typeis " + type_nt + " at position " + position);
if (prio_np.equals(NodePrio.HIGH.toString())) {
Log.i("prio_np", "high red");
prioView_view.setBackgroundColor(Color.RED);
} else if (prio_np.equals(NodePrio.NORMAL.toString())) {
Log.i("prio_np", "norm magenta");
prioView_view.setBackgroundColor(Color.MAGENTA);
} else {
Log.i("prio_np", "else (low) white");
prioView_view.setBackgroundColor(Color.WHITE);
}
mGenCursor_cur.close();
bindView(convertView, mContext, getCursor());
return(convertView);
}
私の問題は、SimpleListAdapterに渡されるカーソルには、必要なサブセット内の対応するノードの_ID(私が推測する)が含まれますが、getViewの位置パラメーターはサブセットに対してローカルであるため、相互参照できないことです。ノードテーブル。簡単なテクニックが足りませんか?
いつものように、どんな助けもありがたいです。
マークされた回答のコメント4は、これを解決するのに役立ちました。SimpleCursorAdapterに渡されたカーソルをメンバー変数として保存し、movetopositionとgetLongを使用します。
mCursor_cur.moveToPosition(position);
long id = mCursor_cur.getLong(0);
ここで、positionはgetView()メソッドのローカルパラメーターであり、db_idはSimpleCursorAdapterに渡されるカーソルの最初の列であるため、getLongには0が使用されます。