0

我有以下源代码运行完美。它做的不多,但它运行我可以指向,点击和摆弄。

一旦我取消评论:

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 函数,之后的行是 .补充一点也不例外)

4

3 回答 3

1

你不应该使用这个构造函数吗?

ArrayAdapter(Context context, int resource, int textViewResourceId, List<T> objects)

所以你可以做

new StableArrayAdapter(this,android.R.layout.simple_list_item_1,android.r.id.text1,list);

you really dont even need that custom class if all you are doing is just displaying text in a row and you can just use SimpleAdapter

于 2013-05-22T16:20:30.730 回答
1

Okay, so let's look at what you are doing here:

final StableArrayAdapter adapter = new StableArrayAdapter(this, R.id.theList, list); //listview.setAdapter(adapter);

You don't override any of the array adapter constructors, so you're calling this array adapter constructor:

public ArrayAdapter (Context context, int textViewResourceId, T[] objects) here

So, what are the arguments you are passing in?

  • context is your activity (this) which is fine
  • textViewResourceId is your list id.
  • objects is your list

The problem is the second argument textViewResourceId. This is a bit misleading in this context since it is not asking for the resource id of the your actual list. It's looking for a resource id of how the row items are laid out.

android.R.layout.simple_list_item_1 is a default layout of items in a listview.

You could define your own layout, for example,if you wanted to have an icon or more text or whatever.

But this is why setAdapter is throwing an exception.

于 2013-05-22T16:36:50.617 回答
0

To use the default implementation of ArrayAdapter, the view passed in the constructor (textViewResourceId) should be a layout element that contains an text view element named "text1".

As @tyczj suggested, if your are wanting a simple basic list, you can use "android.R.layout.simple_list_item_1". If you look in the source code it is a layout element that has only one child element. A text view named "text1".

于 2013-05-22T16:31:43.460 回答