Android惰性图标列表视图
==编辑=== 有没有这样的东西不起作用的原因?我觉得它应该起作用,但它不起作用lView.getChildCount返回0,即使列表中有很多内容,然后这会导致强制关闭Android惰性图标列表视图,android,listview,arraylist,icons,position,Android,Listview,Arraylist,Icons,Position,==编辑=== 有没有这样的东西不起作用的原因?我觉得它应该起作用,但它不起作用lView.getChildCount返回0,即使列表中有很多内容,然后这会导致强制关闭 for (int i = 0; i < lView.getChildCount(); i++) { ImageView imageView = (ImageView) lView.getChildAt(i).findViewById(R.id.listIcon); imageView.setImageResou
for (int i = 0; i < lView.getChildCount(); i++) {
ImageView imageView = (ImageView) lView.getChildAt(i).findViewById(R.id.listIcon);
imageView.setImageResource(R.drawable.someIcon);
}
无论如何,这是我目前正在使用的代码,以备不时之需,感谢您的帮助或输入
public class SampleList extends ListActivity {
private List<String> item = null;
private List<String> path = null;
private String root = "/sdcard/somefolder/";
private String current = null;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
context = getApplicationContext();
try{
getDir(root);
File directory = new File(root);
File[] contents = directory.listFiles();
if (contents.length == 0) {
TextView t = new TextView(this);
t=(TextView)findViewById(R.id.empty);
t.setText(R.string.emptyFol);
}
}catch (Exception e) {
Toast.makeText(NandPicker.this, R.string.nMount, Toast.LENGTH_LONG).show();
}
}
private void getDir(String dirPath){
item = new ArrayList<String>();
path = new ArrayList<String>();
File f = new File(dirPath);
File[] files = f.listFiles();
for(int i=0; i < files.length; i++){
File file = files[i];
//Problem was adding the path without the /
//path.add(file.getPath());
if(file.isDirectory()){
path.add(file.getPath()+"/");
item.add(file.getName() + "/");
}else{
path.add(file.getPath());
item.add(file.getName());
}
}
current = dirPath;
ArrayAdapter<String> fileList = new ArrayAdapter<String>(this, R.layout.row, R.id.rowTextView, item);
setListAdapter(fileList);
class IgnoreCaseComparator implements Comparator<String> {
public int compare(String strA, String strB) {
return strA.compareToIgnoreCase(strB);
}
}
IgnoreCaseComparator icc = new IgnoreCaseComparator();
java.util.Collections.sort(path,icc);
java.util.Collections.sort(item,icc);
}
@Override
protected void onListItemClick(ListView l, View v, final int position, long id) {
}
}
请看一个例子。这一点经常被提及。我用一个循环编辑了我的问题,我写了一个我觉得应该可以工作的循环,但是出于某种原因getChildCount方法返回0,即使列表中有很多子项,我也会在适配器和所有设置完成后很好地运行该循环,这不应该更改每个图标吗?
public class SampleList extends ListActivity {
private List<String> item = null;
private List<String> path = null;
private String root = "/sdcard/somefolder/";
private String current = null;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
context = getApplicationContext();
try{
getDir(root);
File directory = new File(root);
File[] contents = directory.listFiles();
if (contents.length == 0) {
TextView t = new TextView(this);
t=(TextView)findViewById(R.id.empty);
t.setText(R.string.emptyFol);
}
}catch (Exception e) {
Toast.makeText(NandPicker.this, R.string.nMount, Toast.LENGTH_LONG).show();
}
}
private void getDir(String dirPath){
item = new ArrayList<String>();
path = new ArrayList<String>();
File f = new File(dirPath);
File[] files = f.listFiles();
for(int i=0; i < files.length; i++){
File file = files[i];
//Problem was adding the path without the /
//path.add(file.getPath());
if(file.isDirectory()){
path.add(file.getPath()+"/");
item.add(file.getName() + "/");
}else{
path.add(file.getPath());
item.add(file.getName());
}
}
current = dirPath;
ArrayAdapter<String> fileList = new ArrayAdapter<String>(this, R.layout.row, R.id.rowTextView, item);
setListAdapter(fileList);
class IgnoreCaseComparator implements Comparator<String> {
public int compare(String strA, String strB) {
return strA.compareToIgnoreCase(strB);
}
}
IgnoreCaseComparator icc = new IgnoreCaseComparator();
java.util.Collections.sort(path,icc);
java.util.Collections.sort(item,icc);
}
@Override
protected void onListItemClick(ListView l, View v, final int position, long id) {
}
}