我使用多选file-chooser从我的sdcard文件夹中选择了一组项目。它返回精确选择的值,但是在滚动列表视图的同时动态更改选择突出显示的项目(使用颜色)。任何人都可以告诉我如何解决它?
这里我添加了我的文件选择器类和适配器类..
public class FileChooser extends ListActivity {
private File currentDir;
private FileArrayAdapter adapter;
private Bundle selectedfiles;
String selectedFileAbspath;
ArrayList<String> images_arr = new ArrayList<String>();
boolean is_multiple;
Button file_upload;
Integer val;
Pattern fileExtnPtrn;;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
is_multiple = this.getIntent().getExtras()
.getBoolean("is_multiple", false);
val = this.getIntent().getExtras()
.getInt("value");
if (is_multiple)
{
setContentView(R.layout.file_chooser_list);
file_upload = (Button) findViewById(R.id.file_upload);
file_upload.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
if(selectedfiles.size()!=0)
{
Intent i = new Intent();
i.putExtra("selected_files", selectedfiles);
setResult(RESULT_OK, i);
finish();
}
else
{
showDialog("Please Choose Atleast one File");
}
}
});
}
currentDir = new File("/sdcard/");
selectedfiles = new Bundle();
fill(currentDir);
}
private void fill(File f)
{
File[]dirs = f.listFiles();
this.setTitle("Current Dir: "+f.getName());
List<Item>dir = new ArrayList<Item>();
List<Item>fls = new ArrayList<Item>();
try
{
for(File ff: dirs)
{
Date lastModDate = new Date(ff.lastModified());
DateFormat formater = DateFormat.getDateTimeInstance();
String date_modify = formater.format(lastModDate);
if(ff.isDirectory())
{
File[] fbuf = ff.listFiles();
int buf = 0;
if(fbuf != null)
{
buf = fbuf.length;
}
else buf = 0;
String num_item = String.valueOf(buf);
if(buf == 0)
num_item = num_item + " item";
else
num_item = num_item + " items";
//String formated = lastModDate.toString();
dir.add(new Item(ff.getName(),num_item,date_modify,ff.getAbsolutePath(),"directory_icon"));
}
else
{
fls.add(new Item(ff.getName(),ff.length() + " Byte", date_modify, ff.getAbsolutePath(),"file_icon"));
}
}
}
catch(Exception e)
{
}
Collections.sort(dir);
Collections.sort(fls);
dir.addAll(fls);
if(!f.getName().equalsIgnoreCase("sdcard"))
dir.add(0,new Item("..","Parent Directory","",f.getParent(),"directory_up"));
adapter = new FileArrayAdapter(FileChooser.this,R.layout.file_view,dir);
this.setListAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
Item o = adapter.getItem(position);
if(o.getImage().equalsIgnoreCase("directory_icon")||o.getImage().equalsIgnoreCase("directory_up")){
currentDir = new File(o.getPath());
fill(currentDir);
}
else
{
onFileClick(o,v);
}
}
private void onFileClick(Item o,View v)
{
selectedFileAbspath = currentDir.toString() + "/" + o.getName();
RelativeLayout list_item=(RelativeLayout) v;
if(is_multiple)
{
if(validateFileExt(selectedFileAbspath))
{
if (selectedfiles.containsKey(selectedFileAbspath))
{
selectedfiles.remove(selectedFileAbspath);
list_item.setBackgroundColor(getResources().getColor(R.color.white));
}
else
{
selectedfiles.putString(selectedFileAbspath, "1");
list_item.setBackgroundColor(getResources().getColor(R.color.red));
}
}
else
{
showDialog("please select valid image file");
}
}
else
{
if(validateFileExt(selectedFileAbspath))
{
Intent intent = new Intent();
intent.putExtra("GetPath", currentDir.toString());
intent.putExtra("GetFileName", o.getName());
setResult(RESULT_OK, intent);
finish();
}
else
{
Intent intent = new Intent();
setResult(RESULT_CANCELED, intent);
finish();
}
}
}
private boolean validateFileExt(String fileName)
{
fileExtnPtrn = Pattern.compile("([^\\s]+(\\.(?i)(jpg|png))$)");
Matcher mtch = fileExtnPtrn.matcher(fileName);
return mtch.matches();
}
private void showDialog(String string)
{
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle(string);
alertDialog.setPositiveButton("Okay",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
alertDialog.show();
}
}
适配器类
public class FileArrayAdapter extends ArrayAdapter {
private Context c;
private int id;
private List<Item>items;
public FileArrayAdapter(Context context, int textViewResourceId,
List<Item> objects) {
super(context, textViewResourceId, objects);
c = context;
id = textViewResourceId;
items = objects;
}
public Item getItem(int i)
{
return items.get(i);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(id, null);
}
/* create a new view of my layout and inflate it in the row */
//convertView = ( RelativeLayout ) inflater.inflate( resource, null );
final Item o = items.get(position);
if (o != null) {
TextView t1 = (TextView) v.findViewById(R.id.TextView01);
TextView t2 = (TextView) v.findViewById(R.id.TextView02);
TextView t3 = (TextView) v.findViewById(R.id.TextViewDate);
/* Take the ImageView from layout and set the city's image */
ImageView imageCity = (ImageView) v.findViewById(R.id.fd_Icon1);
String uri = "drawable/" + o.getImage();
int imageResource = c.getResources().getIdentifier(uri, null, c.getPackageName());
Drawable image = c.getResources().getDrawable(imageResource);
imageCity.setImageDrawable(image);
if(t1!=null)
t1.setText(o.getName());
if(t2!=null)
t2.setText(o.getData());
if(t3!=null)
t3.setText(o.getDate());
}
return v;
}
}
答案 0 :(得分:0)
列表的ListAdapter中的getItem方法中的问题
答案 1 :(得分:0)
尝试此适配器)您必须确定案例中每个项目的所有元素)
public class FileArrayAdapter extends ArrayAdapter{
private Context c;
private int id;
private List<Item>items;
public FileArrayAdapter(Context context, int textViewResourceId,
List<Item> objects) {
super(context, textViewResourceId, objects);
c = context;
id = textViewResourceId;
items = objects;
}
public Item getItem(int i)
{
return items.get(i);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(id, null);
}
/* create a new view of my layout and inflate it in the row */
TextView t1 = (TextView) v.findViewById(R.id.TextView01);
TextView t2 = (TextView) v.findViewById(R.id.TextView02);
TextView t3 = (TextView) v.findViewById(R.id.TextViewDate);
/* Take the ImageView from layout and set the city's image */
ImageView imageCity = (ImageView) v.findViewById(R.id.fd_Icon1);
String uri = "drawable/" + o.getImage();
int imageResource = c.getResources().getIdentifier(uri, null, c.getPackageName());
Drawable image = c.getResources().getDrawable(imageResource);
imageCity.setImageDrawable(image);
if(t1!=null)
t1.setText(o.getName());
if(t2!=null)
t2.setText(o.getData());
if(t3!=null)
t3.setText(o.getDate());
return v;
}
}