Skip to content Skip to sidebar Skip to footer

Android Adding View From Inflate Layout And How To Update Particular Textview Value In Layout

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 bala

Solution 1:

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:

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

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

Edit

// declare it as global
View views[] = newView[size]

for (int i=0; i<AccNoList.size; i++) {
  LayoutInflaterinflater= (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  views[i] =  inflater.inflate(R.layout.my_table_row, myTable, true);
   //inflater.setId(i);ImageButtonbalancebtn= (ImageButton) views[i].findViewById(R.id.balancebtn);
  TextViewtextview_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(newView.OnClickListener() {
     @OverridepublicvoidonClick(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

publicvoidcheckBalance(finalint position){
  // .................// ..............// after received responseupdateStatus(position);
}

updateStatus method

privatevoidupdateStatus(int position, String text){
  if(position < views.length){
     TextViewtextview_accnumber= (TextView)views[i].findViewById(R.id.textview_accnumber);
     if(textview_accnumber != null)
       textview_accnumber.setText(text);
  }
}

Solution 2:

You can use array of views to identify them easily.

View[] viewlist=newView[AccNoList.size];
    for (int i=0; i<AccNoList.size; i++) {
        LayoutInflaterinflater= (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        viewlist[i]=  inflater.inflate(R.layout.my_table_row, myTable, true);
        inflater.setId(position);
        ...
        balancebtn.setOnClickListener(newView.OnClickListener() {
        @OverridepublicvoidonClick(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");
        }
        });          
       ...
    }

Post a Comment for "Android Adding View From Inflate Layout And How To Update Particular Textview Value In Layout"