Android AlertDialog-CheckedTextView不';行不通
我在AlertDialog上显示了一个客户列表。我希望当我单击一个项目时,CheckedTextView被设置为选中。我试了几个小时,但没有Android AlertDialog-CheckedTextView不';行不通,android,checkedtextview,Android,Checkedtextview,我在AlertDialog上显示了一个客户列表。我希望当我单击一个项目时,CheckedTextView被设置为选中。我试了几个小时,但没有setChecked()似乎不起作用 有人能帮我吗 final List<Producteur> p = DAOProducteur.getInstance(null).recherche(producteur); AlertDialog.Builder builder = new AlertDialog.Builder(activity);
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();
首先
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();
}
}
我看不到将刷新列表视图的notifydatasetChange()
调用。因此,在onClick()
中,在更改备份数据源之后,也应该刷新适配器
备选方案
在getView()中,您可以为复选框设置onClickListener
,对于每个onClick()
回调,您应该更新备份数据源并调用`on复选框以切换状态
代码
模型类
类模型{
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;
}
活动
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();
}
}
public类MainActivity扩展活动实现OnClickListener{
CustomAdapter mAdapter=null;
List-mDataSource=null;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDataSource=newarraylist();
mDataSource.add(新模型(“Tom”,false));
添加(新模型(“Goffy”,false));
mDataSource.add(新模型(“Ron”,false));
mDataSource.add(新模型(“Pit”,false));
添加(新模型(“Dwyane”,false));
add(新模型(“John”,false));
mDataSource.add(新模型(“Son”,false));
mAdapter=新的CustomAdapter(此为mDataSource);
}
@凌驾
受保护的void onResume(){
super.onResume();
AlertDialog.Builder=新建AlertDialog.Builder(此);
建筑商。设置SingleChoiceItems(mAdapter,-1,本);
建造商名称(“定制”);
builder.create().show();
}
@凌驾
public void onClick(DialogInterface dialog,int which){
Log.d(“@gaurav”,“which”+which);
Model m=mDataSource.get(which);
布尔b=m.getState();
b=!b;
m、 设定状态(b);
mAdapter.notifyDataSetChanged();
}
}
单行.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>
编辑的缺点是用户只能按复选框,而不能按整行…还有其他想法吗?您可以在视图上设置onClickListener
。或者,如果您可以从alertbox获取列表视图,则可以在其上设置onItemClickListener
,您正在编写!如果我使用notifydatasetChange()
在视图OnClickListener
上,单选按钮工作正常,但速度较慢,当我滚动时,单选按钮未锁定。但感谢您的帮助!:)