Android - custom ArrayAdapter
Can someone help me solve my problem. I am still trying to work with DraggingListView
and ArrayAdapter
. Now I want to implement the delete element from listview
click, but when I do:
StableArrayAdapter.this.notifyDataSetChanged();
i get nullPointer Exception
...
here is my adapter:
public class StableArrayAdapter extends ArrayAdapter<Product> {
final int INVALID_ID = -1;
LayoutInflater lInflater;
Context ctx;
public static final String PREFS_NAME = "com.shvedchenko.skleroshop";
public static final String PREFS_THEME = "theme";
HashMap<Product, Integer> mIdMap = new HashMap<Product, Integer>();
public StableArrayAdapter(Context context, int textViewResourceId, List<Product> prod) {
super(context, textViewResourceId, /*objects*/prod);
lInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ctx = context;
for (int i = 0; i < prod.size(); ++i) {
mIdMap.put(prod.get(i), i);
}
}
//
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
// , view
View view = convertView;
if (view == null) {
view = lInflater.inflate(R.layout.item, parent, false);
}
Product p = getItem(position);
SharedPreferences pref = ctx.getSharedPreferences(PREFS_NAME, ctx.MODE_PRIVATE);
int theme = pref.getInt(PREFS_THEME, 0); // getting Integer
if(theme == 0)
((TextView) view.findViewById(R.id.tvDescr)).setTextColor(Color.WHITE);
else
((TextView) view.findViewById(R.id.tvDescr)).setTextColor(Color.BLACK);
((TextView) view.findViewById(R.id.tvDescr)).setText(p.getProductName());
((ImageView) view.findViewById(R.id.ivImage)).setImageResource(p.getProductImage());
ImageView iv = (ImageView)view.findViewById(R.id.ivImage);
iv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mIdMap.remove(getItem(position));
System.out.println("FROM CLICK -- " + mIdMap.size() );
/*for( Product p : mIdMap.keySet() ) {
System.out.println( p.getProductName() );
}*/
//StableArrayAdapter.this.notifyDataSetChanged();
}
});
return view;
}
/*@Override
public Object getItem(int position) {
return mIdMap.get(position);
}*/
@Override
public long getItemId(int position) {
if (position < 0 || position >= mIdMap.size()) {
return INVALID_ID;
}
Product item = (Product) getItem(position);
return mIdMap.get(item);
}
@Override
public boolean hasStableIds() {
return true;
}
}
here is the error:
08-26 08: 50: 58.902 2167-2167 / com.shvedchenko.skleroshop E / AndroidRuntime: FATAL EXCEPTION: main java.lang.NullPointerException at com.shvedchenko.skleroshop.StableArrayAdapter.getItemId (StableArrayAdapter. androidjava:104) widget.AdapterView.rememberSyncState (AdapterView.java:1195) in android.widget.AdapterView $ AdapterDataSetObserver.onChanged (AdapterView.java:810) in android.widget.AbsListView $ AdapterDataSetObserver.onChanged (Absolutely 95ist ).javaatabase .DataSetObservable.notifyChanged (DataSetObservable.java:37) at android.widget.BaseAdapter.notifyDataSetChanged (BaseAdapter.java:50) at android.widget.ArrayAdapter.notifyDataSetChanged (ArrayAdapter.javachenko:286) at comrayAdapter.javachenko:286) at comrayAdapter.javachenko:286) .onClick (StableArrayAdapter.java: 85) to android.view.View.performClick (View.java:4204) to android.view.View $ PerformClick.run (View.java:17355) to android.os.Handler.handleCallback (Handler.java:725 ) to android.os.Handler.dispatchMessage (Handler.java:92) to android.os.Looper.loop (Looper.java:137) to android.app.ActivityThread.main (ActivityThread.java:5041) to java.lang .reflect.Method.invokeNative (Native Method) at java.lang.reflect.Method.invoke (Method.java:511) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:793) at com .android.internal.os.ZygoteInit.main (ZygoteInit.java:560) in dalvik.system.NativeStart.main (native method)View $ PerformClick.run (View.java:17355) on android.os.Handler.handleCallback (Handler.java:725) on android.os.Handler.dispatchMessage (Handler.java:92) on android.os.Looper.loop (Looper.java:137) in android.app.ActivityThread.main (ActivityThread.java:5041) in java.lang.reflect.Method.invokeNative (native method) in java.lang.reflect.Method.invoke (Method.java : 511) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:793) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:560) at dalvik.system.NativeStart. main (native method)View $ PerformClick.run (View.java:17355) on android.os.Handler.handleCallback (Handler.java:725) on android.os.Handler.dispatchMessage (Handler.java:92) on android.os.Looper.loop (Looper.java:137) at android.app.ActivityThread.main (ActivityThread.java:5041) at java.lang.reflect.Method.invokeNative (native method) at java.lang.reflect.Method.invoke (Method.java : 511) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:793) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:560) at dalvik.system.NativeStart. main (native method)92) at android.os.Looper.loop (Looper.java:137) at android.app.ActivityThread.main (ActivityThread.java:5041) at java.lang.reflect.Method.invokeNative (native method) at java.lang .reflect.Method.invoke (Method.java:511) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:793) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java : 560) in dalvik.system.NativeStart.main (native method)92) at android.os.Looper.loop (Looper.java:137) at android.app.ActivityThread.main (ActivityThread.java:5041) at java.lang.reflect.Method.invokeNative (native method) at java.lang .reflect.Method.invoke (Method.java:511) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:793) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java : 560) in dalvik.system.NativeStart.main (native method)android.internal.os.ZygoteInit.main (ZygoteInit.java:560) in dalvik.system.NativeStart.main (native method)android.internal.os.ZygoteInit.main (ZygoteInit.java:560) in dalvik.system.NativeStart.main (native method)
/ UPDATED /
My adapter now:
public class StableArrayAdapter extends ArrayAdapter<Product> {
final int INVALID_ID = -1;
LayoutInflater lInflater;
Context ctx;
public static final String PREFS_NAME = "com.shvedchenko.skleroshop";
public static final String PREFS_THEME = "theme";
HashMap<Product, Integer> mIdMap = new HashMap<Product, Integer>();
ArrayList<Product> prods = new ArrayList<Product>();
public StableArrayAdapter(Context context, int textViewResourceId, List<Product> prod) {
super(context, textViewResourceId, /*objects*/prod);
lInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ctx = context;
for (int i = 0; i < prod.size(); ++i) {
mIdMap.put(prod.get(i), i);
prods.add(i,prod.get(i));
}
}
//
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
// , view
View view = convertView;
if (view == null) {
view = lInflater.inflate(R.layout.item, parent, false);
}
Product p = getItem(position);
SharedPreferences pref = ctx.getSharedPreferences(PREFS_NAME, ctx.MODE_PRIVATE);
int theme = pref.getInt(PREFS_THEME, 0); // getting Integer
if(theme == 0)
((TextView) view.findViewById(R.id.tvDescr)).setTextColor(Color.WHITE);
else
((TextView) view.findViewById(R.id.tvDescr)).setTextColor(Color.BLACK);
((TextView) view.findViewById(R.id.tvDescr)).setText(p.getProductName());
((ImageView) view.findViewById(R.id.ivImage)).setImageResource(p.getProductImage());
ImageView iv = (ImageView)view.findViewById(R.id.ivImage);
iv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
prods.remove(position);
StableArrayAdapter.this.notifyData(prods);
}
});
return view;
}
public void notifyData(List<Product> prod) {
//First of all Clear Map
mIdMap.clear();
for (int i = 0; i < prod.size(); ++i) {
mIdMap.put(prod.get(i), i);
}
notifyDataSetChanged();
}
@Override
public long getItemId(int position) {
if (position < 0 || position >= mIdMap.size()) {
return INVALID_ID;
}
Product item = (Product) getItem(position);
return mIdMap.get(item);
}
@Override
public boolean hasStableIds() {
return true;
}
}
/ ERROR /
08-26 09: 53: 19.270 2530-2530 / com.shvedchenko.skleroshop E / AndroidRuntime: FATAL EXCEPTION: main java.lang.NullPointerException at com.shvedchenko.skleroshop.StableArrayAdapter.getItemId (StableArrayAdapter.java:122) at android. widget.AdapterView.rememberSyncState (AdapterView.java:1195) in android.widget.AdapterView $ AdapterDataSetObserver.onChanged (AdapterView.java:810) in android.widget.AbsListView $ AdapterDataSetObserver.onChanged (Absolutely 95ist ).javaatabase .DataSetObservable.notifyChanged (DataSetObservable.java:37) at android.widget.BaseAdapter.notifyDataSetChanged (BaseAdapter.java:50) at android.widget.ArrayAdapter.notifyDataSetChanged (ArrayAdapter.javachenko:286). notifyData (StableArrayAdapter.java: 112) at com.shvedchenko.skleroshop.StableArrayAdapter $ 1.onClick (StableArrayAdapter.java:91)
What am I doing wrong?
TNX in advance!
source to share
Correct adapter code:
public class StableArrayAdapter extends ArrayAdapter<Product> {
final int INVALID_ID = -1;
LayoutInflater lInflater;
Context ctx;
public static final String PREFS_NAME = "com.shvedchenko.skleroshop";
public static final String PREFS_THEME = "theme";
HashMap<Product, Integer> mIdMap = new HashMap<Product, Integer>();
public StableArrayAdapter(Context context, int textViewResourceId, List<Product> prod) {
super(context, textViewResourceId, /*objects*/prod);
lInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ctx = context;
for (int i = 0; i < prod.size(); i++) {
mIdMap.put(prod.get(i), i);
}
}
//
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
// , view
View view = convertView;
if (view == null) {
view = lInflater.inflate(R.layout.item, parent, false);
}
Product p = getItem(position);
SharedPreferences pref = ctx.getSharedPreferences(PREFS_NAME, ctx.MODE_PRIVATE);
int theme = pref.getInt(PREFS_THEME, 0); // getting Integer
if(theme == 0)
((TextView) view.findViewById(R.id.tvDescr)).setTextColor(Color.WHITE);
else
((TextView) view.findViewById(R.id.tvDescr)).setTextColor(Color.BLACK);
((TextView) view.findViewById(R.id.tvDescr)).setText(p.getProductName());
((ImageView) view.findViewById(R.id.ivImage)).setImageResource(p.getProductImage());
ImageView iv = (ImageView)view.findViewById(R.id.ivImage);
iv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
StableArrayAdapter.this.remove(getItem(position));
StableArrayAdapter.this.notifyDataSetChanged();
}
});
return view;
}
@Override
public long getItemId(int position) {
if (position < 0 || position >= mIdMap.size()) {
return INVALID_ID;
}
Product item = (Product) getItem(position);
return mIdMap.get(item);
}
@Override
public boolean hasStableIds() {
return true;
}
}
source to share
Remove item from list
list.remove(position);
Then call the method below using the adapter object.
stableAdapter.notifyData(list);
Write a method in the adapter class.
public void notifyData(List<Product> prod)
{
//First of all Clear Map
mIdMap.clear();
for (int i = 0; i < prod.size(); ++i) {
mIdMap.put(prod.get(i), i);
}
notifyDataSetChanged();
}
Call this method using adpater object
source to share