我正面临一个问题,我明白但无法找到解决方案。因此 - 我有一个listview和一些项目。我想更改背景颜色项目,这完美的作品,但是当项目小于屏幕的大小。当项目更多,我想改变一个项目的背景颜色是不可观看的应用程序崩溃。 下面是一些代码:当试图更改项目背景颜色时,Listview崩溃应用程序
btnRefresh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setItemRed(v);
}
});
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listData);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String name = adapterView.getItemAtPosition(i).toString();
Cursor data = myDB.getItemID(name);
int itemID = -1;
while(data.moveToNext()){
itemID = data.getInt(0);
}
if(itemID > -1){
Intent intent = new Intent(ViewElevators.this, ElevatorInfo.class);
intent.putExtra("id",itemID);
intent.putExtra("name",name);
startActivity(intent);
}
else{
toastMessage("No ID associated with that name");
}
}
});
而且setItemRed(); :
public void setItemRed(View view) {
for (int i = 0; i <= myDB.getLastID(); i++) {
String date = myDB.getcol3(i);
if (date.equals("null")) {
} else {
String day = date.substring(0, 2);
String dayp = date.substring(0, 1);
int dayp1 = Integer.parseInt(dayp);
int day1 = 0;
if (dayp1 == 0) {
String dayone = date.substring(1, 2);
day1 = Integer.parseInt(dayone);
} else {
day1 = Integer.parseInt(day);
}
String currentDate = currentDate();
String currentDay = currentDate.substring(0, 2);
int currentDay1 = Integer.parseInt(currentDay);
if (date.length() == 10) {
if (day1 < currentDay1) {
listView.getChildAt(i).setBackgroundColor(Color.RED);
}
}
}
的logcat:
E/AndroidRuntime(3486):
java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.view.View.setBackgroundColor(int)' on a null object reference
我已阅读并尝试了很多东西,但没有helped.Also,我相信这个问题来自屏幕旋转或列表视图具有存储位置在一个布尔值为ie的情况下记住它们。 非常感谢您的帮助!
这似乎是合乎逻辑的。所以getChildAt()可以获得屏幕配给中的位置吗?我会尝试以其他方式做,谢谢! – Goshoy