2

配列アダプターを使用して listView を作成しています。これは私のコードです。

listView = new ListView(context);
                 ArrayAdapter<String>adapter = new ArrayAdapter<String>(context,android.R.layout.simple_list_item_single_choice, choice);
                 listView.setAdapter(adapter);

これにより、テキストに黒色が提供されます。テキストの色を青色に変更する必要があります。どうすればこれを変更できますか。私は配列アダプタで私のレイアウトを使用しました。これがコードです

listView = new ListView(context);
                 ArrayAdapter<String>adapter = new ArrayAdapter<String>(context,layout.my_single_choice, choice);
                 listView.setAdapter(adapter);

my_single_choice.xml のコードを以下に示します

<?xml version="1.0" encoding="utf-8"?>
<RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:id="@+id/my_choice_radio"
    android:layout_height="match_parent"
    android:button="@null"
    android:drawableRight="@android:drawable/btn_radio"
    android:text="Option"
    style="@style/ListItemTextColor" />

これは機能しません。すべてのラジオボタンを選択できます。そして、clickListenerが機能していないときは...? どうすれば解決できますか

4

2 に答える 2

4

これを使用するために実行できるいくつかのアプローチがあります。これを行う最も一般的な方法は次のとおりです。

  • カスタム リストビューの使用 ( http://www.androidpeople.com/android-custom-listview-tutorial-part-1 )
  • カスタム レイアウトの使用 (android.R 名前空間内ではない)

    ビューで、次のコードを使用します。

    <?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/tv"
        android:textColor="@color/font_content"
        android:padding="5sp"
        android:layout_width="fill_parent"
        android:background="@drawable/rectgrad"
        android:singleLine="true"
        android:gravity="center"
        android:layout_height="fill_parent"/>
    

    クラスで、次のコードを使用します。

    ListView lst = new ListView(context);
    String[] arr = {"Item 1","Item 2"};
    ArrayAdapter<String> ad = new ArrayAdapter<String (context,R.layout.mytextview,arr);
    lst.setAdapter(ad);
    
  • 私のお気に入りは、ArrayAdapter の getView メソッドをオーバーライドすることです

    ListView listView = (ListView) this.findViewById(R.id.listView);
    listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, MobileMuni.getBookmarkStore().getRecentLocations() ) {
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView textView = (TextView) super.getView(position, convertView, parent);
    
        String currentLocation = RouteFinderBookmarksActivity.this.getResources().getString(R.string.Current_Location);
        int textColor = textView.getText().toString().equals(currentLocation) ? R.color.holo_blue : R.color.text_color_btn_holo_dark;
        textView.setTextColor(RouteFinderBookmarksActivity.this.getResources().getColor(textColor));
    
        return textView;
        }
    });
    

また、これはここからの議論の結果でした:単純なリスト項目のテキストの色を変更する方法

于 2013-09-11T00:00:00.953 に答える