android - AlertDialog - CheckedTextView 不起作用

标签 android checkedtextview

我在 AlertDialog 上显示了一个自定义列表。我希望当我单击一个项目时,CheckedTextView 被设置为选中。我试了几个小时,但没有 setChecked()似乎不起作用...

谁能帮帮我?

final List<Producteur> p = DAOProducteur.getInstance(null).recherche(producteur);

AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle("Résultat(s) :");
builder.setSingleChoiceItems(
        new ArrayAdapter<Producteur>(
                activity, 
                R.layout.activity_recherche_producteurs, 
                p) {
                    @Override
                    public View getView(int position, View convertView, ViewGroup parent) {
                        LayoutInflater inflater = (LayoutInflater) parent.getContext()
                            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                        View rowView = inflater.inflate(R.layout.activity_recherche_producteurs, parent, false);

                        // Chargement des éléments à partir du layout
                        TextView numProd        = (TextView) rowView.findViewById(R.id.num_prod);
                        TextView numLaiterie    = (TextView) rowView.findViewById(R.id.num_laiterie);
                        TextView numLabo        = (TextView) rowView.findViewById(R.id.num_labo);
                        TextView raisonSociale  = (TextView) rowView.findViewById(R.id.raison_sociale);
                        TextView adresse        = (TextView) rowView.findViewById(R.id.adresse);
                        final CheckedTextView checkBox = (CheckedTextView) rowView.findViewById(R.id.check_box);

                        // Mise en place des textes sur les éléments
                        numProd.setText("n° " + String.valueOf(p.get(position).getNumProd()));
                        numLaiterie.setText(", " + String.valueOf(p.get(position).getNumLaiterie()));
                        String nLabo = String.valueOf(p.get(position).getNumLabo());
                        nLabo = nLabo.substring(0, nLabo.length() - 1) + " " + nLabo.substring(nLabo.length() - 1, nLabo.length());
                        numLabo.setText(nLabo);
                        raisonSociale.setText(String.valueOf(p.get(position).getRaisonSociale()));
                        adresse.setText(String.valueOf(p.get(position).getAdresse()));
                        checkBoxList.put(position, checkBox);

                        return rowView;
                    }
                }, 
        -1, 
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int position) {
                producteurChoisi = p.get(position);
                checkBoxList.get(dernierCheck).setChecked(false);
                checkBoxList.get(position).setChecked(true);
                dernierCheck = position;
            }
        });
builder.setPositiveButton(R.string.valider, new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int position) {
                dialog.cancel();

                if(producteurChoisi != null) {
                    // Ajout du producteur dans la tournée modèle
                    DAOTourneeModele.getInstance(activity).nouveauProducteur(
                            date, 
                            tournee, 
                            tour, 
                            producteurChoisi.getNumProd());

                    // Ouverture du litrage correspondant au nouveau producteur
                    Intent i = new Intent(activity.getApplicationContext(), SaisieActivity.class);
                    i.putExtra("date", date);
                    i.putExtra("tournee", String.valueOf(tournee));
                    i.putExtra("tour", tour);
                    i.putExtra("producteur", String.valueOf(producteurChoisi.getNumProd()));
                    activity.startActivity(i);
                } else {
                    Toast.makeText(activity, getString(R.string.erreur_producteur_non_choisi), Toast.LENGTH_LONG).show();
                }
            }
        });
builder.setNegativeButton(R.string.quitter, new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
                new AjouteProdDialog().show(activity.getFragmentManager(), "dialog");
            }
        });
AlertDialog alert = builder.create();
alert.show();

SparseArray<CheckedTextView> checkBoxList = new SparseArray<CheckedTextView>();int dernierCheck = 0;之前声明过。

还有我的 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/num_prod"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/etat"
        android:text="n° 477009"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/num_laiterie"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/etat"
        android:layout_toRightOf="@+id/num_prod"
        android:text=", 131" />

    <TextView
        android:id="@+id/num_labo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/etat"
        android:layout_centerHorizontal="true"
        android:layout_toRightOf="@+id/num_laiterie"
        android:text="477009 5" />

    <TextView
        android:id="@+id/raison_sociale"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/num_prod"
        android:layout_alignParentLeft="true"
        android:text="GAEC DE ST AIGNAN" />

    <TextView
        android:id="@+id/adresse"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/raison_sociale"
        android:layout_alignParentLeft="true"
        android:text="KERAIGNAN" />

    <CheckedTextView
        android:id="@+id/check_box"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:checkMark="?android:attr/listChoiceIndicatorSingle" />

</RelativeLayout>

最佳答案

首先

我看不到将刷新 ListView 的 notifydatasetChange() 调用。因此在 onClick() 中更改后备数据源后,您也应该刷新适配器。

备选

在 getView() 中,您可以为 checkBox 设置 onClickListener 并且对于每个 onClick() 回调,您应该更新支持数据源并调用 `toggle()在复选框上切换状态。

代码

模型类 类模型 {

public Model(String data, boolean state) {
    this.data = data;
    this.state = state;
}

String data;
Boolean state;
    // getter and setters 
   }

自定义适配器

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = inflater.inflate(R.layout.single_row, null, false);
    TextView tv = (TextView) view.findViewById(R.id.content);
    CheckedTextView ctv = (CheckedTextView) view.findViewById(R.id.checker);
    Model m = backingsource.get(position);
    tv.setText(m.getData());
    ctv.setChecked(m.getState());
    return view;
}

Activity

public class MainActivity extends Activity implements OnClickListener {
    CustomAdapter mAdapter = null;
    List<Model> mDataSource = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mDataSource = new ArrayList<Model>();
        mDataSource.add(new Model("Tom", false));
        mDataSource.add(new Model("Goffy", false));
        mDataSource.add(new Model("Ron", false));
        mDataSource.add(new Model("Pit", false));
        mDataSource.add(new Model("Dwyane", false));
        mDataSource.add(new Model("John", false));
        mDataSource.add(new Model("Son", false));

        mAdapter = new CustomAdapter(this, mDataSource);
    }

    @Override
    protected void onResume() {
        super.onResume();
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setSingleChoiceItems(mAdapter, -1, this);
        builder.setTitle("Customized");
        builder.create().show();
    }

    @Override
    public void onClick(DialogInterface dialog, int which) {
        Log.d("@gaurav", "which " + which);
        Model m = mDataSource.get(which);
        boolean b = m.getState();
        b = !b;
        m.setState(b);
        mAdapter.notifyDataSetChanged();
    }
      }

single_row.xml

<?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="match_parent"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Some text" >
    </TextView>

    <CheckedTextView
        android:id="@+id/checker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checkMark="?android:attr/listChoiceIndicatorSingle" />

</LinearLayout>

关于android - AlertDialog - CheckedTextView 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23512626/

相关文章:

Android - listView 中的自定义 CheckedTextView 在点击时未被选中/禁用 SINGLE_CHOICE 模式

android - 覆盖引用的样式属性

android - 查看动画(调整球的大小)

android - 理想的 Gradle 配置为每个源集创建单独的模块做什么?他会改变他的依赖关系吗?

Android NDK CPU 宏。 ARMv7

java - android.R.layout.simple_list_item_checked 不在 ListView 中切换

android - 更改 CheckedTextView 的 checkMark 或大小

java - 跨多个 Activity 的 Android 数据库

java - 使用多个警报