I'm new in programming and I have a problem. I'm making an app that when a button is clicked randomly shows meals with a HMTL-link to that specific meal(recipe). I have the randomizer working and all but I need a counter that counts how many times a specific meal has been selected. I don't know how to make the counter take notice of which link is clicked (index). Anybody out there got an idea? Thanks in advance. Here is my code:
public class MainActivity extends AppCompatActivity {
private CookBook mMatforslag = new CookBook();
private int [] mKlickCounter;
private int clickCounter;
private String CLICK_COUNTER = "clickCounter";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView dinnerLabel = (TextView) findViewById(R.id.DinnerTextView);
final TextView klick = (TextView) findViewById(R.id.textViewKlick);
final Button showDinnerButton = (Button) findViewById(R.id.showDinnerButton);
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View view) {
int index = mMatforslag.getRandomFoodIndex();
String food = mMatforslag.getFood(index);
String link = mMatforslag.getLink(index);
dinnerLabel.setText(food + " \n\n " + link);
}
};
showDinnerButton.setOnClickListener(listener);
TextView newTV = (TextView) findViewById(R.id.DinnerTextView);
newTV.setOnTouchListener(new View.OnTouchListener() {
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
int[] indexKlick = mMatforslag.getIndexKlick;
if (event.getAction() == MotionEvent.ACTION_DOWN) {
clickCounter++;
}
Log.i("clicked text", "click click");
klick.setText(String.valueOf("you have selected the meal: " + clickCounter + " times"));
return false;
}
});
}
public class CookBook {
public int[] mKlickCounter = {0};
public String[] mMatforslag =
{
"Fisk i mild gräddsås med kokt potatis och ärtor",
"Köttbullar med stuvade makaroner",
"Spaghetti och köttfärssås",
};
public String[] mLinks =
{
"http://ift.tt/2CN9HtF",
"http://ift.tt/2BGlDOw",
"http://ift.tt/2CN9J4L",
};
public int[] getIndexKlick;
public int getRandomFoodIndex() {
//Randomly select a dinner
Random randomGenerator = new Random(); //Construct a new Random number generator
int randomNumber = randomGenerator.nextInt(mMatforslag.length);
//Convert random number to text
return randomNumber;
}
public String getFood(int index) {
return mMatforslag[index];
}
public String getLink(int index) {
return mLinks[index];
}
Aucun commentaire:
Enregistrer un commentaire