计算触摸字符串(链接)的次数

时间:2017-12-20 20:52:22

标签: android string random indexing count

我是编程新手,我遇到了问题。我正在创建一个应用程序,当随机点击一个按钮时,会显示与该特定膳食(食谱)的HMTL链接的膳食。我有随机发生器工作,但我需要一个计数器来计算特定膳食被选中的次数。我不知道如何让计数器注意点击了哪个链接(索引)。有人有想法吗?提前致谢。这是我的代码:

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://www.tasteline.com/recept/fisk-i-mild-graddsas-med-kokt-potatis-och-artor/",
                "https://www.coop.se/recept--mat/recept/k/koettbullar-med-stuvade-makaroner/",
                "https://www.ica.se/recept/spaghetti-och-kottfarssas-712805/",


        };
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];
}

0 个答案:

没有答案