开发者

how to change the background color of a listview when clicked once and when clicked again i need to change the background color without using selector

how to change the background color of a listview when clicked once and when clicked again i need to change the background color without using selector.I have worked on changing the background when i select each item.but when i click again on the item i need to change the background color as red.how can i give the condition.i will post my code here.please give suggestions.please help...

public class ProvierActivity extends Activity {

private String text[] = { "BroadStripe-Cable (Seattle)",
        "BroadStripe-Digital (Seattle)", "BroadStripe-Cable (Seattle)",
        "Comcast king county south)", "BroadStripe-Cable (Seattle)",
        "Comcast king county south", "BroadStripe-Digital (Seattle)",
        "BroadStripe-Digital (Seattle)", "BroadStripe-Cable (Seattle)",
        "Comcast king county south" };

ImageView icon;
public static int selectedRow;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final ListView list = (ListView) findViewById(R.id.listview_id);

    list.setAdapter(new ArrayAdapter<String>(this, R.layout.list,
            R.id.title, text));
    list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapter, View view,
                int position, long arg3) {

            for (int i = 0; i < adapter.getChildCount(); i++) {
                if (i == position) {

                    adapter.getChildAt(i).setBackgroundColor(Color.BLUE);

                } else {
                    ad开发者_运维问答apter.getChildAt(i).setBackgroundColor(Color.BLACK);

                }

            }

        }

    });
}

}


use this edited code.

public static int selectedRow;

/** Called when the activity is first created. */

private int prePos=-1;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final ListView list = (ListView) findViewById(R.id.listview_id);

    list.setAdapter(new ArrayAdapter<String>(this, R.layout.list,
            R.id.title, text));
    list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapter, View view,
                int position, long arg3) {

            for (int i = 0; i < adapter.getChildCount(); i++) {
                if (i == position) {   

                    if(position!=prePos){   
                       adapter.getChildAt(i).setBackgroundColor(Color.BLUE);

                      prePos=position;

                    }else{
                        adapter.getChildAt(i).setBackgroundColor(Color.BLACK);
                          prePos=-1;
                        }

                   }else{

                      adapter.getChildAt(i).setBackgroundColor(Color.BLACK);

                   }

                }


        }

    });
}

}

this will help you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