1. Download our Official Android App: Forums for Android!

Support Only once per day validation for user from a list view in Android

Discussion in 'Android Help' started by Sudhakar100, Sep 1, 2016.

  1. Sudhakar100

    Sudhakar100 Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    1
    Joined:
    Aug 12, 2016

    Aug 12, 2016
    1
    0
    5
    Male
    I need to validate the user to pick only one item per day using timer or alarm manager in Android. Is there a better way to design it, so that it is easy check if the user does not get more than one item per day. I have a list of ten items displayed in a list view. The user get an item from the list. He should get only one item. If he tries again, he should be alerted that one item only once per day.

    public class MainActivity extends Activity {

    @override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Effects.getInstance().init(this);
    ListView lv = (ListView) findViewById(R.id.listView);

    List<ItemDetail> itemList = createItems(10);

    // Load animation
    final Animation anim = AnimationUtils.loadAnimation(this, R.anim.fade_anim);

    final ArrayAdapter<ItemDetail> aAdpt = new ArrayAdapter<ItemDetail>(this, android.R.layout.simple_list_item_1,
    itemList);
    lv.setAdapter(aAdpt);

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    @override
    public void onItemClick(AdapterView<?> adapterView, final View view, final int position, long id) {
    MediaPlayer mPlayer = MediaPlayer.create(MainActivity.this, R.raw.gum_bubble_pop);
    mPlayer.start();
    anim.setAnimationListener(new Animation.AnimationListener() {

    @override
    public void onAnimationStart(Animation animation) {
    }

    @override
    public void onAnimationRepeat(Animation animation) {

    }

    @override
    public void onAnimationEnd(Animation animation) {

    long now = System.currentTimeMillis();
    long lastCheckedMillis = 0;
    long diffMillis = now - lastCheckedMillis;
    if( diffMillis >= (3600000 * 24) ) {
    // store now (i.e. in shared prefs)

    // do the check
    } else {
    // too early
    }



    if(aAdpt.getCount()==9) {
    Toast.makeText(MainActivity.this, "You can get only one gift per day "+now, Toast.LENGTH_LONG).show();
    }
    else {
    ItemDetail item = aAdpt.getItem(position);
    aAdpt.remove(item);
    }
    }
    });
    view.startAnimation(anim);

    }
    });

    }

    private List<ItemDetail> createItems(int size) {
    List<ItemDetail> result = new ArrayList<ItemDetail>();
    for (int i = 0; i < size; i++) {
    ItemDetail item = new ItemDetail(i, "Gift " + (i + 1));
    result.add(item);
    }

    return result;
    }

    }
     

    Advertisement

Share This Page

Loading...