タイトル/サブタイトルTextViewsのペアとして項目を表示するために、カスタムアダプタでListViewを使用しています。カスタムアンドロイドリストビュー項目は全領域で選択できません
残念ながら、私はちょうどその上半分をクリックして項目を選択することができ、タイトルここ
によって占め一つは、私が使用していますコードです:
journal_list.xml
<?xml version="1.0" encoding="utf-8"?>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawSelectorOnTop="false"
android:background="#fff"
android:cacheColorHint="#fff"
android:paddingBottom="6dp"
/>
リスト項目のレイアウトのためのjournal_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
>
<TextView
android:id="@+id/journal_entry_date"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="#000"
android:textSize="18sp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
/>
<TextView
android:id="@+id/journal_content"
android:paddingLeft="28dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="16sp"
android:textColor="#555"
android:inputType="textMultiLine"
android:maxLines="2"
android:minLines="1"
android:layout_below="@id/journal_entry_date"
android:layout_alignParentLeft="true"
/>
また
アダプタのコード:ListActivityさんのonCreateから
private class JournalAdapter extends ArrayAdapter<JournalEntry> {
Context ctxt;
public JournalAdapter(Context ctxt) {
super(ctxt, R.layout.journal_list_item);
this.ctxt = ctxt;
}
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
JournalHolder holder;
if (row == null) {
row = ((LayoutInflater)ctxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).
inflate(R.layout.journal_list_item, parent, false);
holder = new JournalHolder(row);
row.setTag(holder);
} else {
holder = (JournalHolder) row.getTag();
}
JournalEntry crt = getItem(position);
holder.getDate().setText(crt.dateWritten);
holder.getContent().setText(crt.content);
return row;
}
}
private static class JournalHolder {
private TextView date;
private TextView content;
private View base;
public JournalHolder(View base) {
this.base = base;
}
public TextView getDate() {
if (date == null)
date = (TextView) base.findViewById(R.id.journal_entry_date);
return date;
}
public TextView getContent() {
if (content == null)
content = (TextView) base.findViewById(R.id.journal_content);
return content;
}
}
コード()メソッド:また
private JournalAdapter adapter;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.journal_list);
adapter = new JournalAdapter(this);
setListAdapter(adapter);
registerForContextMenu(getListView());
}
が、私はonResumeでupdateList()(と呼ばれるメソッドを呼び出しています)
private void updateList() {
adapter.clear();
Cursor cursor = helper.listJournal(start, end);
cursor.moveToFirst();
JournalEntry crt;
while (!cursor.isAfterLast()) {
crt = new JournalEntry();
crt.dateWritten = cursor.getString(cursor.getColumnIndex("date_written"));
crt.content = cursor.getString(cursor.getColumnIndex("content"));
crt.id = cursor.getInt(cursor.getColumnIndex("entry_id"));
adapter.add(crt);
cursor.moveToNext();
}
cursor.close();
adapter.notifyDataSetChanged();
}
リストアイテムのクリックは、次のようにListActivityのonListItemClickで処理されます。
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
Intent intent = new Intent(this, JournalEditor.class);
intent.setAction(Intent.ACTION_EDIT);
intent.putExtra("entry_id", adapter.getItem(position).id);
startActivity(intent);
}
明らかに、私は何か重要なことを忘れているので、この奇妙なことが起こるのは明らかです。
私はここで似たような議論を見つけました:http://groups.google.com/group/android-developers/browse_thread/thread/5636c8ea74033657しかしそれほど役に立ちませんでした。
あなたはの関連する部分を投稿することができますlayout/journal_list.xmlを膨らませるActivityまたはListActivity?関連する部分には 'onCreate'と' onListItemClick'があります。 – Ribose
@Ribose onCreate()メソッドとonListItemClick()メソッドとupdateList()メソッドを公開しました。これは、データベースからレコードを取得してアダプタに追加するメソッドです。 – Gabriel
また、ListViewにコンテキストメニューを登録しますが、アイテムを長時間クリックするとうまく動作します。 – Gabriel