Android Adding view from inflate layout and how to update particular textview value in layout
Asked Answered
M

2

-1

I have created view from inflating separate layout. I used for loop to create view more than 5 view like an ListView. Here I want call balance check. Inside view, I will click balance button and once receive response from server, I want to update particular TextView in an existing created view. Any help? I am expecting same for phonepe app balance checking page. enter image description here while scrolling also, it should not hide the updated balance. My Ref stack link: Updating a textview in different layout xml from the main activity

Create a custom View by inflating a layout? My Sample Code:

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);
}
Marras answered 10/2, 2018 at 10:56 Comment(5)
i used for loop to create view more than 5 view like an listview -> do you have done using list of views? (like View[] array or ArrayList<View> )Gaudery
pls check my above sample codeMarras
what is position here?Gaudery
position is for i only. i have another original project is there. here, simple textview update inside layout means, i will do it. here, am communicating server, its taking more code to receive response, anyway, am come out of the inside layout.Marras
linearlayout.addView(inflater); -> How did you add inflater ? did this work? .Gaudery
K
2

To update a view property you have to store that view reference. In your case store the views you have created from inflating separate layout.

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

Then when you receive response from server, get reference the child view you want to update by findViewById() method and update child's property.

Example:

TextView textView1 = view1.findViewById(viewId);
textView1.setText("Updated Text");

TextView textView2 = view2.findViewById(viewId);
textView2.setText("Updated Text");

Edit

// 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 method

public void checkBalance(final int position){
  // .................
  // ..............
 // after received response
    updateStatus(position);
}

updateStatus method

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);
  }
}
Kermit answered 10/2, 2018 at 11:7 Comment(3)
thank you for update yousuf. am using for loop for inflating layout and add view. dont know, how to global access with specific row view. here, i dont have any view array to retrieve it.Marras
Best way to solve this problem is to use ListView and Adapter. Android provides ListView and Adapter for this type of problem. When new data source is available you just have to notify the adapter thats it, ListView and Adapter will handle all for you. Try using ListView and Adapter.Kermit
than you yousuf. let me check above it. also, i will try for listview also.Marras
G
1

You can use array of views to identify them easily.

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");
        }
        });          
       ...
    }
Gaudery answered 10/2, 2018 at 11:23 Comment(1)
thank for reply. i am trying for same. but, i did not get my textview data update. but, i did not receive any error. ((TextView)inflated_layout_view[0].findViewById(R.id.textview_resp)).setText("Your Text");Marras

© 2022 - 2024 — McMap. All rights reserved.