微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

列表显示.有没有一种方法可以根据变量在android的Listview中更改行的颜色?

我的代码如下:这使我可以显示来自数据库(MysqL)的数据,但是我要做的就是根据称为手推车计数的变量更改颜色行,这就是我编写的代码.显示具有相同颜色背景的普通列表视图.是否可以根据我的需要进行更改?请勿点击. Activity UI.我希望它像这样显示,其中手推车数量少于5的将是红色,手推车数量大于5但小于15的将显示为橙色,手推车数量大于15的将显示为绿色.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
    mToggle = new ActionBarDrawerToggle(this,mDrawerLayout,R.string.open,R.string.close);
    mDrawerLayout.addDrawerListener(mToggle);
    mToggle.syncState();
    getSupportActionBar().setdisplayHomeAsUpEnabled(true);
    NavigationView navigationView = (NavigationView)findViewById(R.id.navigation_view);
    navigationView.setNavigationItemSelectedListener(this);

    listView = (ListView)findViewById(R.id.listView);
    adapter = new ArrayAdapter<String> (this,android.R.layout.simple_list_item_1);
    listView.setAdapter(adapter);
    new Connection().execute();

    btn =(Button) findViewById(R.id.button2);
    btn.setonClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            openActivity2();
        }
    });
}

private void openActivity2() {
    Intent in = new Intent(this,Main2Activity.class);
    startActivity(in);
}

@Override
public void onPointerCaptureChanged(boolean hasCapture) {

}

class Connection extends AsyncTask<String, String, String>{

    @Override
    protected String doInBackground(String... strings) {
        String result = "";
        String host = "http://10.0.3.2/Client/docks.PHP";
        try
        {
            HttpClient client = new DefaultHttpClient();
            HttpGet request = new HttpGet();
            request.setURI(new URI(host));
            HttpResponse response = client.execute(request);
            BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

            StringBuffer stringBuffer = new StringBuffer("");

            String line = "";
            while ((line = reader.readLine())!=null)
            {
                stringBuffer.append(line);
                break;
            }
            reader.close();
            result = stringBuffer.toString();


        }
        catch(Exception e)
        {
            return new String("There exception: "+e.getMessage());
        }

        return result;
    }
    @Override
    protected void onPostExecute(String result)
    {
        Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();
        try {
            JSONObject jsonResult = new JSONObject(result);
            int success = jsonResult.getInt("success");
            if(success==1)
            {
                JSONArray data = jsonResult.getJSONArray("abc");
                for(int i = 0; i < data.length(); i++)
                {
                    JSONObject data1 = data.getJSONObject(i);
                    int id = data1.getInt("dock_id");
                    String name = data1.getString("dock_name");
                    String description = data1.getString("dock_desc");
                    int flightarrival = data1.getInt("flight_arrival");
                    int count = data1.getInt("trolley_count");
                    String time = data1.getString("snapshot_time");
                    String line = id+"."+name+"-"+count+"-"+flightarrival+"-"+time;
                    adapter.add(line);






                }
            }
            else
            {
                Toast.makeText(getApplicationContext(), "there is no data", Toast.LENGTH_SHORT).show();
            }
        } catch (JSONException e) {
            Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
        }

    }
}
@Override
public boolean onoptionsItemSelected(MenuItem item)
{
    if(mToggle.onoptionsItemSelected(item))
    {
        return true;
    }
    return super.onoptionsItemSelected(item);
}

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    int id = item.getItemId();
    if( id == R.id.home)
    {
        Toast.makeText(this,"This is home", Toast.LENGTH_SHORT).show();
    }
    if( id == R.id.trolleycount)
    {
        Toast.makeText(this,"This is trolleycount", Toast.LENGTH_SHORT).show();
    }
    if( id == R.id.log)
    {
        Intent in = new Intent(this,MainActivity.class);
        startActivity(in);
        Toast.makeText(this,"You have successfully logged out of your account", Toast.LENGTH_SHORT).show();

    }
    return false;
    }
}

解决方法:

您可以使用以下代码更改每行的背景颜色.

在这样的onPost方法

    //Above code remains same,,

    String line = id+"."+name+"-"+count+"-"+flightarrival+"-"+time;
    adapter.add(line);

    int items_count = listView.getAdapter().getCount();
    int i = 0;
    while (i <= items_count) {
        if (i < 5) {
            listView.getChildAt(i).setBackgroundColor(getResources().getColor(android.R.color.holo_red_dark));
        } else if (i >= 5 || i < 15) {
            listView.getChildAt(i).setBackgroundColor(getResources().getColor(android.R.color.holo_orange_dark));
        } else {
            listView.getChildAt(i).setBackgroundColor(getResources().getColor(android.R.color.holo_green_dark));
        }
        ++i;
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