如何在ListView中更改子项的颜色?

时间:2014-04-13 12:02:57

标签: android

我正在制作一个项目,我正在获得目前可用的无线网络。 问题是它以白色显示wifi网络。我可以知道如何更改列表的颜色

我使用的代码是......

WifiManager mainWifi;
    WifiReceiver receiverWifi;
    List<ScanResult> wifiList;
    String wifis[];
    StringBuilder sb = new StringBuilder();
    int list;
    ListView list1;
    Button b;
    ProgressDialog p;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        p=new ProgressDialog(this);
        p.setTitle("Searching Wifi Networks");
        p.setMessage("Loading");
        p.show();

        Thread t= new Thread(new updater());
        t.start();

        list1 = (ListView) findViewById(R.id.listView1);
        b=(Button)findViewById(R.id.button1);
        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                registerReceiver(receiverWifi, new IntentFilter(
                        WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
                mainWifi.startScan();

            }
        });
        mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        mainWifi.setWifiEnabled(true);
        receiverWifi = new WifiReceiver();


        // Register broadcast receiver
        // Broacast receiver will automatically call when number of wifi
        // connections changed
        registerReceiver(receiverWifi, new IntentFilter(
                WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
        mainWifi.startScan();
            }

    public class updater extends Thread{
        @Override
        public void run() {

            // TODO Auto-generated method stub
            super.run();
            p.incrementProgressBy(20);
            for(int ii=0;ii<5;ii++){
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            }p.dismiss();
        }
    }

    class WifiReceiver extends BroadcastReceiver {

        // This method call when number of wifi connections changed
        public void onReceive(Context c, Intent intent) {

            sb = new StringBuilder();
            wifiList = mainWifi.getScanResults();
            wifis = new String[wifiList.size()];
            for (int i = 0; i < wifiList.size(); i++) {
                wifis[i] = ((wifiList.get(i).SSID).toString());
            }
            //list1.setBackgroundColor(Color.BLACK);




            list1.setAdapter(new ArrayAdapter<String>(getApplicationContext(),
                    android.R.layout.simple_list_item_1 , android.R.id.text1, wifis));

            list1.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    // TODO Auto-generated method stub
                     int itemPosition     = position;

                        // ListView Clicked item value
                        String  itemValue    = (String) list1.getItemAtPosition(position);

                         Intent i= new Intent(MainActivity.this,Progressshow.class);
                         i.putExtra("Name", itemValue);
                         startActivity(i);
                }


            });

        }
    }




    protected void onPause() {
        unregisterReceiver(receiverWifi);
        super.onPause();
    }

    protected void onResume() {
        registerReceiver(receiverWifi, new IntentFilter(
                WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
        super.onResume();
    }
}

2 个答案:

答案 0 :(得分:1)

创建您自己的自定义列表视图,以便您可以根据您的要求进行修改,请查看

Custom List View Tutorial

如果您有任何其他疑问,请随意提问

答案 1 :(得分:0)

您可以使用

更改listView的背景颜色
list1.setBackgroundColor(Color.GREEN);

您还可以为列表项定义自定义布局(而不是使用标准android.R.layout.simple_list_item_1)并在其中设置颜色:

android:background="@android:color/darker_gray"