单独的列表选择框,不需要确定按钮,通过内部接口类,在activity或者 fragment中接收返回值:
public class SelectSortDialog extends AlertDialog.Builder implements DialogInterface.OnClickListener {
private int topicID=-1;//如果需要返回的是第几项,用这个,从0开始
private String toSort="";//直接返回选择的内容,
private OnTestListening onTestListening;
public SelectSortDialog(Context context,final OnTestListening onTestListening) {
super(context);
this.onTestListening=onTestListening;
Vector<String> vec=getSort(); //获得选择项列表,可以是数组,或者从数据库中获取
final String[] datas=vec.toArray(new String[]{});
this.setTitle("选择需要更换的分类");
this.setItems(datas, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int topicID) {
//onTestListening.getTopicID(topicID);
onTestListening.getTopicID(datas[topicID]);
}
});
this.show();
}
public interface OnTestListening{//内部接口类
// void getTopicID(int topicID);
void getTopicID(String toSort);
}
public void setTopicID(int topicID) {
this.topicID = topicID;
}
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO 自动生成的方法存根
// onTestListening.getTopicID(topicID);
onTestListening.getTopicID(toSort);
}
在MainActivity中使用
SelectSortDialog selectsortdialog=new SelectSortDialog(MainActivity.this,new SelectSortDialog.OnTestListening() {
@Override
//public void getTopicID(int topicID) {
public void getTopicID(String toSort) {
// TODO 自动生成的方法存根
System.out.println(toSort);
}
});
在fragment中使用
SelectSortDialog selectsortdialog=new SelectSortDialog(getContext(),new SelectSortDialog.OnTestListening() {
//getContext()或者 getActivity()
@Override
//public void getTopicID(int topicID) {
public void getTopicID(String toSort) {
// TODO 自动生成的方法存根
System.out.println(toSort);
}
});