Android Viewpager, button click or any otherthing about Viewpager
Question
well you can see at image. buttons not working properly. And how is this problem occur I dont understand.
I am making a quiz app. if user click wrong answer, I want make buttons background color red, else click true answer, button background color will green.
at first page which is user selected from a list, buttons working normal. after swipe in pages buttons working wrong. always it changes previous page button color.
I think the reason of this is, viewpager dont know which button's color should change after my controling method.
this is my controlling method in viewpager activity:
public void click(View v) {
int c = 0;
switch (v.getId()) {
case R.id.cevap1a:
c = 1;
controlanswer(c);
break;
case R.id.cevap2b:
c = 2;
controlanswer(c);
break;
case R.id.cevap3c:
c = 3;
controlanswer(c);
break;
case R.id.cevap4d:
c = 4;
controlanswer(c);
break;
case R.id.cevap5e:
c = 5;
controlanswer(c);
break;
default:
break;
}
}
public void controlanswer(int a) {
int d = mViewPager.getCurrentItem();
int answer = mquestiondatabase.get(d).gettrueanswer();
if (a == answer) {
truecolor(a);
} else {
wrongcolor(a);
truecolor(answer);
}
}
public void wrongcolor(int wrong) {
cevapA = (TextView) mViewPager.findViewById(R.id.cevap1a);
cevapB = (TextView) mViewPager.findViewById(R.id.cevap2b);
cevapC = (TextView) mViewPager.findViewById(R.id.cevap3c);
cevapD = (TextView) mViewPager.findViewById(R.id.cevap4d);
cevapE = (TextView) mViewPager.findViewById(R.id.cevap5e);
switch (wrong) {
case 1:
cevapA.setBackgroundResource(R.drawable.red_buton_clicked);
break;
case 2:
cevapB.setBackgroundResource(R.drawable.red_buton_clicked);
break;
case 3:
cevapC.setBackgroundResource(R.drawable.red_buton_clicked);
break;
case 4:
cevapD.setBackgroundResource(R.drawable.red_buton_clicked);
break;
case 5:
cevapE.setBackgroundResource(R.drawable.red_buton_clicked);
break;
default:
break;
}
}
public void truecolor(int answer) {
cevapA = (TextView) findViewById(R.id.cevap1a);
cevapB = (TextView) findViewById(R.id.cevap2b);
cevapC = (TextView) findViewById(R.id.cevap3c);
cevapD = (TextView) findViewById(R.id.cevap4d);
cevapE = (TextView) findViewById(R.id.cevap5e);
AnimationDrawable rocketAnimation;
switch (answer) {
case 1:
cevapA.setBackgroundResource(R.drawable.yesilflash);
rocketAnimation = (AnimationDrawable) cevapA.getBackground();
rocketAnimation.start();
break;
case 2:
cevapB.setBackgroundResource(R.drawable.yesilflash);
rocketAnimation = (AnimationDrawable) cevapB.getBackground();
rocketAnimation.start();
break;
case 3:
cevapC.setBackgroundResource(R.drawable.yesilflash);
rocketAnimation = (AnimationDrawable) cevapC.getBackground();
rocketAnimation.start();
break;
case 4:
cevapD.setBackgroundResource(R.drawable.yesilflash);
rocketAnimation = (AnimationDrawable) cevapD.getBackground();
rocketAnimation.start();
break;
case 5:
cevapE.setBackgroundResource(R.drawable.yesilflash);
rocketAnimation = (AnimationDrawable) cevapE.getBackground();
rocketAnimation.start();
break;
default:
break;
}
}
}
Well I combine my old question in a one question
Solution
Well I have solved.
The main problem is, I had written my code in fragmentactivity. but After that I have suspiced that and put all my codes in fragment.
I think this was a good example of using viewpager.
public class newFragment extends Fragment {
public static final String ExtraID = "com.num";
public static ViewPager mViewPager;
private static String tag = "sqllist";
int sayfano;
private int c = 0;
int mNum2;
soru msoru;
TextView baslik;
TextView aciklama;
TextView soru;
TextView cevapA;
TextView cevapB;
TextView cevapC;
TextView cevapD;
TextView cevapE;
CheckBox mSolvedCheckBox;
public static newFragment newInstance(int num) {
Bundle args = new Bundle();
args.putInt("num", num);
newFragment fragment = new newFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
mNum2 = (int) getArguments().getInt("num");
TestAdapter mDbHelper = new TestAdapter(getActivity());
mDbHelper.createDatabase();
mDbHelper.open();
List<soru> mSorus = mDbHelper.getTestData();
msoru = mSorus.get(mNum2);
int v = newPagerActivity.sayfano();
Log.d(tag, "crime fragment on create mnum= " + v + "v = " + v);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
if (NavUtils.getParentActivityName(getActivity()) != null) {
NavUtils.navigateUpFromSameTask(getActivity());
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_crime, parent, false);
aciklama = (TextView) v.findViewById(R.id.aciklama);
soru = (TextView) v.findViewById(R.id.soru);
cevapA = (TextView) v.findViewById(R.id.cevap1a);
cevapB = (TextView) v.findViewById(R.id.cevap2b);
cevapC = (TextView) v.findViewById(R.id.cevap3c);
cevapD = (TextView) v.findViewById(R.id.cevap4d);
cevapE = (TextView) v.findViewById(R.id.cevap5e);
baslik = (TextView) v.findViewById(R.id.baslik);
try {
baslik.setText(msoru.getmId() + ". Soru");
aciklama.setText(msoru.getmTitle().toString());
soru.setText(msoru.getmSoru().toString());
cevapA.setText("A) " + msoru.getmCevap1().toString());
cevapB.setText("B) " + msoru.getmCevap2().toString());
cevapC.setText("C) " + msoru.getmCevap3().toString());
cevapD.setText("D) " + msoru.getmCevap4().toString());
cevapE.setText("E) " + msoru.getmCevap5().toString());
} catch (Exception e) {
Log.d(tag, "bir kısım kendini bilmez soru kısımcıkları kaybolmuş");
e.printStackTrace();
}
cevapA.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
c = 0;
cevapanahtari(c);
}
});
cevapB.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
c = 1;
cevapanahtari(c);
}
});
cevapC.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
c = 2;
cevapanahtari(c);
}
});
cevapD.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
c = 3;
cevapanahtari(c);
}
});
cevapE.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
c = 4;
cevapanahtari(c);
}
});
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
if (NavUtils.getParentActivityName(getActivity()) != null)
getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
}
return v;
}
public void cevapanahtari(int a) {
int d = newPagerActivity.getViewPager();
int cevap = msoru.getCevapanahtari();
if (a == cevap) {
Log.d(tag, "cevapanahtarı if");
renkdogru(a);
} else {
Log.d(tag, "cevapanahtarı else");
renkyanlis(a);
renkdogru(cevap);
}
}
public void renkyanlis(int yanlis) {
Log.d(tag, "renkyanlis" + yanlis);
switch (yanlis) {
case 1:
cevapA.setBackgroundResource(R.drawable.red_buton_clicked);
break;
case 2:
cevapB.setBackgroundResource(R.drawable.red_buton_clicked);
break;
case 3:
cevapC.setBackgroundResource(R.drawable.red_buton_clicked);
break;
case 4:
cevapD.setBackgroundResource(R.drawable.red_buton_clicked);
break;
case 5:
cevapE.setBackgroundResource(R.drawable.red_buton_clicked);
break;
default:
break;
}
}
public void renkdogru(int tiklanancevap) {
AnimationDrawable rocketAnimation;
switch (tiklanancevap) {
case 1:
Log.d(tag, "rendogru case 1");
cevapA.setBackgroundResource(R.drawable.yesilflash);
rocketAnimation = (AnimationDrawable) cevapA.getBackground();
rocketAnimation.start();
break;
case 2:
cevapB.setBackgroundResource(R.drawable.yesilflash);
rocketAnimation = (AnimationDrawable) cevapB.getBackground();
rocketAnimation.start();
break;
case 3:
cevapC.setBackgroundResource(R.drawable.yesilflash);
rocketAnimation = (AnimationDrawable) cevapC.getBackground();
rocketAnimation.start();
break;
case 4:
cevapD.setBackgroundResource(R.drawable.yesilflash);
rocketAnimation = (AnimationDrawable) cevapD.getBackground();
rocketAnimation.start();
break;
case 5:
cevapE.setBackgroundResource(R.drawable.yesilflash);
rocketAnimation = (AnimationDrawable) cevapE.getBackground();
rocketAnimation.start();
break;
default:
break;
}
}
}
On the otherhand this my new fragment activity
public class newPagerActivity extends FragmentActivity {
public static ViewPager mViewPager;
private int sorusirasi;
private List<soru> mSorus;
final String tag = "anahtar";
soru mSoruchec;
public static int c=1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mViewPager = new ViewPager(this);
mViewPager.setId(R.id.viewPager);
setContentView(mViewPager);
TestAdapter mDbHelper = new TestAdapter(this);
mDbHelper.createDatabase();
mDbHelper.open();
mSorus = mDbHelper.getTestData();
Log.d(tag, "fragment activit yaratıldı");
FragmentManager fm = getSupportFragmentManager();
mViewPager.setAdapter(new FragmentStatePagerAdapter(fm) {
@Override
public int getCount() {
return mSorus.size();
}
@Override
public Fragment getItem(int sorusirasi) {
return newFragment.newInstance(sorusirasi);
}
});
sorusirasi = getIntent().getIntExtra("num", 0);
for (int i = 1; i < mSorus.size(); i++) {
if (mSorus.get(i).getmId() == sorusirasi) {
mViewPager.setCurrentItem(i);
break;
}
}
}
public static int sayfano(){
return c;
}
public static int getViewPager() {
return mViewPager.getCurrentItem();
}
}