在android中加载列表时如何更改背景颜色?

时间:2014-12-18 12:41:44

标签: android android-listview wifi

对不起语言错误。

在我的项目中,我扫描了所有可用的wifi网络ssid并列出了它。同时我有sqlite db,在那个db中我存储了netwotk的ssid。如果我从列表中单击一个网络,它将使用存储在db中的ssid验证所选的ssid,如果它是相同的,则backround颜色变为蓝色到list中的特定ssid。我正在使用onitemclicklistener。

如何在列表页面加载时验证列表项,并在不使用onitemclicklistener的情况下将背景颜色更改为蓝色。

这是我的代码:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    dbhelper=new SdtDBHelper(this);
    listv = (ListView)findViewById(R.id.listView1);
    wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    wifiManager.setWifiEnabled(true);
    wifiReciever = new WifiScanReceiver();
    wifiManager.startScan();
}

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

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

class WifiScanReceiver extends BroadcastReceiver {
    @SuppressLint("UseValueOf")
    public void onReceive(Context c, Intent intent) {
        List<ScanResult> wifiScanList = wifiManager.getScanResults();
        wifis = new String[wifiScanList.size()];
        for(int i = 0; i < wifiScanList.size(); i++){
            /*String ssid = wifiScanList.get(i).SSID;*/
            wifis[i] = (( wifiScanList.get(i).SSID));
        }

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

        listv.setOnItemClickListener(new OnItemClickListener(){

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

                TextView temp=(TextView) view;
                String wifi_id=temp.getText().toString();
                String networkSSID = wifi_id;

                dbhelper.open();
                dbhelper.CreateWifiEntry();
                dbhelper.getAllWifi();
                Cursor cursor=dbhelper.getAllWifi();

                if ( cursor.moveToFirst() ) {
                    // start activity a
                    if(dbhelper.validatewifi(wifi_id)){
                        view.setBackgroundColor(Color.BLUE);
                    }
                    else{
                        /*dbhelper.CreateWifiEntry();*/
                        Toast.makeText(MainActivity.this, "Entry Created..", Toast.LENGTH_LONG).show();
                    }
                }
            }
        });
    }
}

1 个答案:

答案 0 :(得分:0)

创建一个与wifis相对应的Arraylist .... 获得wifis列表后,在数据库中点击查询以获取db中ssid的可用性。 如果它存在,arraylist.get(position) = true,则保留它。

然后

class CustomAdapter extends BaseAdapter/ArrayAdapter{    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

    if (arraylist.get(position)){
        //set Background color here, something like below
        row.setBackgroundColor(getResources().getColor(R.color.white));    
    }
  }
}