我的代码如下:这允许我显示来自数据库 (mySQL) 的数据,但我想做的是根据名为 trolley count 的变量更改颜色行,这就是我编写的代码。显示具有相同颜色背景的普通 listview
。是否可以根据我的需要进行更改?不涉及点击。 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;
}
我是一名优秀的程序员,十分优秀!