Android - Circular array - android

I have a array of jokes
String jokes[]={"x","y","z","j"};
i am using two methods next and previous to go front and back.
public void nextJoke(View view) {
if (jokeNumber < jokes.length - 1) {
jokeNumber++;
tv1.setText(jokes[jokeNumber]);
}
}
public void prevJoke(View view) {
if (jokeNumber > 0) {
jokeNumber--;
tv1.setText(jokes[jokeNumber]);
}
I want to give this list a endless scroll functionality .
That means if the user next or previous - and list reaches the last three elements , it should again start from first element.
How can i achieve this functionality .
This is more of a programming question .
Any Help will be highly appreciated.

Using the remainder operator, you'll get the required behavior.
Replace:
jokeNumber++;
with:
jokeNumber = ++jokeNumber % jokes.length;
and do the same for jokeNumber--;

On next:
next(){
jokeNumber++;
if(jokeNumber>=jokes.length) { jokeNumber =0;}
}
on previous:
previous(){
jokeNumber--;
if(jokeNumber<=0) { jokeNumber =jokes.length;}
}

public void nextJoke(View view) {
if (jokeNumber < jokes.length ) {
jokeNumber = ++jokeNumber % jokes.length;
tv1.setText(jokes[jokeNumber]); } }
public void prevJoke(View view) {
if (jokeNumber > 0) {
jokeNumber = --jokeNumber % jokes.length;
tv1.setText(jokes[jokeNumber]);
}

Related

Unity GameObject "flash" after SetActive on Android Project

I have a project that use Android to display Unity Player. So I export Untiy project as Android module which implemented by Android Application.
I create buttons in Android Activity which contains UnityPlayer, And when I click button, it send a message to Unity Player to invoke C# function, just like this:
findViewById(R.id.btnChange).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mUnityPlayer.UnitySendMessage("ScriptHolder", "ChangeSkin", "");
}
});
And the function named "ChangeSkin" is just to change some GameObjects' active. Just like this:
void ChangeSkin()
{
int prefab;
if (_currentPrefab == PREFAB_DEFAULT)
{
prefab = PREFAB_PRINCESS;
}
else
{
prefab = PREFAB_DEFAULT;
}
ShowSkin(prefab);
}
private void ShowSkin(int prefab)
{
_currentPrefab = prefab;
foreach (var item in _defaultDressList)
{
item.SetActive(prefab == PREFAB_DEFAULT);
}
foreach (var item in _princessDressList)
{
item.SetActive(prefab == PREFAB_PRINCESS);
}
}
And something weird happening: when I click button to change the person's cloth in Unity, the GameObjects which called SetActive(true) show at the position above the right position for a frame and become normal, it looks like they flash. Here is the gif of the project demo:
It looks like the position offset is equal to the height of status bar. If I create a button on Unity Scene and call "ChangeSkin" function, everything will be OK.
I tried all I can to fix this but not succeed. So I hope you will help me, thx.
You should check your Unity layout. If you have a layout group that has content size fitter component, and the child objects also have it (content size fitter), this could cause these glitches.
I fixed this problem by using a flag (or trigger). Just like this:
// set value true to trigger ChangeSkin() in Update(),
// instead of invoke ChangeSkin() directly
private bool _changingSkinTrigger = false;
void ChangeSkin()
{
if (_currentPrefab == PREFAB_DEFAULT)
{
_currentPrefab = PREFAB_PRINCESS;
}
else
{
_currentPrefab = PREFAB_DEFAULT;
}
_changingSkinTrigger = true;
}
void Update()
{
if (_changingSkinTrigger)
{
_changingSkinTrigger = false;
ShowSkin();
}
}
private void ShowSkin()
{
foreach (var item in _defaultDressList)
{
item.SetActive(_currentPrefab == PREFAB_DEFAULT);
}
foreach (var item in _princessDressList)
{
item.SetActive(_currentPrefab == PREFAB_PRINCESS);
}
}
Thank #AndriyMarshalek for enlightening me.

What triggers and what happens on onPageDataChanged?

