0

我已经用 sdcard 内容填充了列表视图,但它在单独的列表活动中打开。我想在我自己的活动中填写我自己的名为“list_local_content”的列表视图。这是代码..

公共类 Local_Contents 扩展 ListActivity {

private File currentDir;
private FileArrayAdapter adapter;

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    currentDir = new File("/");
    fill(currentDir);
}

private void fill(File f)
{
    File[] dirs = f.listFiles();
    this.setTitle("Current Dir: "+f.getName());
    List<Option>dir = new ArrayList<Option>();
    List<Option>files = new ArrayList<Option>();
    try{
        for(File ff: dirs)
        {
            if(ff.isDirectory())
                dir.add(new Option(ff.getName(),"Folder",ff.getAbsolutePath()));
            else
                files.add(new Option(ff.getName(),"File Size: "+ff.length(),ff.getAbsolutePath()));
        }
    }catch(Exception e){

    }
    Collections.sort(dir);
    Collections.sort(files);
    dir.addAll(files);
    if(!f.getName().equalsIgnoreCase("sdcard"))
        dir.add(0,new Option("..","Parent Directory",f.getParent()));
    adapter = new FileArrayAdapter(Local_Contents.this,R.layout.listview_filler,dir);
    this.setListAdapter(adapter);

}
@Override
protected void onListItemClick(ListView l, View v, int position, long id){
super.onListItemClick(l, v, position, id);
Option o = adapter.getItem(position);
if(o.getData().equalsIgnoreCase("folder")||o.getData().equalsIgnoreCase("parent directory")){
    currentDir = new File(o.getPath());
    fill(currentDir);
}
else
    onFileClick(o);
}

private void onFileClick(Option o)
{
    Toast.makeText(this, "File Clicked: "+o.getName(), Toast.LENGTH_SHORT).show();
}

}

和其他处理 filearrayadapter 的类是

公共类 FileArrayAdapter 扩展 ArrayAdapter{

private Context c;
private int id;
private List<Option>items;

public FileArrayAdapter(Context context, int textViewResourceId, List<Option> objects)
{
    super(context, textViewResourceId, objects);
    c = context;
    id = textViewResourceId;
    items = objects;
}

public Option 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);
    }
    final Option o = items.get(position);
    if(o != null)
    {
        TextView t1 = (TextView) v.findViewById(R.id.Text_filler1);
        TextView t2 = (TextView) v.findViewById(R.id.Text_filler2);
        if(t1!=null)
            t1.setText(o.getName());
        if(t2!=null)
            t2.setText(o.getName());
    }
    return v;
}

}

如何在我的活动中填写我自己的列表视图,而减少修改此代码...

4

1 回答 1

0

首先,您需要更改从 listactivity 扩展到活动的内容,然后使用 findViewById 找到您的 listview,然后在 listview obj 上使用方法 setAdapter 并完成。

于 2013-07-29T07:31:23.807 回答