1

在我不滚动之前一切正常。只要我向下滚动其他 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();
    }
4

2 回答 2

0

您应该在 onCreateView 方法中重置 TextView 颜色

tv1.setColor(Color.Black)

将使每个 ceil 变为黑色,除了点击(它们将是黄色的)

这一切都是因为 ListView 中的 ceil 被重用了。

升级版:

price = (TextView)row.findViewById(R.id.textView2);
         price.setText(prices[position]);        


if (itemShouldBeYellow(position)){
    price.setColor(Color.Yellow); 
} else {
    price.setColor(Color.Black); 
}
于 2013-02-05T15:26:04.990 回答
0

我认为你的问题可能是这一行:

updated[count]=tv1;

您能否验证所有更改为黄色的文本视图都是从更新后的数组中填充的?

于 2013-02-05T15:27:34.783 回答