【问题标题】:Android Adding view from inflate layout and how to update particular textview value in layoutAndroid 从膨胀布局添加视图以及如何更新布局中的特定文本视图值
【发布时间】:2018-07-21 01:24:15
【问题描述】:

我通过膨胀单独的布局创建了视图。我使用 for 循环创建视图超过 5 个视图,如 ListView。在这里,我要检查通话余额。在视图内部,我将单击余额按钮,一旦收到来自服务器的响应,我想在现有创建的视图中更新特定的 TextView。有什么帮助吗?我期望电话应用余额检查页面也一样。 同时滚动,它不应该隐藏更新的余额。我的参考堆栈链接: 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);
}

【问题讨论】:

  • 我使用 for 循环来创建超过 5 个视图的视图,例如列表视图 -> 你已经使用视图列表了吗? (如 View[] 数组或 ArrayList
  • 请检查我上面的示例代码
  • 这里的position 是什么?
  • 位置仅适用于 i。我还有另一个原创项目。在这里,布局内的简单文本视图更新意味着,我会这样做。在这里,正在通信的服务器,它需要更多的代码来接收响应,无论如何,我是从内部布局出来的。
  • linearlayout.addView(inflater); -> 你是如何添加充气机的?这行得通吗? .

标签: android listview view layout-inflater android-viewgroup


【解决方案1】:

要更新视图属性,您必须存储该视图引用。 在您的情况下,存储您通过膨胀单独的布局创建的视图。

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

【讨论】:

  • 感谢您更新优素福。我正在使用 for 循环来膨胀布局和添加视图。不知道,如何使用特定的行视图进行全局访问。在这里,我没有任何视图数组来检索它。
  • 解决此问题的最佳方法是使用ListViewAdapter。 Android 为此类问题提供了ListViewAdapter。当新数据源可用时,您只需通知适配器即可,ListViewAdapter 将为您处理一切。尝试使用ListViewAdapter
  • 比你优素福。让我检查一下。另外,我也会尝试listview。
【解决方案2】:

您可以使用视图数组轻松识别它们。

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");
        }
        });          
       ...
    }

【讨论】:

  • 感谢您的回复。我也在努力。但是,我没有得到我的 textview 数据更新。但是,我没有收到任何错误。 ((TextView)inflated_layout_view[0].findViewById(R.id.textview_resp)).setText("你的文字");
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-02
  • 2013-07-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多