我有以下源代码运行完美。它做的不多,但它运行我可以指向,点击和摆弄。
一旦我取消评论:
listview.setAdapter(adapter);
所有的地狱刹车都松了,我真的不明白为什么。
05-22 18:12:52.962: E/AndroidRuntime(27603): FATAL EXCEPTION: main
05-22 18:12:52.962: E/AndroidRuntime(27603): android.content.res.Resources$NotFoundException: Resource ID #0x7f080001 type #0x12 is not valid
我完全不知道这种高级语言在我脸上吐什么。Java,我恨你。但我们彼此纠缠不清,所以如果有人能帮助我,那就太好了。
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView listview = (ListView) findViewById(R.id.theList);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux",
"OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2",
"Android", "iPhone", "WindowsMobile" };
final ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < values.length; ++i) {
list.add(values[i]);
}
final StableArrayAdapter adapter = new StableArrayAdapter(this, R.id.theList, list);
//listview.setAdapter(adapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
final String item = (String) parent.getItemAtPosition(position);
Log.d("Test", item);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void listFiles() {
File file = new File(Environment.getExternalStorageDirectory(), "");
File files[] = file.listFiles();
for (int i=0; i < files.length; i++)
{
String filename = files[i].getName();
Integer pos = filename.indexOf(".pdf");
if (pos >= 0) {
Log.d("Files", "FileName:" + filename);
//listItems.add(filename);
//adapter.notifyDataSetChanged();
}
}
}
private class StableArrayAdapter extends ArrayAdapter<String> {
HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();
public StableArrayAdapter(Context context, int textViewResourceId,
List<String> objects) {
super(context, textViewResourceId, objects);
for (int i = 0; i < objects.size(); ++i) {
mIdMap.put(objects.get(i), i);
}
}
@Override
public long getItemId(int position) {
String item = getItem(position);
return mIdMap.get(item);
}
@Override
public boolean hasStableIds() {
return true;
}
}
}
我想要做的只是将项目添加到我的名为@+id/theList
. 它看起来比我做过的任何事情都要难..由于某种原因,该列表在我的应用程序中不可见,即使它似乎是用项目填充的(在添加循环中运行 .log 函数,之后的行是 .补充一点也不例外)