有人可以告诉我,我应该如何创建类似[here] [1]的listview。
问题:如何在我的代码中实现具有图标,文件名和文件大小的外观和感觉,同时在每个文件对象上看起来干净简单,如示例中所示链接[这里] [2] ??
有人可以指导这件事,因为我在android / java中比较新...谢谢
答案 0 :(得分:0)
问题2:为什么我的列表视图 目录时出错 尽管有这个要处理,但是空的 我的xml中的“空”
你记得列出你的列表视图吗?:
@id/android:list
如果你能够进一步帮助请清除问题1 ,那么它更清晰简洁你想要的东西。
<强>更新强>
列表视图的ID应为:@id/android:list
texview的ID应为:@id/android:empty
答案 1 :(得分:0)
请参阅以下url for how to implement custom listview
<强>更新强>
public static class VideoInfo {
public String name = "";
public String size= "";
public String path = "";//add any more variables of which you want info
}
然后你在哪里创建arraylist,即getVideoFiles()
创建这个类的对象
声明ArrayList<VideoInfo> videoItems;
private void getVideoFiles(File[] videoList)
{
videoItems = new ArrayList<VideoInfo>();
for (int i = 0; i < videolist.length; i++)
{
File mfile = videoList[i];
String path = mfile.getPath();
path = path.substring(path.lastIndexOf("/", path.indexOf("."));
VideoInfo model = new VideoInfo();
model.name = path;
model.size = mfile.length();
videoItems.add(model);
}
setListAdapter(new ListViewAdapter(this, R.layout.row, videoItems));
}
现在在你的适配器中传递arraylist的这个对象以及如何在listview中设置变量已在上面的链接中给出
答案 2 :(得分:0)
您尚未创建自定义适配器以将布局合并到代码列表视图中。 这是你可以使用的东西
private class ListViewAdapter extends ArrayAdapter<Object> {
private ArrayList<Object> items;
public ListViewAdapter(Context context, int textViewResourceId,
ArrayList<Object> items) {
super(context, textViewResourceId, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
Object info = items.get(position);
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
if (info != null) {
ImageView imView = (ImageView) v.findViewById(R.id.icon);
TextView txtname =(TextView) v.findViewById(R.id.toptext);
TextView txtAddr = (TextView) v.findViewById(R.id.bottomtext);
//set image and set text as you like
}
return v;
}
}
希望这可以提供帮助。
答案 3 :(得分:0)
首先创建要为列表中的每一行显示的布局,然后将BaseAdapter类扩展为自定义列表。此类包含getView方法,可以根据需要多次复制列表中的行。
类HighScoreAdapter扩展了BaseAdapter { / * layout inflater将您的XML布局转换为View以在每行列表中呈现。* / private LayoutInflater minflator = LayoutInflater.from(HighScoreList.this); ViewHolder holder;
@Override
public Object getItem(int position)
{
return position;
}
@Override
public long getItemId(int position)
{
return position;
}
boolean toRemove = false;
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
/* first time is null */
if (convertView == null )
{
convertView = minflator.inflate(R.layout.highscorelist, null);
holder = new ViewHolder();
holder.rank = (TextView) convertView.findViewById(R.id.user_id);
holder.username = (EditText) convertView.findViewById(R.id.user_nameedit);
holder.time = (TextView) convertView.findViewById(R.id.user_time);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.rank.setText(String.valueOf(position+1)+".");
/* returns the view for next row as layout will be same i.e. this increases the your list's scrolling and working faster even though your list contains thousands of Entry*/
return convertView;
}
/* this class is to make reference to your child views of your layout by using this you can set your child views properties and Listeners acording to your need.*/
class ViewHolder
{
TextView rank;
EditText username;
TextView time;
}
}
我希望这完全解决你的问题.. :)
答案 4 :(得分:0)
将此方法添加到您的代码中,如果您收到任何错误或异常,请告知我们。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
Object info = items.get(position);
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
if (info != null) {
ImageView imView = (ImageView) v.findViewById(R.id.icon);
TextView txtname =(TextView) v.findViewById(R.id.toptext);
TextView txtAddr = (TextView) v.findViewById(R.id.bottomtext);
//set image and set text as you like
imview.setImageBitmap(IMAGE YOU WANT TO SET AAS ICON);
txtname.setText(FILENAME YOU WANT TO SET);
txtadr,setText(FILESIZE YOU WANT TO SET);
// Better you take each info filename,filesize and icon in a arraylist and set them
}
return v;
}