gpt4 book ai didi

android - 列表显示。有没有办法根据变量更改android中Listview中行的颜色?

转载 作者:太空宇宙 更新时间:2023-11-03 13:08:09 25 4
gpt4 key购买 nike

我的代码如下:这允许我显示来自数据库 (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;
}

关于android - 列表显示。有没有办法根据变量更改android中Listview中行的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52587702/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com