Android – ArrayAdapter personalizado

¿Podría alguien ayudarme a resolver mi problema. Todavía estoy intentando trabajar con DraggingListView y ArrayAdapter . Ahora quiero realizar el elemento de la cancelación del listview por tecleo, pero cuando hago:

 StableArrayAdapter.this.notifyDataSetChanged(); 

me sale la nullPointer Exception

Aquí está mi adaptador:

 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; } } 

Aquí está el error:

08-26 08: 50: 58.902 2167-2167 / com.shvedchenko.skleroshop E / AndroidRuntime: EXCEPCIÓN FATAL: principal java.lang.NullPointerException en com.shvedchenko.skleroshop.StableArrayAdapter.getItemId (StableArrayAdapter.java:104) en android. widget.AdapterView.rememberSyncState (AdapterView.java:1195) en android.widget.AdapterView $ AdapterDataSetObserver.onChanged (AdapterView.java:810) en android.widget.AbsListView $ AdapterDataSetObserver.onChanged (AbsListView.java:5958) en android.database .DataSetObservable.notifyChanged (DataSetObservable.java:37) en android.widget.BaseAdapter.notifyDataSetChanged (BaseAdapter.java:50) en android.widget.ArrayAdapter.notifyDataSetChanged (ArrayAdapter.java:286) en com.shvedchenko.skleroshop.StableArrayAdapter $ 1 .onClick (StableArrayAdapter.java:85) en android.view.View.performClick (View.java:4204) en android.view.View $ PerformClick.run (View.java:17355) en android.os.Handler.handleCallback Handler.java:725) en android.os.Handler.dispatchMessage (Handler.java:92) en android.es .Looper.loop (Looper.java:137) en android.app.ActivityThread.main (ActivityThread.java:5041) en java.lang.reflect.Method.invokeNative (Método nativo) en java.lang.reflect.Method.invoke (Method.java:511) en com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:793) en com.android.internal.os.ZygoteInit.main (ZygoteInit.java:560) en dalvik. system.NativeStart.main (Método nativo)

/ ACTUALIZADO /

Mi adaptador ahora:

 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: EXCEPCIÓN FATAL: principal java.lang.NullPointerException en com.shvedchenko.skleroshop.StableArrayAdapter.getItemId (StableArrayAdapter.java:122) en android. widget.AdapterView.rememberSyncState (AdapterView.java:1195) en android.widget.AdapterView $ AdapterDataSetObserver.onChanged (AdapterView.java:810) en android.widget.AbsListView $ AdapterDataSetObserver.onChanged (AbsListView.java:5958) en android.database .DataSetObservable.notifyChanged (DataSetObservable.java:37) en android.widget.BaseAdapter.notifyDataSetChanged (BaseAdapter.java:50) en android.widget.ArrayAdapter.notifyDataSetChanged (ArrayAdapter.java:286) en com.shvedchenko.skleroshop.StableArrayAdapter. notifyData (StableArrayAdapter.java:112) en com.shvedchenko.skleroshop.StableArrayAdapter $ 1.onClick (StableArrayAdapter.java:91)

que estoy haciendo mal?

TNX por adelantado!

Código correcto para el adaptador:

 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; } } 

Eliminar elemento de la lista

  list.remove(position); 

A continuación, llame al método siguiente utilizando el objeto adaptador.

  stableAdapter.notifyData(list); 

Escribe un método en tu clase Adapter.

  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(); } 

Llame a este método utilizando un objeto adpater

  • CursorAdapter vs ArrayAdapter para un ListView
  • Cómo agregar un objeto al principio de la lista en ArrayAdapter?
  • Android - Adaptador ListView personalizado - Selección múltiple eliminar - Indexoutofbounds - ¿por qué?
  • No puedo acceder a R.layout.mylayoutname en mi ArrayAdapter personalizado
  • Custom ArrayAdapter en un ListFragment
  • ListView con disposición de filas personalizada - Android
  • Android ListView problemas en Android 2.X
  • El método getView () de ArrayAdapter no se recibe
  • ¿Qué es "android.R.layout.simple_list_item_1"?
  • Vista de lista de Android dentro de un fragmento
  • Spinner no puede cargar una matriz de números enteros?
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.