我需要一点帮助来编写课程。我想在我的listView中添加一些项目,为此我使用在这个论坛中找到的自定义适配器,但修改后供我使用:
public class AdvertisingAdapter extends cCursorAdapter {
private static final String ADMOB_PUBLISHER_ID = "YOUR_ADMOB_ID_HERE";
private final Activity activity;
private final cCursorAdapter delegate;
Context context;
static int rec = 15;
public AdvertisingAdapter(Activity activity, cCursorAdapter delegate, Context context, Cursor cursor)
{
super(context, cursor, 0);
this.activity = activity;
this.delegate = delegate;
this.context = context;
delegate.registerDataSetObserver(new DataSetObserver()
{
@Override
public void onChanged()
{
notifyDataSetChanged();
}
@Override
public void onInvalidated()
{
notifyDataSetInvalidated();
}
});
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
if (position == 0 || position % rec == 0)
{
j = j + 1 ;
if (convertView instanceof AdView)
{
notifyDataSetChanged();
return convertView;
}
else
{
AdView adView = new AdView(context);
// Disable focus for sub-views of the AdView to avoid problems with
// trackpad navigation of the list.
for (int i = 0; i < adView.getChildCount(); i++)
{
adView.getChildAt(i).setFocusable(false);
}
adView.setFocusable(false);
// Default layout params have to be converted to ListView compatible
// params otherwise there will be a ClassCastException.
float density = activity.getResources().getDisplayMetrics().density;
int height = Math.round(AdSize.BANNER.getHeight() * density);
AbsListView.LayoutParams params
= new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT,
height);
adView.setLayoutParams(params);
AdRequest adRequest = new AdRequest.Builder().build();
adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
adView.setAdSize(com.google.android.gms.ads.AdSize.BANNER);
adView.loadAd(adRequest);
notifyDataSetChanged();
return adView;
}
}
else
{
Log.i ("TEST AD ADAPTER", String.valueOf(j)
+ " " + String.valueOf(position)
+ " " + String.valueOf(getNbreAd())
+ " " + String.valueOf(getNbrePos(position))
+ " " + String.valueOf(getCount()));
return delegate.getView(position - getNbrePos(position), convertView, parent);
}
}
public int getNbreAd(){
int l = delegate.getCount();
return (l / rec) + 1;
}
public int getNbrePos(int position){
int k = (position/rec) + 1;
return k;
}
@Override
public int getCount()
{
return delegate.getCount() + getNbreAd();
}
@Override
public Object getItem(int i)
{
return delegate.getItem(i - 1);
}
@Override
public long getItemId(int i)
{
return delegate.getItemId(i - 1);
}
@Override
public int getViewTypeCount()
{
return delegate.getViewTypeCount() + 1;
}
@Override
public int getItemViewType(int position)
{
return position == 0 ? delegate.getViewTypeCount()
: delegate.getItemViewType(position - 1);
}
@Override
public boolean areAllItemsEnabled()
{
return false;
}
@Override
public boolean isEnabled(int position)
{
return position != 0 && delegate.isEnabled(position - 1);
}
}
我的cCursorAdapter:
public class cCursorAdapter extends android.widget.CursorAdapter {
DataHelper mDBHelper;
public cCursorAdapter(Context context, Cursor cursor, int flags) {
super(context, cursor, 0);
}
// The newView method is used to inflate a new view and return it,
// you don't bind any data to the view at this point.
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.list_result2, parent, false);
}
// The bindView method is used to bind all data to a given view
// such as setting the text on a TextView.
@Override
public void bindView(View view, Context context, Cursor cursor){
// Find fields to populate in inflated template
TextView nameResult = (TextView) view.findViewById(R.id.nameResult2);
TextView ageG = (TextView) view.findViewById(R.id.ageG2);
TextView nbreJoueur = (TextView) view.findViewById(R.id.nbreJoueur2);
TextView timeResult = (TextView) view.findViewById(R.id.time2);
ImageView placeGD = (ImageView) view.findViewById(R.id.placeD2);
TextView IDR2 = (TextView) view.findViewById(R.id.IDR2);
// Extract properties from cursor
String name = cursor.getString(cursor.getColumnIndexOrThrow("name"));
String place = cursor.getString(cursor.getColumnIndexOrThrow("place"));
int ageMin = cursor.getInt(cursor.getColumnIndexOrThrow("agemin"));
int ageMax = cursor.getInt(cursor.getColumnIndexOrThrow("agemax"));
int nbreMin = cursor.getInt(cursor.getColumnIndexOrThrow("nbremin"));
int nbreMax = cursor.getInt(cursor.getColumnIndexOrThrow("nbremax"));
int time = cursor.getInt(cursor.getColumnIndexOrThrow("time"));
final int ID = cursor.getInt(cursor.getColumnIndexOrThrow("_id"));
// Populate fields with extracted properties
nameResult.setText(name);
ageG.setText(String.valueOf(ageMin) + " - " + String.valueOf(ageMax));
nbreJoueur.setText(String.valueOf(nbreMin) + " - " + String.valueOf(nbreMax));
timeResult.setText(String.valueOf(time));
IDR2.setText(String.valueOf(ID));
if (place.equals("extérieur")) {
Drawable placeD = MainActivity.mContext.getResources().getDrawable(R.drawable.ic_wb_sunny_black_24dp);
placeGD.setImageDrawable(placeD);
} else if (place.equals("intérieur")){
Drawable placeD = MainActivity.mContext.getResources().getDrawable(R.drawable.ic_event_seat_black_24dp);
placeGD.setImageDrawable(placeD);
} else if (place.equals("partout")) {
Drawable placeD = MainActivity.mContext.getResources().getDrawable(R.drawable.ic_explore_black_24dp);
placeGD.setImageDrawable(placeD);
}
//Cacher l'ID (c'est pas joli)
IDR2.setVisibility(View.INVISIBLE);
}
//Override the convert to string sequence
@Override
public CharSequence convertToString(Cursor cursor){
final int colIndex = cursor.getColumnIndexOrThrow("name");
return cursor.getString(colIndex);
}
@Override
public Cursor runQueryOnBackgroundThread(CharSequence constraint)
{
Cursor currentCursor = null;
if (getFilterQueryProvider() != null)
{
return getFilterQueryProvider().runQuery(constraint);
}
String args = "";
if (constraint != null)
{
args = constraint.toString();
}
//Initialize that shit
mDBHelper = new DataHelper(MainActivity.mContext);
currentCursor = mDBHelper.getNameCursor(args);
return currentCursor;
}
}
以某种方式,它有效。但是当我快速滑动listView时,我有这个错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.tanoshi.traveler.doctorgamesfree.cCursorAdapter.bindView(cCursorAdapter.java:57)
at android.widget.CursorAdapter.getView(CursorAdapter.java:254)
at com.tanoshi.traveler.doctorgamesfree.AdvertisingAdapter.getView(AdvertisingAdapter.java:102)
我认为这是因为我的listView尝试在未加载的视图中加载项目。 我该怎么做才能解决这个问题?
感谢您的时间。