列表视图.有没有办法根据变量更改 android 中列表视图中行的颜色?



我的代码如下: 这允许我显示来自数据库(mySQL(的数据,但我想做的是根据称为手推车计数的变量更改颜色行,这就是我编码的。显示具有相同颜色背景的普通listview。是否可以根据我想要的进行更改?不涉及点击。活动用户界面。我希望它像这样显示,其中手推车数量少于 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;
}

执行此操作的一种方法是使用自定义适配器并覆盖getItemViewType(int position(方法。这样做,您可以根据任何参数或变量选择视图

public class CustomAdapter extends ListView.Adapter<ViewHolder> {
@Override
public int getItemViewType(int position) {
//Here you can return an integer based on some value
return (itemCount < 5 ? 0 : (itemCount > 15 ? 1 : 2));
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case 0: return new ViewHolder0(...);
case 1: return new ViewHolder2(...);
...
}
}

您可以简单地创建具有不同背景颜色的自定义视图。

您可以查看此答案: 如何创建具有多种视图类型的回收器视图?

这个想法是使用自定义列表视图项目

CostomItems adapter = new CustomItems(context,list);

private class CutomItems extends ArrayAdapter<String> {
public CutomItems(@NonNull Context context, ArrayList<String> list) {
super(context, R.layout.cutom_items, list);
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater layoutInflater = LayoutInflater.from(getContext());
View myView = layoutInflater.inflate(R.layout.cutom_items, parent, false);
TextView textOfItems = myView.findViewById(R.id.textOfItems);
if (some condition){
textOfItems.setBackgroundColor(R.color.red);
}else if (some other condition){
textOfItems.setBackgroundColor(R.color.blue);
}
return myView;
}
}

如您所见,我创建了一个扩展ArrayAdapter类,该类使用名为cutome_items的布局中的 Items 。 假设我的自定义项目只是一个文本视图。 当getView函数被调用时(创建列表视图时(,每个项目的颜色都会根据条件或您想要的颜色而有所不同!

您可以使用以下代码更改背景颜色:

String color = "#eff0f1";
Drawable mDrawable = context.getResources().getDrawable(R.drawable.circle_background_inside).mutate();
mDrawable.setColorFilter(new
PorterDuffColorFilter(Color.parseColor(color), PorterDuff.Mode.SRC_IN));
binding.layoutColor.setBackground(mDrawable);

最新更新