安卓:按钮preSS后,复选框onCheckedChange不工作 [英] Android : After button press, CheckBox onCheckedChange not working
问题描述
我创建了一个的ArrayList
自定义 ArrayAdater
。每个列表项
包含复选框
和的TextView
。
I've created an ArrayList
with custom ArrayAdater
. Each ListItem
contains a CheckBox
and a TextView
.
的TextView包含的目录的名称和复选框表示目录是否应扫描
TextView contains the name of the directories and the CheckBox indicates whether that directory should be scanned.
我维持的ArrayList
,用于存储所有目录的名称检查。
有一些按钮
倒在ListView。
I'm maintaining an ArrayList
that stores all the names of the directories checked.
There a some buttons
down in the ListView.
起初,当我检查/取消申请ArrayList中正常工作,并增加了/在它移除元素。
Initially, when I check/uncheck the application the ArrayList works fine and adds/removes elements in it.
但是,当我preSS任何按钮ArrayList的冻结,不能添加/删除元素。
But when I press any button the ArrayList freezes and does not add/delete the elements.
code以下: (很抱歉的长度)的
Code Below: (Sorry for the length)
MainActivity类别:
MainActivity Class:
public class MainActivity extends ListActivity implements OnClickListener {
Button done, cancel, selectAll, back;
ListView listView;
ArrayAdapter<Directory> adapter;
List<Directory> list;
ArrayList<String> checkedList;
private final static String ROOT = "/";
private String currentDir; // Used at the time of back button press
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
setContentView(R.layout.main);
checkedList = new ArrayList<String>();
done = (Button) findViewById(R.id.bDone);
back = (Button) findViewById(R.id.bBack);
cancel = (Button) findViewById(R.id.bCancel);
selectAll = (Button) findViewById(R.id.bSelAll);
done.setOnClickListener(this);
back.setOnClickListener(this);
cancel.setOnClickListener(this);
currentDir = ROOT;
new AsyncHandler(this, ROOT).execute();
} catch (Exception e) {
Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// super.onListItemClick(l, v, position, id);
try {
Directory d = list.get(position);
File f = d.getFile();
if (f.isDirectory() && f.canRead()) {
// Updating View on List TextView item click
currentDir = f.getAbsolutePath();
new AsyncHandler(this, f.getAbsolutePath()).execute();
} else {
Toast.makeText(this, f.getName() + " NOT READABLE",
Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.bDone:
ArrayList<String> localCheckedList = checkedList;
checkedList = filterCheckedList();
String allCkecdDirs = "";
for (int i = 0; i < checkedList.size(); i++) {
allCkecdDirs = allCkecdDirs + "\n" + checkedList.get(i);
}
Toast.makeText(this, allCkecdDirs, Toast.LENGTH_LONG).show();
break;
case R.id.bBack:
if(!currentDir.equals(ROOT)){
/*
* here backtracking current directory one step
* if current die = /mnt/sdcard/music
* so now it will be /mnt/sdcard
* this is because if Back button is pressed,
* in ElementAdapter while rendering elements
* the currentDir will become /mnt/sdcard/music/music
*/
currentDir = new File(currentDir).getParent();
new AsyncHandler(this, currentDir).execute();
}else{
Toast.makeText(this, "Listing already at Root Folder", Toast.LENGTH_LONG).show();
}
break;
case R.id.bCancel:
this.finish();
break;
}
}
private ArrayList<String> filterCheckedList() {
/*
* Removing duplicates
*/
HashSet hs = new HashSet();
hs.addAll(checkedList);
checkedList.clear();
checkedList.addAll(hs);
/*
* Deleting sub-folders if their parent folder has already been added in
* the list
*/
Collections.sort(checkedList);
ArrayList<String> filteredList = new ArrayList<String>();
for (int i = 0; i < checkedList.size(); i++) {
File current = new File(checkedList.get(i));
File parent = current;
while ((parent = parent.getParentFile()) != null) {
if (checkedList.contains(parent.getAbsolutePath())) {
current = parent;
}
}
if (current != null
&& !filteredList.contains(current.getAbsolutePath())) {
filteredList.add(current.getAbsolutePath());
}
}
checkedList = filteredList;
return checkedList;
}
public class AsyncHandler extends AsyncTask {
Context context;
String currentFolder;
private List<Directory> prevList;
public AsyncHandler(Context c, String currentFolder) {
this.context = c;
this.currentFolder = currentFolder;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Object doInBackground(Object... arg0) {
getList();
return null;
}
@Override
protected void onPostExecute(Object result) {
/*
* Checking if list does not contain any directory. If it does not
* then the previous view's list is treated as current list and
* rendered again.
*/
if (list.size() == 0) {
Toast.makeText(context, "No Sub Directory", Toast.LENGTH_SHORT)
.show();
list = prevList;
}
setListAdapter(new ElementAdapter(context, list, checkedList, currentDir));
}
private void getList() {
prevList = list;
list = new ArrayList<Directory>();
File root = new File(currentFolder);
File[] files = root.listFiles();
for (File f : files) {
if (f.isDirectory() && !f.isHidden() && f.canRead()) {
list.add(new Directory(f, f.getName()));
}
}
}
}
}
ArrayAdapter类:
ArrayAdapter Class:
public class ElementAdapter extends ArrayAdapter {
private Context context;
private List<Directory> list;
ArrayList<String> checkedList;
public final String fileName = "AudioSessionFile";
SharedPreferences sharedAudioPrefrences;
private static String currentDir; // This will be appended at the time when
// chcing if the checkbox is aready
// checked or not
public ElementAdapter(Context c, List<Directory> list,
ArrayList checkedList, String currentDir) {
super(c, R.layout.row, list);
this.context = c;
sharedAudioPrefrences = c.getSharedPreferences(fileName,
Context.MODE_PRIVATE);
this.list = list;
this.checkedList = checkedList;
this.currentDir = currentDir;
}
static class ViewHolder {
protected TextView textView;
protected CheckBox checkBox;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
View view = convertView;
if (view == null) {
LayoutInflater inflator = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflator.inflate(R.layout.row, null);
viewHolder = new ViewHolder();
viewHolder.textView = (TextView) view.findViewById(R.id.tvElement);
viewHolder.checkBox = (CheckBox) view.findViewById(R.id.checkBox1);
viewHolder.checkBox
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
int getPosition = (Integer) buttonView.getTag();
list.get(getPosition).setSelected(
buttonView.isChecked());
/*
* Code to check if checkbox is checked on
* unchecked. If checked then, the corresponding
* entry is made in the HashMap (which contains the
* state of directories to be scanned)
*/
if (isChecked
&& !checkedList.contains(list
.get(getPosition).getFile()
.getAbsolutePath())) {
/*
* Here we can also put condition &&
* !checkedList
* .contains(list.get(getPosition).getFile
* ().getParent()) to avoid creating unnecessary
* entries
*/
checkedList.add(list.get(getPosition).getFile()
.getAbsolutePath());
} else if (!isChecked
&& checkedList.contains(list
.get(getPosition).getFile()
.getAbsolutePath())) {
checkedList.remove(list.get(getPosition)
.getFile().getAbsolutePath());
}
}
});
view.setTag(viewHolder);
view.setTag(R.id.tvElement, viewHolder.textView);
view.setTag(R.id.checkBox1, viewHolder.checkBox);
} else {
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.checkBox.setTag(position);
viewHolder.textView.setText(list.get(position).getName());
viewHolder.checkBox.setChecked(list.get(position).isSelected());
/*
* checking if the directory is already selected earlier by the user
*/
String absPath = currentDir + "/" + viewHolder.textView.getText();
if (checkedList != null
&& checkedList.contains(absPath)) {
viewHolder.checkBox.setChecked(true);
}
return view;
}
}
虽然code并进入onCheckedChangeListener在ElementAdapter而当返回流回到Activity类的列表保持原样。
推荐答案
尝试添加 notifyDataSetChanged()
在的 setListAdapter(新ElementAdapter(上下文,列表,checkedList,currentDir))
语句。
如:
ElementAdapter myAdpt = new ElementAdapter(context, list, checkedList, currentDir)
setListAdapter(myAdpt);
myAdpt.notifyDataSetChanged();
这篇关于安卓:按钮preSS后,复选框onCheckedChange不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!