web-dev-qa-db-ja.com

Android:カスタムリストビューのラジオボタン

リストビューにラジオボタンを実装する必要があるアプリケーションを開発しています。各行に1つのラジオボタンと2つのテキストビューがあるリストビューを実装します。リストビューの下にある「OK」ボタン1つ。

リストビューとカスタムアダプターを作成しました。リストビューのコードは次のとおりです。

<ListView
    Android:id="@+id/listview"
    Android:layout_width="fill_parent"
    Android:layout_height="fill_parent"
    Android:cacheColorHint="#00000000"
    Android:overScrollMode="never"
    tools:ignore="NestedScrolling"
    Android:choiceMode="singleChoice" >
</ListView>

そして、カスタムアダプターレイアウトを次のように作成しました。

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
    xmlns:tools="http://schemas.Android.com/tools"
    Android:layout_width="fill_parent"
    Android:layout_height="fill_parent" >

    <TableRow
        Android:layout_width="fill_parent"
        Android:layout_height="wrap_content"
        tools:ignore="UselessParent" >

        <RadioButton
            Android:id="@+id/radiobutton"
            Android:layout_width="0sp"
            Android:layout_height="wrap_content"
            Android:layout_weight=".1" />

        <TextView
            Android:id="@+id/textview1"
            Android:layout_width="0sp"
            Android:layout_height="wrap_content"
            Android:layout_weight=".3" />

        <TextView
            Android:id="@+id/textview2"
            Android:layout_width="0sp"
            Android:layout_height="wrap_content"
            Android:layout_weight=".3" />

    </TableRow>

</TableLayout>

Javaフラグメントのコードは次のとおりです。

ListView listView = (ListView) view.findViewById(R.id.listview);

// values is a StringArray holding some string values.
CustomAdapter customAdapter = new CustomAdapter (getActivity(), values);
listView.setAdapter(customAdapter );
listView.setOnItemClickListener(this);

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {}

また、アダプターのコードは次のとおりです。

public class CustomAdapter extends ArrayAdapter<String> {   
    /** Global declaration of variables. As there scope lies in whole class. */
    private Context context;
    private String[] listOfValues;

    /** Constructor Class */
    public CustomAdapter (Context c,String[] values) {
        super(c,R.layout.adapter_layout,values);
        this.context = c;
        this.listOfValues = values;
    }

    /** Implement getView method for customizing row of list view. */
    public View getView(final int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        // Creating a view of row.
        View rowView = inflater.inflate(R.layout.adapter_layout, parent, false);

            TextView textView1 = (TextView)rowView.findViewById(R.id.textview1);
            TextView textView2 = (TextView)rowView.findViewById(R.id.textview2);

            RadioButton radioButton = (RadioButton) rowView.findViewById(R.id.radiobutton);

            radioButton.setOnClickListener(new OnClickListener() {          
            @Override   
            public void onClick(View v) {
                Toast.makeText(context, CustomAdapter[position], Toast.LENGTH_SHORT).show();
            }
        });

        return rowView;
    }
}    

Textview1のデータはSQLiteデータベースから読み込まれ、textview2のデータは「Status Closed」です。また、ラジオボタンを選択するか、ラジオボタンをクリックすると、テキストビューのテキストが「ステータスオープン」に変わります。

問題は:アプリケーションの必要性は、1つのラジオボタンのみが選択を取得し、textview2のデータが選択時に変更を取得することです。ユーザーが他のラジオボタンをクリックすると選択され、前のラジオボタンが選択解除され、textview2のテキストが以前に選択したラジオボタンの「ステータスクローズ」に変更され、ラジオボタンをクリックして「ステータスオープン」になります。

編集1:

そして、「OK」ボタンをクリックして、リストビューtextview1とtextview2の位置、テキストを取得したいので、そのテキストを表示してSQLiteデータベースに保存します。

どの手順に従うべきかを教えてください。私は申請の途中です。貴重なガイダンスが必要です。

19
Manoj Fegde

主なアイデアは次のとおりです

  • RadioButtonがチェックされたら、すべてのビューが更新されるようにnotifyDataSetChanged()を呼び出す必要があります。
  • RadioButtonがチェックされているとき、selectedPositionを設定して、どのRadioButtonが選択されているかを追跡する必要があります
  • ViewsはListViews内でリサイクルされます。したがって、ListViewでの絶対位置の変更。したがって、ListAdapter#getView()内では、各RadioButtonsetTag()を呼び出す必要があります。これにより、RadioButtonがクリックされたときに、リスト内のRadioButtonの現在の位置を決定できます。
  • RadioButton#setChecked()は、新規または既存のViewsに対してgetView()内で更新する必要があります。

これらのアイデアを実証するために私が書いてテストしたArrayAdapterの例を次に示します

public class MainActivity extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // I do no use these values anywhere inside the ArrayAdapter. I could, but don't.
        final Integer[] values = new Integer[] {1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,};

        ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(this, R.layout.row, R.id.textview, values) {

            int selectedPosition = 0;

            @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.row, null);
                    RadioButton r = (RadioButton)v.findViewById(R.id.radiobutton);
                }
                TextView tv = (TextView)v.findViewById(R.id.textview);
                tv.setText("Text view #" + position);
                RadioButton r = (RadioButton)v.findViewById(R.id.radiobutton);
                r.setChecked(position == selectedPosition);
                r.setTag(position);
                r.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        selectedPosition = (Integer)view.getTag();
                        notifyDataSetChanged();
                    }
                });
                return v;
            }

        };
        setListAdapter(adapter);
    }
}
69
Brian Attwell

以下のアダプターを試してください:

public class ChooseAdapter extends ArrayAdapter<LinkedHashMap<String, String>> {
private ArrayList<LinkedHashMap<String, String>> listMenu;

int position_id;

private LayoutInflater inflater;
Context context;

public ChooseAdapter(Activity activity,
        ArrayList<LinkedHashMap<String, String>> listMenu, int type) {

    super(activity, R.layout.choose_single_item, listMenu);
    this.listMenu = listMenu;

    context = activity.getApplicationContext();
    inflater = LayoutInflater.from(context);

}

public int getCount() {
    return listMenu.size();
}

public long getItemId(int position) {
    return position;

}

public static class ViewHolder

{
    public CheckBox chk;

}

public View getView(final int position, View convertView, ViewGroup parent) {
    final ViewHolder view;

    if (convertView == null) {

        view = new ViewHolder();

        convertView = inflater.inflate(R.layout.choose_single_item, null);
        view.chk = (CheckBox) convertView
                .findViewById(R.id.selection_checkbox);

        view.chk.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                // TODO Auto-generated method stub
                if (isChecked) {

                    listMenu.get((Integer) buttonView.getTag()).put(
                            "checked", "true");
                    for (int i = 0; i < listMenu.size(); i++) {
                        if (i != (Integer) buttonView.getTag()) {
                            if (listMenu.get(i).containsKey("checked"))
                                listMenu.get(i).remove("checked");
                        }
                    }
                } else {
                    listMenu.get((Integer) buttonView.getTag()).remove(
                            "checked");
                }

                notifyDataSetChanged();
            }
        });

        convertView.setTag(R.id.selection_checkbox, view.chk);
        convertView.setTag(view);

    }

    else {
        view = (ViewHolder) convertView.getTag();
    }
    view.chk.setTag(position);

    view.chk.setText(listMenu.get(position).get("name"));

    if (listMenu.get(position).containsKey("checked")) {
        view.chk.setChecked(true);
    } else
        view.chk.setChecked(false);

    return convertView;

}
}

そして、私が膨らませているレイアウトは次のとおりです。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
Android:layout_width="match_parent"
Android:layout_height="wrap_content"
Android:orientation="horizontal" >

<CheckBox
    Android:id="@+id/selection_checkbox"
    Android:layout_width="wrap_content"
    Android:layout_height="wrap_content"
    Android:layout_margin="10dp"
    Android:text="abc"
    Android:textColor="#8C8C8C"
    Android:textSize="16sp" />

</LinearLayout>

ここでは、チェックボックスを使用しましたが、ラジオボタンを代わりに使用することもできます。他に変更する必要はありません。

それが役に立てば幸い!

2
Priyank Joshi