Java – listview update single line
Once I press the button, I want to update the single line in the list view with different content I know I can use notifydatasetchanged(), but this will update the entire listview
I have read this answer and it is very suitable for what I want to do
I have finished the list view of 5 lines. When I press the button, I want to update line 4 with different text I don't want to set the text programmatically because it's just a virtual project to see if it can refresh a single line, and my real project is much more complex than just textview
So my question is: can I use getview () to update the single line in listview?
This is my code:
My activities:
public class MainActivity extends Activity { public ListView list1; public listadapter adapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); list1 = (ListView) findViewById(R.id.my_list); adapter = new listadapter(this); list1.setAdapter(adapter); Button button1 = (Button) findViewById(R.id.my_button); button1.setOnClickListener(new OnClickListener() { public void onClick(View v) { adapter.setText("Different text"); View row2Update = list1.getChildAt(3); list1.getAdapter().getView(3,row2Update,list1); } }); } }
My adapter:
public class listadapter extends BaseAdapter{ public Activity activity; public String text="Normal Text"; public listadapter(Activity activity){ this.activity = activity; } public void setText(String text){ this.text = text; } public int getCount() { return 5; } public Object getItem(int position) { return null; } public long getItemId(int position) { // TODO Auto-generated method stub return 0; } public View getView(int position,View convertView,ViewGroup parent) { LayoutInflater inflater = activity.getLayoutInflater(); LinearLayout rowView = (LinearLayout) inflater.inflate(R.layout.row_layout,null); TextView textView = (TextView) rowView.findViewById(R.id.row_text); textView.setText(text); return rowView; } }
This is what the activity looks like:
But when I press my button, nothing changes
Solution
You cannot call the getview () method of the adapter yourself The getview () method of the adapter is called only when called
>The list view is created > when the user scrolls the list view > when notifysetdatachanged() is called
All this is done by the operating system Call getview() for all rows in the list view It requires more than one line Therefore, if you want to change the row, you must provide the data again in string [], ArrayList < > If you want to display different text for a single line, onclick () of the button – you can do this
public class listadapter extends BaseAdapter {
public Activity activity; public ArrayList<String> text; public listadapter(Activity activity){ this.activity = activity; } public void setText(ArrayList<String> text){ this.text = text; } public int getCount() { return 5; } public Object getItem(int position) { return null; } public long getItemId(int position) { // TODO Auto-generated method stub return 0; } public View getView(int position,ViewGroup parent) { LayoutInflater inflater = activity.getLayoutInflater(); LinearLayout rowView = (LinearLayout) inflater.inflate(R.layout.row_layout,null); TextView textView = (TextView) rowView.findViewById(R.id.row_text); textView.setText(text[position]); return rowView; }
}
In your event:
list1 = (ListView) findViewById(R.id.my_list); adapter = new listadapter(this); String[] entries={"Normal Text","Normal Text","Normal text","Normal text"}; ArrayList<String> text=Arrays.asList(entries); adapter.setText(text); list1.setAdapter(adapter); Button button1 = (Button) findViewById(R.id.my_button); button1.setOnClickListener(new OnClickListener() { public void onClick(View v) { text.set(3,"Different Text"); adapter.setText(text); list1.setAdapter(adapter); } });
As @ Andy suggested in one of the comments, there is another way:
listViewPeople.setOnItemClickListener(new ListView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> a,View v,int position,long l) { //IF YOU WANT TO CHANGE THE CONTENT OF THE ROW CLICKED if(position == someNumber) { text.set(position,"different Text"); list1.setAdapter(text); } } });
Sorry, bold text For some reason, Ctrl K does not apply to the above code