0

i want to create ids for three buttons so that i can use them in a switch case i want the ids to be unique in each loop like btna[1].setid("btna"+1),btnb[1].setid("btnb"+1),btnc[3].setid("btnc"+3) somethimg like that and i want to repet this for n number of times here is my code:

package com.astro.famouspandit.Activities.Activity;

import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.astro.famouspandit.Database.DatabaseHelper;
import com.astro.famouspandit.R;
import com.astro.famouspandit.datasets.ItemProfile;

import java.util.ArrayList;

import mehdi.sakout.fancybuttons.FancyButton;

public class ProfleList extends AppCompatActivity implements View.OnClickListener{
    private TextView txtProfileName,txtProfileDate,txtProfileTime,txtProfileLocation;
    private FancyButton btnEdit[],btnDelete[],btnView[];
    LinearLayout linearlist ;

    DatabaseHelper mydb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profle_list);


        mydb = new DatabaseHelper(this);
        long count = mydb.getCount();
        Log.d("count", "onCreate: "+count);

        if (count == 0){

        }else{
            ArrayList<ItemProfile> listprofile = mydb.profilelist();
            for (int i = 1; i < count+1; i++ ) {
                ItemProfile itemProfile = listprofile.get(i-1);

                linearlist = (LinearLayout)findViewById(R.id.layoutlist);
                View[] myView = new View[i];

                LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                myView[i-1] = inflater.inflate(R.layout.profile_list, null);

                txtProfileName = (TextView)myView[i-1].findViewById(R.id.txtName);
                txtProfileDate = (TextView)myView[i-1].findViewById(R.id.txtDate);
                txtProfileTime = (TextView)myView[i-1].findViewById(R.id.txtTime);
                txtProfileLocation = (TextView)myView[i-1].findViewById(R.id.txtLocation);
                btnEdit[i] = (FancyButton)myView[i-1].findViewById(R.id.btn_edtProfile);
                btnDelete[i] = (FancyButton)myView[i-1].findViewById(R.id.btn_deleteProfile);
                btnView[i] = (FancyButton)myView[i-1].findViewById(R.id.btn_viewProfile);

                btnEdit[i].setOnClickListener(this);
                btnDelete[i].setOnClickListener(this);
                btnDelete[i].setOnClickListener(this);





                String profileName = itemProfile.getProfileName();
                txtProfileName.setText(profileName);
                Log.d("ProfileName", "onCreate: "+itemProfile.getProfileName());
                int dd = itemProfile.getDay();
                int mm = itemProfile.getMonth();
                int yy = itemProfile.getYear();
                txtProfileDate.setText(dd+"/"+mm+"/"+yy);
                Log.d("Profiledate", "onCreate: "+itemProfile.getDay()+itemProfile.getMonth()+itemProfile.getYear());
                int hour = itemProfile.getHour();
                int min = itemProfile.getMinute();
                txtProfileTime.setText(hour+":"+min);
                Log.d("ProfileTime", "onCreate: "+itemProfile.getHour()+itemProfile.getHour());
                String city = itemProfile.getCity();
                txtProfileLocation.setText(itemProfile.getCity());
                Log.d("citylocation,city", "onCreate: "+itemProfile.getCity()+","+city);
                linearlist.addView(myView[i-1]);


            }



        }




    }

    @Override
    public void onClick(View v) {

    }
}
bipin
  • 938
  • 3
  • 11
  • 28
  • Your Buttons already have id's in your code example, `R.id.btn_edtProfile`, `R.id.btn_deleteProfile` etc. If your situation is different, then [this](http://stackoverflow.com/a/15442898/1219389) answer will tell you how to generate unique `View` ids. – PPartisan Apr 03 '16 at 09:01

1 Answers1

2

I recommend using View.setTag(), where you can set an object to a View. View also has a method called findViewWithTag(Object tag) where it searches between the child views for the given tag (using Object.equals()). So to adapt it to your case:

btna[1].setTag("btna" + 1);
btnb[1].setTag("btnb" + 1);
btnc[3].setTag("btnc" + 3);

You can easily make a for loop from this.

And then later:

Button btnc = (Button)container.findViewWithTag("btnc3");

Just make sure that the buttons are direct children of the ViewGroup named container.

To handle click events, modify your onClick() this way:

@Override
public void onClick(View v) {
    String tag = v.getTag();
    if (tag.equals("btna1")) {
        Log.i("Test", "Clicked on button A 1");
    } else if (tag.equals("btnb1")) {
        Log.i("Test", "Clicked on button B 1");
    }
}

And so on... You can use switch-case with Strings only in Java 7, which is available from ADT 22.6.

Daniel Zolnai
  • 14,536
  • 7
  • 52
  • 64