I hope you can help me with this newbie q:) I'm using Android Studio. I'm trying to use an ArrayAdapter within setOnItemClickListener but got an error and I dont know what I'm doing wrong here. Please help me:)

This "listAdapter = new ArrayAdapter(this, R.layout.simplerow, arrayView);" gives this Error:

cannot resolve constructor 'ArrayAdapter(android.widget.AdapterView.OnItemClickListener, int, java.util.Arraylist)'

-----MyCode------

mainListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView<?> arg0, View v, int position,
                            long arg3)
    {
        String name = arg0.getItemAtPosition(position).toString();
        //Log.d("name", "name: "+name);
        int j = 0;
        while (j < livsmedelsNameValues.size()) {
            if (livsmedelsNameValues.get(j).toString().equalsIgnoreCase("Foodlist: "+name)) {
                for (int i = 0; i < 62; i++) {
                    test[0] = livsmedelsNameValues.get(i+j);
                    //Log.d("test", "test: " + test[0]);
                    arrayView.add(test[0].toString());
                }
            }
            j++;
        }
        listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, arrayView);


        mainListView.setAdapter( listAdapter );
    }

});
有帮助吗?

解决方案 2

listAdapter = new ArrayAdapter(this, R.layout.simplerow, arrayView);

here 1st parameter for ArrayAdapter must be context. so in you onclick listener "this" means OnItemClickListener not Context. use "this" if code inside Activity class.

now use getBaseContext() or getApplicationContext() or your activity.this or getActivity() instead of "this" so use

listAdapter = new ArrayAdapter(getBaseContext(),

R.layout.simplerow, arrayView);

or

listAdapter = new ArrayAdapter(getApplicationContext(),R.layout.simplerow, arrayView);

其他提示

You have

listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, arrayView);

this is not a valid context. Use ActivityName.this or getActivity() if in fragment

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top