In a project I am working on, there is a class that uses Modelcallbacks. One of its callbacks is onPageDataChanged.
I have searched on the net but cannot find what triggers and what happens on the callback function.
Can someone please explain it to me? Thanks in advance!
#Override
public void onPageDataChanged(Page changedPage) {
ArrayList<ReviewItem> reviewItems = new ArrayList<ReviewItem>();
for (Page page : mWizardModel.getCurrentPageSequence()) {
page.getReviewItems(reviewItems);
}
Collections.sort(reviewItems, new Comparator<ReviewItem>() {
#Override
public int compare(ReviewItem a, ReviewItem b) {
return a.getWeight() > b.getWeight() ? +1 : a.getWeight() < b.getWeight() ? -1 : 0;
}
});
mCurrentReviewItems = reviewItems;
if (mReviewAdapter != null) {
mReviewAdapter.notifyDataSetInvalidated();
}
}

Android sort Listview, according to user id

I have a listview which contain 3 different values.
The values:
Top problem (getField_top_problem), user id (getUid) difficulty(getField_difficulty)
I would like to sort the list like this.
First part:
Check which user is logged in. Get all the Top problems for that user.
The Top problems from the logged in user, needs to be sorted according to most difficult first.
Second part / Rest of list.
Need to be sorted according to the most difficult first. Does not matter if there is a Top problem from another user, which is not logged in. (If not sure, check image - red box)
If its still unclear, i have added an image. It should look like this.
My progress so far.
I was able to sort it, Top problem first for all users, not just the logged in user. Need to sort it, according to logged in user's Top problem. The id for logged in user is saved as a string.
//Example logged in user id (uid)
String uid = "2";
Any help would be appreciated.
protected void onPostExecute(final List<OcdModel> result) {
super.onPostExecute(result);
dialog.dismiss();
//uid - user id that is logged in.
//Sort - Top problem of user logged in first.
//sort the rest according to difficulty
//check which user is logged in.
//Boolean user1 = a1.getUid().equals(uid);
Collections.sort(result, new Comparator<OcdModel>() {
#Override
public int compare(OcdModel a1, OcdModel a2) {
//Top problem first. Arrange row with most difficult first in top problems list.
Integer tp1 = Integer.parseInt(a1.getField_top_problem().toString());
Integer tp2 = Integer.parseInt(a2.getField_top_problem().toString());
int tpComp = tp1.compareTo(tp2);
//return b1.compareTo(b2);
if (tpComp != 0) {
return tpComp;
} else {
int b1 = Integer.parseInt(a1.getField_difficulty().toString());
int b2 = Integer.parseInt(a2.getField_difficulty().toString());
return (int) (b1-b2);
}
}
});
Collections.reverse(result);
//TODO Need to set data
OcdListAdapter adapter = new OcdListAdapter(getApplicationContext(), R.layout.row_ocd_list, result);
lvToolbox.setAdapter(adapter);
}
Thanks in advance
You need to create multiple camparator like this
public class ModelChainedComparator implements Comparator<OcdModel> {
private List<Comparator<OcdModel>> listComparators;
#SafeVarargs
public ModelChainedComparator(Comparator<OcdModel>... comparators) {
this.listComparators = Arrays.asList(comparators);
}
#Override
public int compare(OcdModel a1, OcdModel a2) {
for (Comparator<OcdModel> comparator : listComparators) {
int result = comparator.compare(a1, a2);
if (result != 0) {
return result;
}
}
return 0;
}
}
public class ModelUserLoginComparator implements Comparator<OcdModel> {
#Override
public int compare(OcdModel a1, OcdModel a2) {
return Boolean.valueOf(a2.isUserLogedIn).compareTo(Boolean.valueOf(a1.isUserLogedIn));
}
}
public class ModelDeficultyComparator implements Comparator<OcdModel> {
#Override
public int compare(OcdModel a1, OcdModel a2) {
int firstObject = Integer.parseInt(a1.getField_difficulty().toString());
int secondObject = Integer.parseInt(a2.getField_difficulty().toString());
return firstObject < secondObject ? 1 : (firstObject == secondObject ? 0 : -1);
}
}
Than you have to use this like
ArrayList<OcdModel> list = getList();
Collections.sort(list, new ModelChainedComparator(
new ModelUserLoginComparator(),
new ModelDeficultyComparator())
);
Let me know if not work

How to Send Drawable as a parameter to a function

