将列表视图的背景颜色更改为另一种颜色
Change background color of listview to another color
我正在 Android 中处理一个应用程序,我在那里关闭了所有服务器。因此,我使用 ArrayAdapter 和 Listview。
在后台进程中,我遍历 IP 地址并关闭所有服务器。
现在,我希望在遍历我的服务器时将 ListView 中的每一行着色为绿色(意味着仍在处理它以将其关闭)或在服务器关闭后立即着色为红色。
我可以在扩展 ArrayAdapter 时用不同的颜色为每一行着色,然后在 getView 方法中为它们全部着色。
但是在后台进程中遍历每一行时我该怎么做呢?
在 Activity class.
的调用期间正在设置我的适配器
我是否也必须将 setAdapter 方法放在我的后台进程中,或者类似的东西?
这是我的代码:
protected void onCreate(Bundle savedInstanceState) {
initComponents();
}
private void initComponents() {
model = new SharedPreferenceModel(getBaseContext());
mydb = new DatabaseHelper(this);
array_list = mydb.getAllCotacts();
hostsOnline = new ArrayList<String>();
btnShutdown = findViewById(R.id.btnShutdown);
lv = (ListView) findViewById(R.id.listView);
CustomArrayAdapter custom = new CustomArrayAdapter(this, android.R.layout.simple_list_item_1, array_list);
lv.setAdapter(custom);
}
private void addListeners(final ShutdownServers shutdownServers) {
btnShutdown.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new AsyncTask<Integer, String, Void>() {
@Override
protected Void doInBackground(Integer... params) {
try {
for(int i = 0; i<array_list.size(); i++){
posInArray++;
String host = array_list.get(i);
if(host.equals("192.168.1.1"))
publishProgress("Shutdown " + host);
else
executeRemoteCommand(getBaseContext(), host);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
protected void onProgressUpdate(String... values) {
hostsOnline.add(values[0]);
custom.setNotifyOnChange(true);
custom.notifyDataSetChanged();
}
}.execute(1);
}
});
}
感谢您的帮助!
您可以使用setNotifyOnChange(boolean) 方法和相应的add()、remove 等方法来控制列表状态(添加、删除、更改项目)。请记住,如果没有它,支持数组字段的更改状态不会自动触发 UI 更改。如果想手动控制变化,可以使用ArrayAdapter的notifyDataSetChanged()方法。
这都是因为 ArrayAdapter 尝试只实例化视图一次,并在向下滚动时将它们重新用于不同的数组元素。 View 的状态应该只在 getView() 中修改,通常每个数组元素只调用一次,当它第一次在屏幕上呈现时。但是,您可以随时使用 notifyDataSetChanged() 强制 'redraw' 以保持 UI 状态与后备数组字段一致。
lv.setBackgroundResource(R.drawable.your file)// from drawable
lv.setBackgroundResource(Color.BLACK)// from color by default
现在我可以解决着色问题了。这是我的解决方案:
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
// Get the current item from ListView
View view = super.getView(position,convertView,parent);
if(notifyCalling==1 && position == getPos()){
Log.d("getView - if - position", String.valueOf(position));
view.setBackgroundColor(Color.GREEN);
}else if(notifyCalling ==1 && position < getPos()){
Log.d("getView - elseif - position", String.valueOf(position));
view.setBackgroundColor(Color.RED);
}else if (position % 2 == 1) {
view.setBackgroundColor(Color.LTGRAY);
} else {
view.setBackgroundColor(Color.WHITE);
}
return view;
}
private void addListeners(final ShutdownServers shutdownServers) {
btnShutdown.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btnShutdown.setClickable(false);
new AsyncTask<Integer, String, Void>() {
@Override
protected Void doInBackground(Integer... params) {
try {
for(int i = 0; i<array_list.size(); i++){
String host = array_list.get(i);
publishProgress(host);
executeRemoteCommand(getBaseContext(), host);
setIndex(i+1);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
protected void onProgressUpdate(String... values) {
custom.setNotifyOnChange(true);
custom.notifyDataSetChanged(getIndex());
}
}.execute(1);
}
});
}
我正在 Android 中处理一个应用程序,我在那里关闭了所有服务器。因此,我使用 ArrayAdapter 和 Listview。
在后台进程中,我遍历 IP 地址并关闭所有服务器。
现在,我希望在遍历我的服务器时将 ListView 中的每一行着色为绿色(意味着仍在处理它以将其关闭)或在服务器关闭后立即着色为红色。
我可以在扩展 ArrayAdapter 时用不同的颜色为每一行着色,然后在 getView 方法中为它们全部着色。
但是在后台进程中遍历每一行时我该怎么做呢?
在 Activity class.
的调用期间正在设置我的适配器我是否也必须将 setAdapter 方法放在我的后台进程中,或者类似的东西?
这是我的代码:
protected void onCreate(Bundle savedInstanceState) {
initComponents();
}
private void initComponents() {
model = new SharedPreferenceModel(getBaseContext());
mydb = new DatabaseHelper(this);
array_list = mydb.getAllCotacts();
hostsOnline = new ArrayList<String>();
btnShutdown = findViewById(R.id.btnShutdown);
lv = (ListView) findViewById(R.id.listView);
CustomArrayAdapter custom = new CustomArrayAdapter(this, android.R.layout.simple_list_item_1, array_list);
lv.setAdapter(custom);
}
private void addListeners(final ShutdownServers shutdownServers) {
btnShutdown.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new AsyncTask<Integer, String, Void>() {
@Override
protected Void doInBackground(Integer... params) {
try {
for(int i = 0; i<array_list.size(); i++){
posInArray++;
String host = array_list.get(i);
if(host.equals("192.168.1.1"))
publishProgress("Shutdown " + host);
else
executeRemoteCommand(getBaseContext(), host);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
protected void onProgressUpdate(String... values) {
hostsOnline.add(values[0]);
custom.setNotifyOnChange(true);
custom.notifyDataSetChanged();
}
}.execute(1);
}
});
}
感谢您的帮助!
您可以使用setNotifyOnChange(boolean) 方法和相应的add()、remove 等方法来控制列表状态(添加、删除、更改项目)。请记住,如果没有它,支持数组字段的更改状态不会自动触发 UI 更改。如果想手动控制变化,可以使用ArrayAdapter的notifyDataSetChanged()方法。
这都是因为 ArrayAdapter 尝试只实例化视图一次,并在向下滚动时将它们重新用于不同的数组元素。 View 的状态应该只在 getView() 中修改,通常每个数组元素只调用一次,当它第一次在屏幕上呈现时。但是,您可以随时使用 notifyDataSetChanged() 强制 'redraw' 以保持 UI 状态与后备数组字段一致。
lv.setBackgroundResource(R.drawable.your file)// from drawable
lv.setBackgroundResource(Color.BLACK)// from color by default
现在我可以解决着色问题了。这是我的解决方案:
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
// Get the current item from ListView
View view = super.getView(position,convertView,parent);
if(notifyCalling==1 && position == getPos()){
Log.d("getView - if - position", String.valueOf(position));
view.setBackgroundColor(Color.GREEN);
}else if(notifyCalling ==1 && position < getPos()){
Log.d("getView - elseif - position", String.valueOf(position));
view.setBackgroundColor(Color.RED);
}else if (position % 2 == 1) {
view.setBackgroundColor(Color.LTGRAY);
} else {
view.setBackgroundColor(Color.WHITE);
}
return view;
}
private void addListeners(final ShutdownServers shutdownServers) {
btnShutdown.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btnShutdown.setClickable(false);
new AsyncTask<Integer, String, Void>() {
@Override
protected Void doInBackground(Integer... params) {
try {
for(int i = 0; i<array_list.size(); i++){
String host = array_list.get(i);
publishProgress(host);
executeRemoteCommand(getBaseContext(), host);
setIndex(i+1);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
protected void onProgressUpdate(String... values) {
custom.setNotifyOnChange(true);
custom.notifyDataSetChanged(getIndex());
}
}.execute(1);
}
});
}