Reputation: 389
Could anybody help me solve my problem.
I still trying work with DraggingListView
and ArrayAdapter
.
Now i want realize delete element from listview
by click, but when i making :
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.java:104) at android.widget.AdapterView.rememberSyncState(AdapterView.java:1195) at android.widget.AdapterView$AdapterDataSetObserver.onChanged(AdapterView.java:810) at android.widget.AbsListView$AdapterDataSetObserver.onChanged(AbsListView.java:5958) at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37) at android.widget.BaseAdapter.notifyDataSetChanged(BaseAdapter.java:50) at android.widget.ArrayAdapter.notifyDataSetChanged(ArrayAdapter.java:286) at com.shvedchenko.skleroshop.StableArrayAdapter$1.onClick(StableArrayAdapter.java:85) at android.view.View.performClick(View.java:4204) at android.view.View$PerformClick.run(View.java:17355) at android.os.Handler.handleCallback(Handler.java:725) at android.os.Handler.dispatchMessage(Handler.java: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) at 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) at android.widget.AdapterView$AdapterDataSetObserver.onChanged(AdapterView.java:810) at android.widget.AbsListView$AdapterDataSetObserver.onChanged(AbsListView.java:5958) at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37) at android.widget.BaseAdapter.notifyDataSetChanged(BaseAdapter.java:50) at android.widget.ArrayAdapter.notifyDataSetChanged(ArrayAdapter.java:286) at com.shvedchenko.skleroshop.StableArrayAdapter.notifyData(StableArrayAdapter.java:112) at com.shvedchenko.skleroshop.StableArrayAdapter$1.onClick(StableArrayAdapter.java:91)
What i am doing wrong?
TNX in advance!
Upvotes: 4
Views: 17658
Reputation: 2318
Remove item from list
list.remove(position);
Then call below method using adapter object.
stableAdapter.notifyData(list);
Write a method in your 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
Upvotes: 2
Reputation: 389
Correct code for 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) {
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;
}
}
Upvotes: 4