ListView setText at a specific position

I have defined the following code to be able to set the text in a specific position.

if(position == 0) { holder.text.setText("blah"); } 

I would like to check if position 0 is equal, and then, if there is one, set the text in position p - every time p = position + 1 (next position). Can we set the text in a text image from the holder, but in a certain position.

Thanks.

+5
source share
6 answers

I think you are still using ListView, if not please update the title of the question.

In your getView(int position, View convertView, ViewGroup parent) method getView(int position, View convertView, ViewGroup parent) check the position for which your view is being created. Since you are already using the owner, I assume that you did (convertView! = Null) a check before retrieving the owner.

Here in getView (int position, View convertView, parent group of ViewGroup), you can check

 if (position == 0) { holder.text.setText("blah"); } else { holder.text.setText(""); } 

Remember that the else part is required because the views are usually reused, and therefore your early β€œblah” must be reset or will be displayed in other views differently when scrolling through your view.

A detailed explanation of how view reuse works, you can check out this great talk in which Romain Guy, who explains everything about ListView,

+6
source

First, you need to implement your own RecyclerView.Adapter inside the adapter, you need to implement the onBindViewHolder () method, where you can specify the text according to the position.

When the position is 0, you get the text and save it in the String field, and when the position is 1, you set the text of this TextView accordingly.

 public class CustomAdapter extends RecyclerView.Adapter { private String tempText; ... @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { if(position == 0) { tempText = "bla"; }else if (position == 1){ holder.text.setText(tempText); } } 

}

+3
source

Attention! This is using RecycleView, it invalidated all views per action and has a view caching mechanism. This can cause problems when changing Views!

The best solution using your own adapter. Or, the best solutions, it uses an adapter for this task - LinkedListView . What works, as well as RecycleView, bu also supporting changing looks at any time.

+1
source

you need to update the list data. Inside the getView () method and don’t worry about the position, because it will automatically increase.

  public class ViewHolder { TextView text; } public View getView(final int position, View rView, ViewGroup parent) { currentPosition = position; rowView =rView; if(rowView == null) { LayoutInflater inflater = LayoutInflater.from(context); rowView= inflater.inflate(R.layout.list_item_layout, null); viewHolder = new ViewHolder(); viewHolder.text= (TextView) rowView.findViewById(R.id.textView); rowView.setTag(viewHolder); } else { viewHolder = (ViewHolder) rowView.getTag(); } if(position==0){ viewHolder.text.setText("your Text"); } else if(position==1){ viewHolder.text.setText("your Text 1"); } notifyDataSetChanged();//this will update your views } 
+1
source

Add this code to your CustomAdapter.class

 public View getView(final int position, View convertView, final ViewGroup parent) { ViewHolder holder = new ViewHolder(); holder.txtDetails = (TextView) vi.findViewById(R.id.txt_expenses_details); holder.txtDetails.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ((ListView) parent).performItemClick(v, position, 0); } }); vi.setTag(holder); return vi; } 

In your MainActivity.class

click text and change value

 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { long viewId = view.getId(); if (viewId == R.id.txt_expenses_details) { ((TextView)view).setText("Hey, I've just been tapped on!"); customListAdapter.notifyDataSetChanged(); } } }); 

Click list view and change the value (or)

 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { TextView tv = (TextView) view.findViewById(R.id.txt_expenses_details); tv.setText(...); customListAdapter.notifyDataSetChanged(); } }); 
0
source

I think the apporach to set the text in a certain position is wrong. The best practice of using code is to modify data. Say your data structure is similar -

 class MyData{ public String status; } ArrayList<MyData> adapterList; 

Now, if you have a MyData list for the adapter, and you want to change the 5th data, then do this:

 private void updatePostion(positionToChange = 5){ for(int i = 0; i < adapterList.size(); i++) adapterList.get(i).status = ""; adapterList.get(positionToChange).status = "blah"; notifyDataSetChanged(); } 

Hope this helps you :)

0
source

All Articles