Skip to content Skip to sidebar Skip to footer

Android - Custom Arrayadapter

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 : St

Solution 1:

Correct code for adapter:

publicclassStableArrayAdapterextendsArrayAdapter<Product> {

    finalintINVALID_ID= -1;
    LayoutInflater lInflater;
    Context ctx;
    publicstaticfinalStringPREFS_NAME="com.shvedchenko.skleroshop";
    publicstaticfinalStringPREFS_THEME="theme";

    HashMap<Product, Integer> mIdMap = newHashMap<Product, Integer>();

    publicStableArrayAdapter(Context context, int textViewResourceId, List<Product> prod) {
        super(context, textViewResourceId, /*objects*/prod);
        lInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        ctx = context;
        for (inti=0; i < prod.size(); i++) {
            mIdMap.put(prod.get(i), i);
        }
    }


    // пункт списка@Overridepublic View getView(finalint position, View convertView, final ViewGroup parent) {
        // используем созданные, но не используемые viewViewview= convertView;
        if (view == null) {
            view = lInflater.inflate(R.layout.item, parent, false);
        }

        Productp= getItem(position);

        SharedPreferencespref= ctx.getSharedPreferences(PREFS_NAME, ctx.MODE_PRIVATE);
        inttheme= pref.getInt(PREFS_THEME, 0);             // getting Integerif(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());

        ImageViewiv= (ImageView)view.findViewById(R.id.ivImage);
        iv.setOnClickListener(newView.OnClickListener() {
            @OverridepublicvoidonClick(View view) {
                StableArrayAdapter.this.remove(getItem(position));
                StableArrayAdapter.this.notifyDataSetChanged();
            }
        });

        return view;
    }


    @OverridepubliclonggetItemId(int position) {
        if (position < 0 || position >= mIdMap.size()) {
            return INVALID_ID;
        }
        Productitem= (Product) getItem(position);
        return mIdMap.get(item);
    }

    @OverridepublicbooleanhasStableIds() {
        returntrue;
    }

}

Solution 2:

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

Post a Comment for "Android - Custom Arrayadapter"