I’ve written code to perform a search and display the results below the search form in the XML file.
However, by creating TextViews dynamically, it keeps adding more TextViews so the number of TextViews increases by 5 everytime I perform a search. How could I re-use the TextViews so I can update the text to be displayed rather than creating more TextViews?
for(int i=0; i<5; i++) {
TextView altName = new TextView(getApplicationContext());
altName.setText("blah");
altName.setLayoutParams(new TableRow.LayoutParams(1));
altName.setTextAppearance(getApplicationContext(), R.style.textSize);
TableRow altNameTr = new TableRow(getApplicationContext());
altNameTr.addView(altName);
table.addView(altNameTr);
}
What you need to do is:
Before search you create TextViews and add them to the table. Don’t forget to store them (
ArrayList<TextView>would work fine)Every time you have your search results, go through your TextViews list and update them accordingly (with
setText())So your code should like: