我有一个Android应用程序,用行填充屏幕,并根据它们是否被点击,背景应为红色或绿色。
如果单击行,背景将变为相反的颜色。我遇到的问题是,如果我点击一行并向下滚动然后向上滚动该行通常不会再设置为正确的颜色。
向下滚动
第二次向下滚动
当我滚动时,这些行似乎随意地改变了背景。
我认为问题出在自定义列表适配器上。这是代码:
public class MyCustomListAdapter extends ArrayAdapter<Coin> {
private ArrayList<Coin> yourArray;
public MyCustomListAdapter(Context ctx, ArrayList<Coin> yourArray){
super(ctx, R.layout.my_custom_list_item, yourArray);
this.yourArray = yourArray;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//Re-use rows to save battery
View row;
if (convertView == null) {
//We inflate our custom view for the ListView item
LayoutInflater inflater = LayoutInflater.from(getContext());
row = inflater.inflate(
R.layout.my_custom_list_item, null);
} else {
row = convertView;
}
Coin coin = yourArray.get(position);
// String[] name= yourArray.get(position);
String year = coin.getYear();//name[0];
String specialty = coin.getSpecialty();//name[2];
String mintage = coin.getMintage();//name[1];
String mint = coin.getMint();
int have = coin.getHave();
TextView tvListItem1 = (TextView) row.findViewById(R.id.textView_year_tag);
TextView tvListItem2 = (TextView) row.findViewById(R.id.textView_mint_tag);
TextView tvListItem3 = (TextView) row.findViewById(R.id.textView_specialty_tag);
TextView tvListItem4 = (TextView) row.findViewById(R.id.textView_mintage_tag);
tvListItem1.setText(year);
tvListItem2.setText(mint);
tvListItem3.setText(specialty);
tvListItem4.setText(mintage);
if(have == 1) {
row.setBackgroundResource(R.color.have);
}
else {
row.setBackgroundResource(R.color.need);
}
return row;
}
如果我删除
if(have == 1) {
row.setBackgroundResource(R.color.have);
}
else {
row.setBackgroundResource(R.color.need);
}
它没有这个问题(但创建时行没有着色)。
我更改onItemClick
MainActivity
行的颜色
public class MainActivity extends ListActivity implements AdapterView.OnItemClickListener {
private CoinsDataSource datasource = new CoinsDataSource(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addValues();
setUpComponents();
}
private void setUpComponents(){
ArrayList<Coin> myValuesToDisplay = getDatabaseContent();
MyCustomListAdapter adapter = new MyCustomListAdapter(this, myValuesToDisplay);
setListAdapter(adapter);
getListView().setOnItemClickListener(this);
}
private ArrayList<Coin> getDatabaseContent(){
datasource.open();
ArrayList<Coin> coins_list = datasource.getAllCoins(MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
return coins_list;
}
public void addValues()
{
//datasource = new CoinsDataSource(this);
datasource.open();
datasource.createCoin("1856", "D", "NEW TEXT", "2000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1857", "P", "MORE TEXT", "17,450,000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1858", "D", "Not NULL", "24,600,000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1859", "D", "NEW TEXT", "2000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1860", "P", "MORE TEXT", "17,450,000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1861", "D", "Not NULL", "24,600,000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1862", "D", "NEW TEXT", "2000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1863", "P", "MORE TEXT", "17,450,000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1864", "D", "Not NULL", "24,600,000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1865", "D", "NEW TEXT", "2000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1866", "P", "MORE TEXT", "17,450,000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1867", "D", "Not NULL", "24,600,000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1868", "D", "NEW TEXT", "2000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1869", "P", "MORE TEXT", "17,450,000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1870", "D", "Not NULL", "24,600,000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1871", "D", "NEW TEXT", "2000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1872", "P", "MORE TEXT", "17,450,000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1873", "D", "Not NULL", "24,600,000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1874", "D", "NEW TEXT", "2000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1875", "P", "MORE TEXT", "17,450,000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1876", "D", "Not NULL", "24,600,000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1877", "D", "NEW TEXT", "2000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1878", "P", "MORE TEXT", "17,450,000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1879", "D", "Not NULL", "24,600,000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1880", "D", "NEW TEXT", "2000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1881", "P", "MORE TEXT", "17,450,000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1882", "D", "Not NULL", "24,600,000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1883", "D", "NEW TEXT", "2000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1884", "P", "MORE TEXT", "17,450,000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1885", "D", "Not NULL", "24,600,000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.close();
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String year_text = ((TextView)view.findViewById(R.id.textView_year_tag)).getText().toString();
String mint_text = ((TextView)view.findViewById(R.id.textView_mint_tag)).getText().toString();
String speciality_text = ((TextView)view.findViewById(R.id.textView_specialty_tag)).getText().toString();
datasource.open();
int x = datasource.adjust_db(MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE, year_text, mint_text, speciality_text);
if(x == 0)
{
TableRow row1 = (TableRow) view.findViewById(R.id.tableRow);
row1.setBackgroundResource(R.color.have);
}
else
{
TableRow row1 = (TableRow) view.findViewById(R.id.tableRow);
row1.setBackgroundResource(R.color.need);
}
datasource.close();
}
当我向下滚动时,有什么想法为什么这些变化不会停留?
答案 0 :(得分:1)
这是你应该做的:
首先我们要定义此适配器:
public class MyCustomListAdapter extends ArrayAdapter<Coin> {
private ArrayList<Coin> yourArray;
private LayoutInflater mInflater;
// This will hold the View Contents
public class RowContent{
TextView tvListItem1;
TextView tvListItem2;
TextView tvListItem3;
TextView tvListItem4;
}
public MyCustomListAdapter(Context ctx, ArrayList<Coin> yourArray){
super(ctx, R.layout.my_custom_list_item, yourArray);
this.yourArray = yourArray;
mInflater = LayoutInflater.from(ctx);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//Re-use rows to save battery
RowContent theRow;
if (convertView == null) {
theRow = new RowContent();
//We inflate our custom view for the ListView item & set it to convertView
convertView = mInflater.inflate(R.layout.my_custom_list_item, null);
// Reference the UI Elements
theRow.tvListItem1 = (TextView) convertView.findViewById(R.id.textView_year_tag);
theRow.tvListItem2 = (TextView) convertView.findViewById(R.id.textView_mint_tag);
theRow.tvListItem3 = (TextView) convertView.findViewById(R.id.textView_specialty_tag);
theRow.tvListItem4 = (TextView) convertView.findViewById(R.id.textView_mintage_tag);
// set the Row Content as the Tag for the View
convertView.setTag(theRow);
} else {
// the row will be recycled
theRow = (RowContent) convertView.getTag();
}
Coin coin = yourArray.get(position);
// String[] name= yourArray.get(position);
String year = coin.getYear();//name[0];
String specialty = coin.getSpecialty();//name[2];
String mintage = coin.getMintage();//name[1];
String mint = coin.getMint();
int have = coin.getHave();
theRow.tvListItem1.setText(year);
theRow.tvListItem2.setText(mint);
theRow.tvListItem3.setText(specialty);
theRow.tvListItem4.setText(mintage);
if(have == 1) {
convertView.setBackgroundResource(R.color.have);
}else {
convertView.setBackgroundResource(R.color.need);
}
return convertView;
}
这是回收物品的正确方法......
然后你想要这样做,而不是手动改变表格行。
public class MainActivity extends ListActivity implements AdapterView.OnItemClickListener {
private CoinsDataSource datasource = new CoinsDataSource(this);
// Have your MyCustomListAdapter be a class member
private MyCustomListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addValues();
setUpComponents();
}
private void setUpComponents(){
ArrayList<Coin> myValuesToDisplay = getDatabaseContent();
// Build the adapter and set it to the member variable
adapter = new MyCustomListAdapter(this, myValuesToDisplay);
setListAdapter(adapter);
getListView().setOnItemClickListener(this);
}
private ArrayList<Coin> getDatabaseContent(){
datasource.open();
ArrayList<Coin> coins_list = datasource.getAllCoins(MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
return coins_list;
}
public void addValues()
{
//datasource = new CoinsDataSource(this);
datasource.open();
datasource.createCoin("1856", "D", "NEW TEXT", "2000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1857", "P", "MORE TEXT", "17,450,000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1858", "D", "Not NULL", "24,600,000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1859", "D", "NEW TEXT", "2000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1860", "P", "MORE TEXT", "17,450,000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1861", "D", "Not NULL", "24,600,000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1862", "D", "NEW TEXT", "2000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1863", "P", "MORE TEXT", "17,450,000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1864", "D", "Not NULL", "24,600,000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1865", "D", "NEW TEXT", "2000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1866", "P", "MORE TEXT", "17,450,000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1867", "D", "Not NULL", "24,600,000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1868", "D", "NEW TEXT", "2000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1869", "P", "MORE TEXT", "17,450,000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1870", "D", "Not NULL", "24,600,000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1871", "D", "NEW TEXT", "2000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1872", "P", "MORE TEXT", "17,450,000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1873", "D", "Not NULL", "24,600,000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1874", "D", "NEW TEXT", "2000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1875", "P", "MORE TEXT", "17,450,000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1876", "D", "Not NULL", "24,600,000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1877", "D", "NEW TEXT", "2000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1878", "P", "MORE TEXT", "17,450,000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1879", "D", "Not NULL", "24,600,000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1880", "D", "NEW TEXT", "2000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1881", "P", "MORE TEXT", "17,450,000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1882", "D", "Not NULL", "24,600,000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1883", "D", "NEW TEXT", "2000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1884", "P", "MORE TEXT", "17,450,000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.createCoin("1885", "D", "Not NULL", "24,600,000", 0, MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE);
datasource.close();
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Update your data in the database
String year_text = ((TextView)view.findViewById(R.id.textView_year_tag)).getText().toString();
String mint_text = ((TextView)view.findViewById(R.id.textView_mint_tag)).getText().toString();
String speciality_text = ((TextView)view.findViewById(R.id.textView_specialty_tag)).getText().toString();
datasource.open();
int x = datasource.adjust_db(MySQLiteHelper.TABLE_PENNY_FLYING_EAGLE, year_text, mint_text, speciality_text);
datasource.close();
// Get this coin from the adapter, and update the coin data object
Coin coin = (Coin) adapter.getItem(position)
coin.setHave(x);
// Tell your adapter its data has changed (this is what i was referring to)
adapter.notifyDataSetChanged();
}
}
答案 1 :(得分:0)
你有没有尝试在if(has = 1)之后插入一个中断时为它着色。有吗?看起来这段代码不会执行。你需要在onClick中找到点击的硬币并将其值更新为1,并在取消选择时将其反复为零。
目前,您只在选择行时对其进行着色。 当从屏幕上移除该行然后再次显示时,它将从适配器获得其颜色,该适配器根据硬币的值来确定其颜色。