ListView setText on particular position
Asked Answered
G

6

5

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

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

What I would like is to check if the position is equals 0, and then if it is, set text on position p - every time it's p = position + 1 (next position). Are we able to set text to a textview from a holder but in a certain position.

Thanks.

Grayce answered 22/6, 2016 at 13:25 Comment(6)
By position you refer to the position of the view inside a list view ?Akimbo
@Akimbo Yes... Adapter positionGrayce
getPosition() and getItem() are for this only. Yes, you can so that You need to invalidate each time if you are replacing any item.Disorient
@Disorient Could you provide an example, please?Grayce
Check these answer -> link1 link2Disorient
have you found the solution ??Keane
K
8

I think that you are still using ListView, if not the please update the Title of Question.

In your getView(int position, View convertView, ViewGroup parent) method, check the position for which your view is being created. Since you are already using holder, I am assuming you have done (convertView != null) check before extracting the holder.

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

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

Remember the else part is required as the views tend to get reused and so your earlier "blah" needs to be reset or else will be visible in other views when your view scrolls.

For a detailed explanation on how reuse of views work, you can check this great talk in which Romain Guy, which explains everything about ListView.

Kinase answered 29/6, 2016 at 7:16 Comment(0)
S
3

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

When the position is 0, you get some text and save it into a String field, and when the position is 1, you set the text of that 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);
    }
}

}

Slovene answered 24/6, 2016 at 13:48 Comment(6)
I can set the text according to the position in my listview adapter, I'm asking how could I set the text on the next position like: if(position == 0) { holder.text.setText("blah"); position 1 holder set text }Grayce
Can you give me the specific requirement please so I can decide what we need to do. If you want to set the text in position1, why don't we just use if(position == 1) ?Slovene
I wan't to set text in position 1, while being in if(position == 0). I can't call if(position == 1), because I'm in position 0.Grayce
You can save the text in a field or somewhere, when the position == 1 and set the TextView text to be what you want.Slovene
But we have another thing, the text is dynamic, when I'm on position 0 i need to set text to position 1 from position 0, and when I'm on position 1 i need to set text to position 2 from position 1, and so on..Grayce
You should change the data in the list and call adapter.notifyDataSetChanged();Slovene
P
1

Note! That using RecycleView, he invalidated all views per action, and has mechanism for caching views. It may produce problems for changing Views!

Better solution, it's using you own adapter. Or, best solutions, it's use Adapter for this task - LinkedListView. Which works as well as RecycleView, bu also supporting changing views in any time.

Pursuit answered 30/6, 2016 at 8:51 Comment(0)
B
1

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

 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
}
Banyan answered 1/7, 2016 at 5:44 Comment(0)
L
0

In your CustomAdapter.class add this code

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 textview 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 listview and change 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();
        }
    });
Longsighted answered 29/6, 2016 at 13:9 Comment(0)
I
0

I think the apporach to set text at particular position is wrong. Better code practice is to change in the data. Let say your data structure is like -

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

Now if you have a list of MyData for adapter and you want to change the 5th data then do like 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 it will help you :)

Irreclaimable answered 30/6, 2016 at 20:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.