在下面的一段代碼中,當我嘗試傳遞DisplayWishesActivity.this時,在我的自定義適配器中出現錯誤。 DisplayWishesActivity是另一項尚未修改的活動。 (請參閱錯誤行的代碼)不是封閉類錯誤(Android)
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;
}
}
你可以發佈你的logcat –