문제

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