0

I am using an async task and i want to change the color of each row twice,

i.e

1.I want to change the color of the row before starting my long running task

2.I want to change the color of the row after completing my long running task

Both 1.and 2. are from doinBackground.

This is my adapter:

    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,al);
    listView.setAdapter(arrayAdapter);

This is my async task

class AT extends AsyncTask<List<String>,Void,Void>{

    @Override
    protected Void doInBackground(List<String>... list) {

        for(int i=0;i<n;i++){
     //iterating my list , so that for every item of my list,i am performing  my long running task and changing the color of rach row

                final int t=i;
        //trying to change the associate row color before starting my task        
            handler.post(new Runnable() {
                @Override
                public void run(){
                Log.d("sri","listView.performItemClick;");
                 listView.performItemClick(listView.getAdapter().getView(t,null,listView),t,t);
                }});


            try {
                Thread.sleep(5000);
                Log.d("TAG","doInBackground =====> Thread.sleep(5000);");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //my long running task would be here

            try {
                Thread.sleep(3000);
                Log.d("sri","doInBackground =====> Thread.sleep(3000);");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

      //trying to change the associate row color after completing my task        
            handler.post(new Runnable() {
                @Override
                public void run(){
                Log.d("sri","listView.performItemClick;");
                 listView.performItemClick(listView.getAdapter().getView(t,null,listView),t,t);
                }});


        }

        return null;
    }}

This is onItemClick code:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            Log.d("TAG","onItemClick --> pos is " + i + "-->id is " + l );

            view.setBackgroundColor(Color.parseColor("#1234ab"));
            //adapterView.getChildAt(i-adapterView.getFirstVisiblePosition()).setBackgroundColor(Color.BLUE);

            Log.d("TAG","onItemClick --> pos is " + i + "-->id is " + l );
        }
    });

My problem is

The line *listView.performItemClick calls the OnItemClick code * (and the variable pos and id are correct ) , but view.setBackgroundColor(Color.parseColor("#1234ab")); has no effect,so the color is not changing.

adi
  • 984
  • 15
  • 33
  • Don't update the ListView UI from the AsyncTask. Update the data within the adapter and let the adapter set the background accordingly – OneCricketeer Dec 26 '16 at 22:11

2 Answers2

0

Try this one:

listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adpterView, View view, int position,
                long id) {
                    listView.getChildAt(i).setBackgroundColor(Color.BLUE);     
            }
        }
    });   
Nick Titov
  • 588
  • 5
  • 19
0

You cannot do it like this , as soon as user scrolls the change will be lost . What you can do is mark the item as selected in an array and notifydatasetChanged and do the check in your adapter , or an easy alternative could be to set the item as selected like explained here(https://stackoverflow.com/a/10791326/2418640) .

Community
  • 1
  • 1
khetanrajesh
  • 300
  • 3
  • 13