不是封闭类错误(Android)

时间:2015-08-13 07:29:07

标签: android android-activity android-adapter

在下面的一段代码中,我在自定义适配器中尝试传递DisplayWishesActivity.this时出错。 DisplayWishesActivity是另一项尚未修改的活动。 (请参阅错误行的代码)

import android.app.Activity;
import android.content.Context;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

import data.DatabaseHandler;
import model.MyWish;


public class WishDetailActivity extends ActionBarActivity {
    private DatabaseHandler databaseHandler;
    private ArrayList<MyWish> wishList;
    private WishAdapter wishAdapter;
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wish_detail);

        wishList = new ArrayList<>();
        listView = (ListView) findViewById(R.id.list_id);

        refreshData();
    }

    private void refreshData(){
        wishList.clear();

        databaseHandler = new DatabaseHandler(getApplicationContext());

        ArrayList<MyWish> wishesFromDB = databaseHandler.getWishes();

        for(MyWish wish : wishesFromDB){
            String title = wish.getTitle();
            String date = wish.getRecordDate();
            String content = wish.getContent();

            MyWish myWish = new MyWish();

            myWish.setTitle(title);
            myWish.setContent(content);
            myWish.setRecordDate(date);

            wishList.add(myWish);

        }
        databaseHandler.close();

        //setup adapter

//*******ERROR********* -->>"DisplayWishesActivity is not an enclosing class"            

        wishAdapter = new WishAdapter(DisplayWishesActivity.this, R.layout.wish_row, wishList);  
    }


    public class WishAdapter extends ArrayAdapter<MyWish>{          //Private Class
        Activity activity;
        int layoutResource;
        MyWish wish;
        ArrayList<MyWish> mData = new ArrayList<>();

        public WishAdapter(Activity act, int resource, ArrayList<MyWish> data) {
            super(act, resource, data);

            activity = act;
            layoutResource = resource;
            mData = data;

            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return mData.size();
        }

        @Override
        public MyWish getItem(int position) {
            return mData.get(position);
        }

        @Override
        public int getPosition(MyWish item) {
            return super.getPosition(item);
        }

        @Override
        public long getItemId(int position) {
            return super.getItemId(position);
        }


        //This method is the heart of our custom adapter class, as this is the place where our custom ListView is created

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row = convertView;
            ViewHolder holder;

            if(row == null || row.getTag() == null){              // i.e. the our list view (row) is currently empty
                LayoutInflater inflater = LayoutInflater.from(activity);
                row = inflater.inflate(layoutResource, null);
                holder = new ViewHolder();

                holder.mTitle = (TextView) row.findViewById(R.id.wish_title_in_list_id);
                holder.mDate = (TextView) row.findViewById(R.id.date_in_list_id);

                row.setTag(holder);
            }

            else{
                holder = (ViewHolder) row.getTag();         //prevents the system from doing all the work again, but instead recycles the views
            }

            holder.myWish = getItem(position);

            holder.mTitle.setText(holder.myWish.getTitle());
            holder.mDate.setText(holder.myWish.getRecordDate());

            return row;
        }
    }


    class ViewHolder{              //helps in recycling the listView, rather than instantiating it every time
        MyWish myWish;
        TextView mTitle;                         
        TextView mId;
        TextView mContent;
        TextView mDate;
    }

}

1 个答案:

答案 0 :(得分:1)

我不知道是什么让你这样做但这是错误的。语法:

ClassName.this

只能在ClassName课程中使用。在您的情况下,当您尝试使用

DisplayWishesActivity.this

它会出错,因为您不在DisplayWishesActivity课程中。您需要DisplayWishesActivity类的实例来调用WishAdapter类的构造函数。尝试创建DisplayWishesActivity类的实例,或者只找到要替换的类的实例

DisplayWishesActivity.this

这应该可以解决问题。