Skip to content Skip to sidebar Skip to footer

Set Text To Textview At A Particular Position Inside A Listview

I am using a ListView with a custom Adapter. I have two TextView inside each row. I need to change the text of these TextView only for the textViews which I click. How can I achiev

Solution 1:

Override the getView(...) function of CustomAdapter,where one of the arguments is int position .

Don't forget to use convertView, holder and settag/gettag for smooth scrolling and correct references

Update:

in getView put:

convertView.setOnClickListener(newOnClickListener() {
        @OverridepublicvoidonClick(View v) {
           //perform update on textView here
        } 
    }); 

Also we need reference of convertView in postExecute(). You can pass this reference in AsycTask arguments or set getter setter for the adapter and get clicked reference in asyctask postexecute()

Solution 2:

create a model(POJO) class first with your two strings and one boolean value call it as isChecked add getters and setters to it

set values(list data) to your arraylist and keep setIsChecked value to false for the first time. like this

ArrayList<model> list = new ArrayList<model>();
list.add(new model("string1","string2",false);
list.add(new model("string11","string22",false);

and so on

now onItemClickListener use for loop like this

for(int i = 0 ; i < list.size; i++){
list.get(i).setIsChecked(false);
}
list.get(itemClickedPosition).setIsChecked(true);
youradapter.notifyDataSetChanged();

after that now come to the adapter class where you can set the selected textview as you want do like this

in getView method do this

if(list.get(position).getIsChecked()){
change your textview
}else{
leave as it is
}

done let me know if it helps you

Solution 3:

PassViewinAsyncTask and then convert into textView and SetTextinDoInBackgroundMethod.

   newPushTask().execute(v);


    @OverrideprotectedIntegerdoInBackground(final View position) {
     runOnUiThread(newRunnable() {
                    publicvoidrun() {
                        // some code #3 (Write your code here to run in UI thread)TextView txtName=(TextView)position;
       txtname.setText(“android”):
                    }
                });

    }

Post a Comment for "Set Text To Textview At A Particular Position Inside A Listview"