下面是我动态创建的视图,我添加了两行但是我无法获得该特定行的位置,所以我可以得到它。我用一个视图创建了四个文本视图,但是如何获得该特定视图的位置?
点击按钮后,我添加了下面提到的代码,
private LinearLayout llAddMember;
llAddMember = (LinearLayout) findViewById(R.id.llAddMember);
AddFamilyMemberModel familyMemberModel = new AddFamilyMemberModel();
familyMemberModel.setStrMemberName(strMemberName);
familyMemberModel.setStrGender(strGender);
familyMemberModel.setStrMemberRelation(strMemberRelation);
familyMemberModel.setStrMemberAge(strMemberAge);
arrAddFamilyMember.add(familyMemberModel);
LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
llAddMember.setBackgroundColor(getResources().getColor(android.R.color.white));
TextView txtResMemberName = new TextView(RegistrationPatientActivity.this);
txtResMemberName.setLayoutParams(lparams);
txtResMemberName.setText("Member Name: " +familyMemberModel.getStrMemberName());
txtResMemberName.setTypeface(Typeface.createFromAsset(getAssets(),"Roboto-Regular.ttf"));
txtResMemberName.setTextSize(14);
txtResMemberName.setTextColor(Color.parseColor("#9C9C9C"));
txtResMemberName.setSingleLine(true);
llAddMember.addView(txtResMemberName);
lparams.setMargins(50, 5, 40, 0);
View view1 = new View(RegistrationPatientActivity.this);
LayoutParams lparamsView1 = new LayoutParams(LayoutParams.WRAP_CONTENT, 2);
view1.setLayoutParams(lparamsView1);
view1.setBackgroundColor(Color.parseColor("#DEDDDA"));
llAddMember.addView(view1);
lparamsView1.setMargins(50, 10, 0, 0);
TextView txtResGender = new TextView(RegistrationPatientActivity.this);
txtResGender.setLayoutParams(lparams);
txtResGender.setText("Member Gender: " +familyMemberModel.getStrGender());
txtResGender.setTypeface(Typeface.createFromAsset(getAssets(), "Roboto-Regular.ttf"));
txtResGender.setTextSize(14);
txtResGender.setTextColor(Color.parseColor("#9C9C9C"));
llAddMember.addView(txtResGender);
lparams.setMargins(50, 10, 0, 0);
View view2 = new View(RegistrationPatientActivity.this);
LayoutParams lparamsView2 = new LayoutParams(LayoutParams.WRAP_CONTENT, 2);
view2.setLayoutParams(lparamsView2);
view2.setBackgroundColor(Color.parseColor("#DEDDDA"));
llAddMember.addView(view2);
lparamsView2.setMargins(50, 10, 0, 0);
TextView txtResMemberRelation = new TextView(RegistrationPatientActivity.this);
txtResMemberRelation.setLayoutParams(lparams);
txtResMemberRelation.setText("Member Relation: " +familyMemberModel.getStrMemberRelation());
txtResMemberRelation.setTypeface(Typeface.createFromAsset(getAssets(), "Roboto-Regular.ttf"));
txtResMemberRelation.setTextSize(14);
txtResMemberRelation.setTextColor(Color.parseColor("#9C9C9C"));
llAddMember.addView(txtResMemberRelation);
lparams.setMargins(50, 10, 0, 0);
View view3 = new View(RegistrationPatientActivity.this);
LayoutParams lparamsView3 = new LayoutParams(LayoutParams.WRAP_CONTENT, 2);
view3.setLayoutParams(lparamsView3);
view3.setBackgroundColor(Color.parseColor("#DEDDDA"));
llAddMember.addView(view3);
lparamsView3.setMargins(50, 10, 0, 0);
TextView txtResMemberAge = new TextView(RegistrationPatientActivity.this);
txtResMemberAge.setLayoutParams(lparams);
txtResMemberAge.setText("Member Age: " +familyMemberModel.getStrMemberAge());
txtResMemberAge.setTypeface(Typeface.createFromAsset(getAssets(), "Roboto-Regular.ttf"));
txtResMemberAge.setTextSize(14);
txtResMemberAge.setTextColor(Color.parseColor("#9C9C9C"));
llAddMember.addView(txtResMemberAge);
lparams.setMargins(50, 10, 0, 0);
View view = new View(RegistrationPatientActivity.this);
LayoutParams lparamsView = new LayoutParams(LayoutParams.WRAP_CONTENT, 4);
view.setLayoutParams(lparamsView);
view.setBackgroundColor(Color.parseColor("#006D67"));
llAddMember.addView(view);
lparamsView.setMargins(0, 10, 0, 0);
答案 0 :(得分:1)
你正在使用列表视图,对吗?
在您的活动/片段中......
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// do stuff ('position' is the position of the item you've clicked on)
}
});
答案 1 :(得分:0)
如果您只有四个视图,则可以在创建时将标记设置为位置。就像这样:
on *:text,loaddservers:#: {
if (%admin. [ $+ [ $nick ] ] = 1) {
msg $chan All default servers are now being loaded.
set %server1 aurora.irchound.tk
set %server2 jupiter.irchound.tk
set %server3 kepler.irchound.tk
}
else {
msg $chan Access denied.
}
}
;Create socket connection to servers on trigure.
on *:text:,status*:#: {
if ($2 = all) {
sockopen IRCAurora %server1 6667
sockopen IRCKepler %server2 6667
sockopen IRCJupiter %server3 6667
set %chan $chan
msg $chan Connecting to servers...
msg $chan >>>Status of all servers<<<
timer2 1 5 checkall
}
if ($2 = aurora) {
sockopen IRCAurora %server1 6667
set %chan $chan
msg $chan Connecting to server $2 $+ ...
timerAURORA 1 5 checkaurora
}
if ($2 = kepler) {
sockopen IRCKepler %server2 6667
set %chan $chan
msg $chan Connecting to server $2 $+ ...
timerKEPLER 1 5 checkkepler
}
if ($2 = jupiter) {
sockopen IRCJupiter %server3 6667
set %chan $chan
msg $chan Connecting to server $2 $+ ...
timerJUPITER 1 5 checkjupiter
}
}
;Aliases for each server status response.
alias checkaurora {
if ($sock(IRCAurora,pause) == 1) {
msg %chan 1Aurora - 9Online
}
else {
msg %chan 1Aurora - 4Offline
}
halt
}
alias checkjupiter {
if ($sock(IRCJupiter,pause) == 1) {
msg %chan 1Jupiter - 9Online
}
else {
msg %chan 1Jupiter - 4Offline
}
halt
}
alias checkkepler {
if ($sock(IRCKepler,pause) == 1) {
msg %chan 1Kepler - 9Online
}
else {
msg %chan 1Kepler- 4Offline
}
halt
}
alias checkall {
if ($sock(IRCAurora,pause) == 1) {
msg %chan 1Aurora - 9Online
}
else {
msg %chan 1Aurora - 4Offline
}
goto check2
halt
:check2
if ($sock(IRCJupiter,pause) == 1) {
msg %chan 1Jupiter - 9Online
}
else {
msg %chan 1Jupiter - 4Offline
}
goto check3
halt
:check3
if ($sock(IRCKepler,pause) == 1) {
msg %chan 1Kepler - 9Online
}
else {
msg %chan 1Kepler - 4Offline
}
}
然后你可以通过以下方式获得职位:
txtResMemberName.setTag(1);
答案 2 :(得分:0)
如上所述,您已使用LinearLayout管理此View,这不是正确的方法,您需要使用Android中默认提供的RecyclerView或ListView来实现此任务。我建议你仔细阅读Link
答案 3 :(得分:0)
我得出了上述问题的结论,我已将其实施到我的项目中,它对我来说很好。
我创建了一个主要的linearlayout,为此我已将这些视图添加到该布局中,最后我将新创建的视图添加到我的主要linearlayout中,并希望它可以正常工作。
AddFamilyMemberModel familyMemberModel = new AddFamilyMemberModel();
familyMemberModel.setStrMemberName(strMemberName);
familyMemberModel.setStrGender(strGender);
familyMemberModel.setStrMemberRelation(strMemberRelation);
familyMemberModel.setStrMemberAge(strMemberAge);
arrAddFamilyMember.add(familyMemberModel);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout linearLayout = new LinearLayout(getApplicationContext());
linearLayout.setLayoutParams(layoutParams);
linearLayout.setOrientation(LinearLayout.VERTICAL);
LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
linearLayout.setBackgroundColor(getResources().getColor(android.R.color.white));
TextView txtResMemberName = new TextView(RegistrationPatientActivity.this);
txtResMemberName.setLayoutParams(lparams);
txtResMemberName.setText("Member Name: " +familyMemberModel.getStrMemberName());
txtResMemberName.setTypeface(Typeface.createFromAsset(getAssets(),"Roboto-Regular.ttf"));
txtResMemberName.setTextSize(14);
txtResMemberName.setTextColor(Color.parseColor("#9C9C9C"));
txtResMemberName.setSingleLine(true);
linearLayout.addView(txtResMemberName);
lparams.setMargins(50, 5, 40, 0);
View view1 = new View(RegistrationPatientActivity.this);
LayoutParams lparamsView1 = new LayoutParams(LayoutParams.WRAP_CONTENT, 2);
view1.setLayoutParams(lparamsView1);
view1.setBackgroundColor(Color.parseColor("#DEDDDA"));
linearLayout.addView(view1);
lparamsView1.setMargins(50, 10, 0, 0);
TextView txtResGender = new TextView(RegistrationPatientActivity.this);
txtResGender.setLayoutParams(lparams);
txtResGender.setText("Member Gender: " +familyMemberModel.getStrGender());
txtResGender.setTypeface(Typeface.createFromAsset(getAssets(), "Roboto-Regular.ttf"));
txtResGender.setTextSize(14);
txtResGender.setTextColor(Color.parseColor("#9C9C9C"));
linearLayout.addView(txtResGender);
lparams.setMargins(50, 10, 0, 0);
View view2 = new View(RegistrationPatientActivity.this);
LayoutParams lparamsView2 = new LayoutParams(LayoutParams.WRAP_CONTENT, 2);
view2.setLayoutParams(lparamsView2);
view2.setBackgroundColor(Color.parseColor("#DEDDDA"));
linearLayout.addView(view2);
lparamsView2.setMargins(50, 10, 0, 0);
TextView txtResMemberRelation = new TextView(RegistrationPatientActivity.this);
txtResMemberRelation.setLayoutParams(lparams);
txtResMemberRelation.setText("Member Relation: " +familyMemberModel.getStrMemberRelation());
txtResMemberRelation.setTypeface(Typeface.createFromAsset(getAssets(), "Roboto-Regular.ttf"));
txtResMemberRelation.setTextSize(14);
txtResMemberRelation.setTextColor(Color.parseColor("#9C9C9C"));
linearLayout.addView(txtResMemberRelation);
lparams.setMargins(50, 10, 0, 0);
View view3 = new View(RegistrationPatientActivity.this);
LayoutParams lparamsView3 = new LayoutParams(LayoutParams.WRAP_CONTENT, 2);
view3.setLayoutParams(lparamsView3);
view3.setBackgroundColor(Color.parseColor("#DEDDDA"));
linearLayout.addView(view3);
lparamsView3.setMargins(50, 10, 0, 0);
TextView txtResMemberAge = new TextView(RegistrationPatientActivity.this);
txtResMemberAge.setLayoutParams(lparams);
txtResMemberAge.setText("Member Age: " +familyMemberModel.getStrMemberAge());
txtResMemberAge.setTypeface(Typeface.createFromAsset(getAssets(), "Roboto-Regular.ttf"));
txtResMemberAge.setTextSize(14);
txtResMemberAge.setTextColor(Color.parseColor("#9C9C9C"));
linearLayout.addView(txtResMemberAge);
lparams.setMargins(50, 10, 0, 0);
View view = new View(RegistrationPatientActivity.this);
LayoutParams lparamsView = new LayoutParams(LayoutParams.WRAP_CONTENT, 4);
view.setLayoutParams(lparamsView);
view.setBackgroundColor(Color.parseColor("#006D67"));
linearLayout.addView(view);
lparamsView.setMargins(0, 10, 0, 0);
int pos = arrAddFamilyMember.size()-1;
linearLayout.setTag(pos);
linearLayout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int position = Integer.parseInt(v.getTag().toString());
AlertDialogUtility.SHOW_TOAST(RegistrationPatientActivity.this, "Position: "+position);
}
});
llAddMember.addView(linearLayout);