アダプターがカスタム ListAdapter (ArrayAdapter ではない) である ListView (lvProperties) があります。このアダプターは、カスタム タイプ セクション (いくつかの文字列、整数、ArrayList、およびメソッドを含む) のインスタンス変数からデータを取得します。
常に選択を表示するには、このリストが必要です (アクティビティの起動時でも、選択された要素が常に存在する必要があります)。
過去に単純な ArrayAdapter を使用する場合、次のようなもので十分でした:
lvProperties.requestFocus();
lvProperties.setSelection(0);
ただし、この場合はまったく機能しません。私はSOとWebを見回して使用しました:
lvProperties.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
lvProperties.setFocusable(true);
lvProperties.setFocusableInTouchMode(true);
lvProperties.requestFocus();
lvProperties.setSelection(0);
それでも何もありません。
マイリストアダプター:
lvProperties.setAdapter(new ListAdapter() {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v=convertView;
if(v==null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v=vi.inflate(R.layout.detail_row1, null);
}
TextView name=(TextView)v.findViewById(R.id.propName);
TextView value=(TextView)v.findViewById(R.id.propValue);
if(name!=null) {
name.setText(section.propNames.get(position));
}
if(value!=null) {
value.setText(section.propValues.get(position));
}
return v;
}
@Override
public void unregisterDataSetObserver(DataSetObserver observer) {}
@Override
public void registerDataSetObserver(DataSetObserver observer) {}
@Override
public boolean isEmpty() {
return false;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
return 0;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public int getCount() {
return section.propNames.size();
}
@Override
public boolean isEnabled(int position) {
return true;
}
@Override
public boolean areAllItemsEnabled() {
return true;
}
});
私の未実装の OnItemSelectedListener:
lvProperties.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
そして、私の detail_row1.xml (次は行のレイアウトに実装しますが、これが現在の方法です:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:padding="6dip"
>
<TextView
android:id="@+id/propName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#000000"
android:gravity="center_vertical|left"
android:paddingTop="10sp"
android:paddingBottom="10sp"
android:paddingLeft="4sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:id="@+id/propValue"
android:textColor="#000000"
android:textSize="16sp"
android:textStyle="bold"
android:paddingTop="10sp"
android:paddingBottom="10sp"
android:paddingRight="4sp"
android:gravity="center_vertical|right"
/>
</LinearLayout><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:padding="6dip"
>
<TextView
android:id="@+id/propName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#000000"
android:gravity="center_vertical|left"
android:paddingTop="10sp"
android:paddingBottom="10sp"
android:paddingLeft="4sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:id="@+id/propValue"
android:textColor="#000000"
android:textSize="16sp"
android:textStyle="bold"
android:paddingTop="10sp"
android:paddingBottom="10sp"
android:paddingRight="4sp"
android:gravity="center_vertical|right"
/>
</LinearLayout>
これを達成する方法についてのガイドへのヘルプ/ポインタはありますか? ドローアブルを使用したくありません。使用しているテーマのデフォルトのセレクターだけです。
繰り返しますが、リストには常に 1 つのエントリが選択されている必要があります。
設計上、これはリストの意図した動作ではないことを認識していますが、これは 1 つのサイズがすべてに適合しないケースの 1 つです。