I am trying to set Imageview using a function
that if we send Image drawable to a function that function set that drawable to the respective Imageview.
This is function code in which it gets "id" and after that respective id function will call
public void showpeg(int id) {
switch (id) {
case 1:
showSeagreen(R.drawable.seagreen);
break;
}
}
This is my ShowSeagreen() function
public void showSeagreen(Drawable draw) {
if (iRow == 1) {
if (iPlace == 1) {
seagreen = 1;
one_first.setBackgroundResource(R.drawable.draw);
iPlace++;
} else if (iPlace == 2) {
seagreen = 2;
one_two.setBackgroundResource(R.drawable.draw);
iPlace++;
}
}
I know i am doing it wrong but i can't find a way to do this Please any help
Thanks in advance
instead of
showSeagreen(R.drawable.seagreen);
use:
showSeagreen(getResources().getDrawable(R.drawable.seagreen));
Any Resources is a int.
Do like this to "showSeagreen":
public void showSeagreen(int draw) {
if (iRow == 1) {
if (iPlace == 1) {
seagreen = 1;
one_first.setBackgroundResource(draw);
iPlace++;
} else if (iPlace == 2) {
seagreen = 2;
one_two.setBackgroundResource(draw);
iPlace++;
}
}
If what i understand from your question is true then i think your aim is to add drawable image as a parameter if thats what you want
then you should add the
#DrawableRes int draw
like
public void showSeagreen(#DrawableRes int draw) {
}
as your parameter then you can add drawable files in here

Roman Nurik's Wizard pager - how to access collected data?

I am trying to make a wizard using Roman Nurik's library (https://plus.google.com/113735310430199015092/posts/6cVymZvn3f4).
I am having trouble accessing the collected data from the Review Fragment.
I made mCurrentReviewItems public in ReviewFragment and then I tried it like this
mNextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (mPager.getCurrentItem() == mCurrentPageSequence.size()) {
ReviewFragment reviewFragment = (ReviewFragment) mPagerAdapter.getItem(mPager.getCurrentItem());
for (ReviewItem item : reviewFragment.mCurrentReviewItems)
Log.d(MainActivity.TAG, "Item: " + item.getDisplayValue());
}
} else {
if (mEditingAfterReview) {
mPager.setCurrentItem(mPagerAdapter.getCount() - 1);
} else {
mPager.setCurrentItem(mPager.getCurrentItem() + 1);
}
}
}
});
However its always null.
Inside if (mPager.getCurrentItem() == mCurrentPageSequence.size()) { }
For single page variable:
String data = mWizardModel.findByKey("Sandwich:Bread").getData().getString(Page.SIMPLE_DATA_KEY);
For customized page:
String data =
mWizardModel.findByKey(THE_KEY).getData().getString(CustomerInfoPage.YOUR_DATA_KEY);
If you want to assign the data back to the wizard, put this at the end of onCreate in FragmentActivity:
Bundle data = new Bundle();
if (!TextUtils.isEmpty(DATA_STRING)) {
data.putString(Page.SIMPLE_DATA_KEY, DATA_STRING);
mWizardModel.findByKey("Sandwich:Bread"").resetData(data);
}
The key "Sandwich:Bread" is from the example, change whatever suit you. Never try the multi one, I think it is more or less the same.
Sorry for big delay, but I think that someone will found this info useful. I found a way to get all ReviewItems since you can have a lot of branches and you won't be able to use the first answer.
I'm pretty sure, that your mPagerAdapter::getItem code looked like in example (so it just returned new fragment, instead of returning current pager fragment). You have to use instantiateItem to get reference on your ReviewFragment.
Object o = mPager.getAdapter().instantiateItem(mPager, mPager.getCurrentItem());
if(o instanceof ReviewFragment) {
List<ReviewItem> items = ((ReviewFragment) o).getCurrentReviewItems();
if(items != null) {
Log.v(TAG, "Items are: " + items.toString());
}
}
This is my code #Anton_Shkurenko
mNextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (mPager.getCurrentItem() == mCurrentPageSequence.size()) {
Object o = mPager.getAdapter().instantiateItem(mPager, mPager.getCurrentItem());
if(o instanceof ReviewFragment) {
List<ReviewItem> items = ((ReviewFragment) o).getCurrentReviewItems();
if(items != null) {
Log.v(TAG, "Items are: " + items.toString());
}
}
}
}
});
The best solution is to include this library in your project as module, and implement your own method for getting review items in ReviewFragment.
public List<ReviewItem> getReviewItems() {
return mCurrentReviewItems;
}
I am not sure why developer did not add that. It's the most important thing in project. Choose items and DO something with them.
Anyone still looking for a solution for this issue you can use following code
ArrayList<ReviewItem> reviewItems = new ArrayList<ReviewItem>();
for (Page page : mWizardModel.getCurrentPageSequence()) {
page.getReviewItems(reviewItems);
}

Categories

Resources