2012-02-26 6 views
0

私はres/valuesからアンケートの質問を追加することで問題に直面しました。 ListViewと各行のチェックボックスが必要なので、私はカスタムListViewを作成しました。 これは私のlist_item.xmlですAndroid - リソースのアイテムとサブアイテム

android:text = "@ string/textLarge"と "@ string/textSmall"行を削除しました。そのためTextViewタグに空白行があります。

<LinearLayout android:id="@+id/LinearLayout01" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
xmlns:android="http://schemas.android.com/apk/res/android" 
> 

<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:padding="2dp"> 

<TextView 
    android:id="@+id/textView_large" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 

    android:textSize="16sp"> 
</TextView> 
<TextView 
    android:id="@+id/textView_small" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 

    android:textSize="12sp"> 
</TextView> 
</LinearLayout> 
<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:padding="2dp" 
    android:gravity="right"> 
    <CheckBox 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/checkBxc"> 

    </CheckBox> 
</LinearLayout> 
</LinearLayout> 

と単純なリスト

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
> 
<ListView 
android:id="@android:id/list" 
android:layout_width="match_parent" 
android:layout_height="match_parent"></ListView> 

私のMain.javaファイルが

public class Main extends ListActivity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    setListAdapter(new MyAdapter(this, 
      android.R.layout.simple_list_item_2, R.id.textView_large, 
      getResources().getStringArray(R.array.questions))); 
} 

private class MyAdapter extends ArrayAdapter<String> { 

    public MyAdapter(Context context, int resource, int textViewResourceId, 
      String[] strings) { 
     super(context, resource, textViewResourceId, strings); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     LayoutInflater inflater = (LayoutInflater)   getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View row = inflater.inflate(R.layout.list_item, parent, false); 
     String[] items = getResources().getStringArray(R.array.questions); 
     CheckBox chk = (CheckBox) row.findViewById(R.id.checkBxc); 
     TextView txtLarge = (TextView) row.findViewById(R.id.textView_large); 

     TextView txtSmall = (TextView) row.findViewById(R.id.textView_small); 

     txtLarge.setText(items[position]); 
     return row; 

であると私はquestions.xmlから項目を追加する場合、それは正常に動作し、それは1行しか表示しませんが、私は項目として質問をする必要があります(つまり、あなたはいくつかのスポーツをしますか?)といくつかのコメントb elow(「している場合はチェック」)。どうすればいい?

答えて

1

カスタムArrayAdapter(ArrayAdapterから継承するクラス)を作成し、それをリストビューに設定する必要がありますlistView.setAdapter(yourAdapter) このアダプタでは、カスタムのリストアイテムビューを返すためにgetViewメソッドをオーバーライドする必要があります。 。

も、この例を参照してください: http://android-codes-examples.blogspot.com/2011/03/customized-listview-items-selection.html

+0

Marcのおかげで、私は少し違った方法で解決しました – user1232884

1

この作品:

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View row = inflater.inflate(R.layout.list_item, parent, false); 

が良くありません:あなたはそれがすでに過度のオブジェクトを破壊する存在していても、常に新しいアイテムビューを作成する時間がかかります。また、ArrayAdapter自体の機能を使用することもできます。 Look hereをご覧ください。

関連する問題