在我不滚动之前一切正常。只要我向下滚动其他 TextView 就会随机获取颜色。这是我的代码。
public View getView(final int position, View convertView, ViewGroup parent) {
// ViewHolder holder = null;
View row = convertView;
if(row==null){
LayoutInflater inflater=getLayoutInflater();
row=inflater.inflate(R.layout.prices_list, parent, false);
}
item = (TextView)row.findViewById(R.id.textView1);
item.setText(items[position]);
price = (TextView)row.findViewById(R.id.textView2);
price.setText(prices[position]);
cprice=(Button)row.findViewById(R.id.button1);
cprice.setTag(position);
cprice.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
RelativeLayout rl = (RelativeLayout)v.getParent();
TextView tv1=(TextView)rl.findViewById(R.id.textView1);
TextView tv2=(TextView)rl.findViewById(R.id.textView2);
create(tv2.getText().toString(),tv2,tv1.getText().toString(),position);
}
});
return row;
}
}
create function:
public void create(String price, final TextView tv1,final String item,final int position)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Update Prices");
builder.setMessage("Current Price "+price);
// Use an EditText view to get user input.
final EditText input = new EditText(this);
builder.setView(input);
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton) {
uvalue = input.getText().toString();
tv1.setText(uvalue);
item_value[count]=uvalue;
prices[position]=uvalue;
item_name[count]=item;
updated[count]=tv1;
DBAdapter dbAdapter = DBAdapter.getDBAdapterInstance(getApplicationContext());
try {
dbAdapter.createDataBase();
} catch (IOException e) {
}
dbAdapter.openDataBase();
try {
String query = "UPDATE Prices SET Price="+item_value[count]+" where Item_Name='"+item_name[count]+"'";
Log.i("TAG","Query executed");
Cursor c = dbAdapter.selectRecordsFromDB(query, null);
ContentValues cv= new ContentValues();
cv.put("Price", item_value[count]);
Log.i("TAG","value to be updated is"+item_value[count]);
Log.i("TAG","Item to be updated is"+item_name[count]);
boolean c1= dbAdapter.updateRecordInDB("Prices", cv, "Item_Name='"+item_name[count]+"'", null);
count++;
if(c1)
tv1.setTextColor(Color.YELLOW);
}
catch (Exception e) {
e.printStackTrace();
}
dbAdapter.close();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.show();
}