Alb*_*ias 39 android listview adapter android-adapterview
我正在为Android做一个应用程序,我需要的是它显示了SD卡中所有文件和目录的列表,它必须能够在不同的目录中移动.我在anddev找到了一个很好的教程.我修改了一些东西,所以应用程序在SD卡中移动而不是在Android根目录中,但其余部分基本相同.
这是我的活动的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</ListView>
Run Code Online (Sandbox Code Playgroud)
这是活动的代码:
import hackreatorz.cifrador.R;
import java.io.File;
import java.util.ArrayList;
import android.app.ListActivity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class ArchivosSD extends ListActivity {
private ArrayList<String> directoryEntries = new ArrayList<String>();
private File currentDirectory = new File("/sdcard/");
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
browseToSD();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
private void browseToSD() {
browseTo(new File("/sdcard/"));
}
private void upOneLevel() {
if(this.currentDirectory.getParent() != null)
this.browseTo(this.currentDirectory.getParentFile());
}
private void browseTo(final File directory) {
if (directory.isDirectory()) {
this.currentDirectory = directory;
fill(directory.listFiles());
} else {
Toast.makeText(ArchivosSD.this, this.directoryEntries.get(this.getSelectedItemPosition()), Toast.LENGTH_SHORT).show();
}
}
private void fill(File[] files) {
this.directoryEntries.clear();
this.directoryEntries.add(getString(R.string.current_dir));
if(this.currentDirectory.getParent() != null)
this.directoryEntries.add(getString(R.string.up_one_level));
int currentPathStringLength = (int) this.currentDirectory.getAbsoluteFile().length();
for (File file : files) {
this.directoryEntries.add(file.getAbsolutePath().substring(currentPathStringLength));
}
setListAdapter(new ArrayAdapter<String>(this, R.layout.archivos_sd, this.directoryEntries));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
int selectionRowID = (int) this.getSelectedItemPosition();
String selectedFileString = this.directoryEntries.get(selectionRowID);
if (selectedFileString.equals(".")) {
this.browseToSD();
} else if(selectedFileString.equals("..")) {
this.upOneLevel();
} else {
File clickedFile = null;
clickedFile = new File(this.currentDirectory.getAbsolutePath() + this.directoryEntries.get(selectionRowID));
if(clickedFile != null)
this.browseTo(clickedFile);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我没有在Eclipse中遇到任何错误,但是当我在手机上运行应用程序时,我得到一个强制关闭,当我看到Logcat时,我看到以下内容:
01-01 23:30:29.858:ERROR/AndroidRuntime(14911):FATAL EXCEPTION:main 01-01 23:30:29.858:ERROR/AndroidRuntime(14911):java.lang.UnsupportedOperationException:addView(View,LayoutParams)不是AdapterView支持
我不知道该怎么做,我在谷歌中查了一下,但我没有找到任何东西,而且我在stackoverflow上做了同样的事情.这是我在Java和Android中的第一个应用程序,所以我是一个真正的n00b,如果答案就在那里,我不理解它,所以如果你能解释我应该做些什么来解决这个错误以及为什么我真的很感激.
提前感谢您的一切.
Ros*_*one 71
对于有这个问题且在ArrayAdapter
's中夸大布局的其他人getView
,请将parent
参数设置为null
,如view = inflater.inflate(R.layout.mylayout, null);
Tob*_*run 68
你应该总是尝试在膨胀视图时传入父级,否则框架将无法识别用于该膨胀视图的布局参数.
处理异常的正确方法是将false作为第三个参数传递,此标志指示是否应将视图直接添加到传递的容器中.
对于适配器,适配器本身将在您从getview方法返回视图后添加视图:
view = inflater.inflate(R.layout.mylayout,parent,false);
BaseAdapter示例来自just-for-us DevBytes:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View result = convertView;
if (result == null) {
result = mInflater.inflate(R.layout.grid_item, parent, false);
}
// Try to get view cache or create a new one if needed
ViewCache viewCache = (ViewCache) result.getTag();
if (viewCache == null) {
viewCache = new ViewCache(result);
result.setTag(viewCache);
}
// Fetch item
Coupon coupon = getItem(position);
// Bind the data
viewCache.mTitleView.setText(coupon.mTitle);
viewCache.mSubtitleView.setText(coupon.mSubtitle);
viewCache.mImageView.setImageURI(coupon.mImageUri);
return result;
}
Run Code Online (Sandbox Code Playgroud)
CursorAdapter示例:
@Override
public View newView(final Context context, final Cursor cursor, final ViewGroup root) {
return mInflater.inflate(R.layout.list_item_ticket, root, false);
}
Run Code Online (Sandbox Code Playgroud)
有关视图通胀的更多信息,请参阅本文.
And*_*bbs 14
当创建一个ArrayAdapter像你这样的,一个ArrayAdapter需要一个布局资源只包含一个TextView. 尝试使用以下命令更改ArrayAdapter创建:
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
this.directoryEntries));
Run Code Online (Sandbox Code Playgroud)
看看它是否有效
我的问题是这样的:我覆盖了 arrayAdapter 中的 getView。然后当我膨胀视图时,我忘记将附加到 root设置为 false并导致此异常。
必须这样做:
convertView = LayoutInflater.from(getContext()).inflate(R.layout.market_search_product_view, parent,false);
Run Code Online (Sandbox Code Playgroud)
问题解决后!
归档时间: |
|
查看次数: |
54836 次 |
最近记录: |