Android 从 inflate 布局添加视图以及如何更新布局中的特定 textview 值
Android Adding view from inflate layout and how to update particular textview value in layout
我已经通过膨胀单独的布局创建了视图。我使用 for 循环来创建超过 5 个视图的视图,例如 ListView。在这里,我要检查通话余额。在视图内部,我将单击平衡按钮,一旦收到来自服务器的响应,我想在现有创建的视图中更新特定的 TextView。有什么帮助吗?我期待 phonepe 应用程序余额检查页面的相同。 同时滚动时,不应隐藏更新后的余额。我的参考堆栈 link:
Updating a textview in different layout xml from the main activity
Create a custom View by inflating a layout?
我的示例代码:
for (int i=0; i<AccNoList.size; i++) {
LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.my_table_row, myTable, true);
inflater.setId(i);
ImageButton balancebtn = (ImageButton) v.findViewById(R.id.balancebtn);
TextView textview_accnumber = (TextView) v.findViewById(R.id.textview_accnumber);
textview_accnumber.setText("Text for this row");
balancebtn.setId(i); // So that when it is clicked we know which one has been clicked!
balancebtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// once received value from server, i want to update the same row textview balance.
checkBalance(i);
}
});
textview_accnumber.setId(i);
tr.setId(i);
textview_accnumber.setText(AccNoList.get(i));
linearlayout.addView(inflater);
}
要更新视图 属性,您必须存储该视图引用。
在您的情况下,存储您通过膨胀单独布局创建的视图。
View view1 = LayoutInflater.from(context).inflate(layoutId, parent, false);
View view2 = LayoutInflater.from(context).inflate(layoutId, parent, false);
View view3 = LayoutInflater.from(context).inflate(layoutId, parent, false);
然后当您收到来自服务器的响应时,通过 findViewById()
方法获取您要更新的子视图的引用并更新子视图的 属性。
示例:
TextView textView1 = view1.findViewById(viewId);
textView1.setText("Updated Text");
TextView textView2 = view2.findViewById(viewId);
textView2.setText("Updated Text");
编辑
// declare it as global
View views[] = new View[size]
for (int i=0; i<AccNoList.size; i++) {
LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
views[i] = inflater.inflate(R.layout.my_table_row, myTable, true);
//inflater.setId(i);
ImageButton balancebtn = (ImageButton) views[i].findViewById(R.id.balancebtn);
TextView textview_accnumber = (TextView) views[i].findViewById(R.id.textview_accnumber);
textview_accnumber.setText("Text for this row");
// balancebtn.setId(i); // So that when it is clicked we know which one has been clicked!
balancebtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// once received value from server, i want to update the same row textview balance.
checkBalance(i);
}
});
//textview_accnumber.setId(i);
//tr.setId(i);
textview_accnumber.setText(AccNoList.get(i));
linearlayout.addView(inflater);
}
checkBalance 方法
public void checkBalance(final int position){
// .................
// ..............
// after received response
updateStatus(position);
}
updateStatus 方法
private void updateStatus(int position, String text){
if(position < views.length){
TextView textview_accnumber = (TextView)views[i].findViewById(R.id.textview_accnumber);
if(textview_accnumber != null)
textview_accnumber.setText(text);
}
}
您可以使用视图数组轻松识别它们。
View[] viewlist=new View[AccNoList.size];
for (int i=0; i<AccNoList.size; i++) {
LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
viewlist[i]= inflater.inflate(R.layout.my_table_row, myTable, true);
inflater.setId(position);
...
balancebtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// once received value from server, i want to update the same row textview balance.
checkBalance(position);
((TextView)viewlist[i].findViewById(that_id)).setText("Your Text");
}
});
...
}
我已经通过膨胀单独的布局创建了视图。我使用 for 循环来创建超过 5 个视图的视图,例如 ListView。在这里,我要检查通话余额。在视图内部,我将单击平衡按钮,一旦收到来自服务器的响应,我想在现有创建的视图中更新特定的 TextView。有什么帮助吗?我期待 phonepe 应用程序余额检查页面的相同。
Create a custom View by inflating a layout? 我的示例代码:
for (int i=0; i<AccNoList.size; i++) {
LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.my_table_row, myTable, true);
inflater.setId(i);
ImageButton balancebtn = (ImageButton) v.findViewById(R.id.balancebtn);
TextView textview_accnumber = (TextView) v.findViewById(R.id.textview_accnumber);
textview_accnumber.setText("Text for this row");
balancebtn.setId(i); // So that when it is clicked we know which one has been clicked!
balancebtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// once received value from server, i want to update the same row textview balance.
checkBalance(i);
}
});
textview_accnumber.setId(i);
tr.setId(i);
textview_accnumber.setText(AccNoList.get(i));
linearlayout.addView(inflater);
}
要更新视图 属性,您必须存储该视图引用。 在您的情况下,存储您通过膨胀单独布局创建的视图。
View view1 = LayoutInflater.from(context).inflate(layoutId, parent, false);
View view2 = LayoutInflater.from(context).inflate(layoutId, parent, false);
View view3 = LayoutInflater.from(context).inflate(layoutId, parent, false);
然后当您收到来自服务器的响应时,通过 findViewById()
方法获取您要更新的子视图的引用并更新子视图的 属性。
示例:
TextView textView1 = view1.findViewById(viewId);
textView1.setText("Updated Text");
TextView textView2 = view2.findViewById(viewId);
textView2.setText("Updated Text");
编辑
// declare it as global
View views[] = new View[size]
for (int i=0; i<AccNoList.size; i++) {
LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
views[i] = inflater.inflate(R.layout.my_table_row, myTable, true);
//inflater.setId(i);
ImageButton balancebtn = (ImageButton) views[i].findViewById(R.id.balancebtn);
TextView textview_accnumber = (TextView) views[i].findViewById(R.id.textview_accnumber);
textview_accnumber.setText("Text for this row");
// balancebtn.setId(i); // So that when it is clicked we know which one has been clicked!
balancebtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// once received value from server, i want to update the same row textview balance.
checkBalance(i);
}
});
//textview_accnumber.setId(i);
//tr.setId(i);
textview_accnumber.setText(AccNoList.get(i));
linearlayout.addView(inflater);
}
checkBalance 方法
public void checkBalance(final int position){
// .................
// ..............
// after received response
updateStatus(position);
}
updateStatus 方法
private void updateStatus(int position, String text){
if(position < views.length){
TextView textview_accnumber = (TextView)views[i].findViewById(R.id.textview_accnumber);
if(textview_accnumber != null)
textview_accnumber.setText(text);
}
}
您可以使用视图数组轻松识别它们。
View[] viewlist=new View[AccNoList.size];
for (int i=0; i<AccNoList.size; i++) {
LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
viewlist[i]= inflater.inflate(R.layout.my_table_row, myTable, true);
inflater.setId(position);
...
balancebtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// once received value from server, i want to update the same row textview balance.
checkBalance(position);
((TextView)viewlist[i].findViewById(that_id)).setText("Your Text");
}
});
...
}