我想创建三个按钮的ID,以便我可以在切换情况下使用它们我希望id在每个循环中都是唯一的,如btna [1] .setid(“btna”+1),btnb [1] .setid(“btnb”+1),btnc [3] .setid(“btnc”+3)somethimg like that我想重复这个n次 这是我的代码:
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) {
}
}
答案 0 :(得分:2)
我建议使用View.setTag()
,您可以在其中将对象设置为View。
View还有一个名为findViewWithTag(Object tag)
的方法,它在子视图之间搜索给定标记(使用Object.equals()
)。
所以要适应你的情况:
btna[1].setTag("btna" + 1);
btnb[1].setTag("btnb" + 1);
btnc[3].setTag("btnc" + 3);
您可以轻松地从中进行for循环。
然后是:
Button btnc = (Button)container.findViewWithTag("btnc3");
只需确保按钮是名为ViewGroup
的{{1}}的直接子项。
要处理点击事件,请以这种方式修改container
:
onClick()
依旧...... 您只能在Java 7中使用带有字符串的switch-case,它可以从ADT 22.6获得。