在列表视图中设置TextView文本颜色会在滚动时为随机文本视图提供颜色吗?

时间:2013-02-05 15:20:49

标签: android listview

一切正常,直到我不滚动。只要我向下滚动其他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();
    }

2 个答案:

答案 0 :(得分:0)

您应该在onCreateView方法

中重置TextView颜色
tv1.setColor(Color.Black)

除了点击(它们将是黄色)之外,每个细胞都会变成黑色

这一切都发生了,因为ListView中的ceils被重用了。

UPD:

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


if (itemShouldBeYellow(position)){
    price.setColor(Color.Yellow); 
} else {
    price.setColor(Color.Black); 
}

答案 1 :(得分:0)

我认为你的问题可能就是这句话:

updated[count]=tv1;

您是否可以验证所有更改为黄色的文字视图是否已从更新后的数组中填充?