Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
232 views
in Technique[技术] by (71.8m points)

java - notifyDataSetChanged() does not affect my adapterView

I am new to android and I have to create an android app for my final project.

I am making a swipe card app and I am using 'com.lorentzos.swipecards:library:1.0.9' library for the swiping engine.

It uses a custom adapter that extends a base adapter to inflate a layout file and populate certain ImageViews and TextViews with iformation coming from an ArrayList.

This is the code of the adapter

package com.botevplovdiv.foodmatch2;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Stelio on 13.5.2017 г..
 */



public class MyAppAdapter extends BaseAdapter {

    private static class ViewHolder {
        public FrameLayout background;
        public TextView DataText;
        public ImageView cardImage;
        public TextView shortDescriptionTextView;
        public TextView title;
    }
    ;
    public List<FoodDish> dishesList = new ArrayList<>();
    public Context mContext;

    public MyAppAdapter(Context context, List<FoodDish> foodDishes) {
        this.dishesList = foodDishes;
        this.mContext = context;
    }



    @Override
    public int getCount() {
        return dishesList.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public List<FoodDish> getDishesList(){
        return this.dishesList;
    }

    public void updateReceiptsList(List<FoodDish> newlist) {
        dishesList.clear();
        dishesList.addAll(newlist);
        this.notifyDataSetChanged();
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        FoodDish current = dishesList.get(position);

        ViewHolder viewHolder;


        if (convertView == null) {

            LayoutInflater inflater = LayoutInflater.from(mContext);
            convertView = inflater.inflate(R.layout.cards_layout, parent,false);
            // configure view holder
            viewHolder = new ViewHolder();
            viewHolder.DataText = (TextView) convertView.findViewById(R.id.priceText);
            viewHolder.background = (FrameLayout) convertView.findViewById(R.id.background);
            viewHolder.cardImage = (ImageView) convertView.findViewById(R.id.cardImage);
            viewHolder.shortDescriptionTextView = (TextView) convertView.findViewById(R.id.shortDescription);
            viewHolder.title = (TextView) convertView.findViewById(R.id.titleText);

            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.DataText.setText(current.getPrice());
        viewHolder.title.setText(current.getTitle());
        viewHolder.shortDescriptionTextView.setText(current.getDescription());
        Glide.with(mContext).load(current.getImagePath()).into(viewHolder.cardImage);



        return convertView;
    }
}

Everything is going fine till one point.

I have a Settings activity, which inflates a preferences.xml which has a bunch of switches. I could do it using MultiSelectListPreferences, but I want to be with switches for aesthetic reasons.

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

<PreferenceCategory
    android:selectable="true"
    android:title="@string/food_categories_name">

    <SwitchPreference
        android:defaultValue="true"
        android:key="fishSwitch"
        android:title="Fish" />
    <SwitchPreference
        android:defaultValue="true"
        android:key="meatSwitch"
        android:title="Meat" />
    <SwitchPreference
        android:defaultValue="true"
        android:key="vegSwitch"
        android:title="Vegeterian" />
    <SwitchPreference
        android:defaultValue="true"
        android:key="pizzaSwitch"
        android:title="Pizza" />
    <SwitchPreference
        android:defaultValue="true"
        android:key="pastaSwitch"
        android:title="Pasta" />
</PreferenceCategory>

<ListPreference
    android:defaultValue="@array/area_list"
    android:entries="@array/area_list"
    android:entryValues="@array/area_list"
    android:key="list_preference_1"
    android:title="@string/areas" />
<PreferenceCategory android:title="@string/venues_categories">

    <SwitchPreference
        android:defaultValue="true"
        android:key="restaurantSwitch"
        android:title="Restaurants" />
    <SwitchPreference
        android:defaultValue="true"
        android:key="takeAwaySwitch"
        android:title="Take Away" />
</PreferenceCategory>

In my MainActivity I have prefereceChangedListner which register changes and refine the searcCriteria and refineList Lists throught the respectful methods.

package com.botevplovdiv.foodmatch2;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.preference.SwitchPreference;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.lorentzos.flingswipe.SwipeFlingAdapterView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class MainActivity extends AppCompatActivity {

    // keys for reading data from SharedPreferences
    public static final String FISH = "fishSwitch";
    public static final String MEAT = "meatSwitch";
    public static final String VEGETARIAN = "vegSwitch";
    public static final String PIZZA = "pizzaSwitch";
    public static final String PASTA = "pastaSwitch";
    public static final String RESTAURANTS = "restaurantSwitch";
    public static final String TAKEAWAY = "takeAwaySwitch";


    private MyAppAdapter myAppAdapter;
    private List<FoodDish> dishList;
    private List<FoodDish> refineList;
    private List<FoodDish> dumpList;
    private List<Venue> venues;
    private boolean[] booleans;
    private String[] booleansTags;
    private List<String> searchCriteria;
    private SwipeFlingAdapterView flingContainer;
    public static Map<String, Bitmap> bitmaps = new HashMap<>();
    boolean fishSwitch;
    boolean meatSwitch;
    boolean vegetarianSwitch;
    boolean pizzaSwitch;
    boolean pastaSwitch;
    boolean restaurantSwitch;
    boolean takeAwaySwitch;
    boolean preferenceChanged = true;
    String TAG = "FoodMatch";



    @Override
     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        flingContainer = (SwipeFlingAdapterView) findViewById(R.id.frame);


        // set default values in the app's SharedPreferences
        PreferenceManager.setDefaultValues(this, R.xml.preferences, false);

        // register listener for SharedPreferences changes
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        sharedPreferences.registerOnSharedPreferenceChangeListener(
                preferencesChangeListener);

        fishSwitch = sharedPreferences.getBoolean(FISH,false);
        meatSwitch =  sharedPreferences.getBoolean(MEAT,false);
        vegetarianSwitch =  sharedPreferences.getBoolean(VEGETARIAN,false);
        pizzaSwitch =  sharedPreferences.getBoolean(PIZZA,false);
        pastaSwitch =  sharedPreferences.getBoolean(PASTA,false);
        restaurantSwitch =  sharedPreferences.getBoolean(RESTAURANTS,false);
        takeAwaySwitch =  sharedPreferences.getBoolean(TAKEAWAY,false);

        booleansTags = new String[] {"FISH","MEAT","VEGETARIAN","PIZZA","PASTA","RESTAURANT","TAKE AWAY"};

        dishList = new ArrayList<>();
        searchCriteria = new ArrayList<>();
        refineList = new ArrayList<>();
        dumpList = new ArrayList<>();


        // load initial data
        loadDishes();

        createSearchCriteria(searchCriteria);
        refineDishesList();

        //lock the device to portrait mode
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        myAppAdapter = new MyAppAdapter(this, refineList);
        flingContainer.setAdapter(myAppAdapter);
        flingContainer.setFlingListener(new SwipeFlingAdapterView.onFlingListener() {
            @Override
            public void removeFirstObjectInAdapter()
            {

            }

            @Override
            public void onLeftCardExit(Object dataObject)
            {
                FoodDish current = refineList.get(0);
                refineList.remove(0);
                myAppAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onRightCardExit(Object dataObject)
            {
                FoodDish current = refineList.get(0);
                Intent intent = new Intent(MainActivity.this,LikedProduct.class).putExtra("current",current);
                startActivity(intent);
                refineList.remove(0);
                myAppAdapter.notifyDataSetChanged();

            }

            @Override
            public void onAdapterAboutToEmpty(int itemsInAdapter)
            {

            }

            @Override
            public void onScroll(float scrollProgressPercent) {
                View view = flingContainer.getSelectedView();
                view.findViewById(R.id.background).setAlpha(0);
                view.findViewById(R.id.item_swipe_left_indicator).setAlpha(scrollProgressPercent < 0 ? -scrollProgressPercent : 0);
                view.findViewById(R.id.item_swipe_right_indicator).setAlpha(scrollProgressPercent > 0 ? scrollProgressPercent : 0);
            }
        });


        // Listener for touching events
        flingContainer.setOnItemClickListener(new SwipeFlingAdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClicked(int itemPosition, Object dataObject) {

                View view = flingContainer.getSelectedView();
                view.findViewById(R.id.background).setAlpha(0);
                myAppAdapter.notifyDataSetChanged();
            }
        });

        //floating buttons for remote flipping the cards

        FloatingActionButton yesFAB =
                (FloatingActionButton) findViewById(R.id.acceptButton);
        yesFAB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onC

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

just quick review on the code, is this part of code correct?

@Override
public Object getItem(int position) {
    return position;
}

why don't change it to

@Override
public Object getItem(int position) {
    return dishesList.get(position);
}

will check it out sometime later


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...