2016-02-11 30 views
0

我使用SQLITE数据库来存储列表项和Baseadapter在我的应用程序中显示列表视图。更改数据库值后,listview不更新baseadapter内部

列表视图中的每个项目都具有编辑文本值,默认值为1. 当用户将值从1更改为2时,该值必须在数据库中更新。

此更新必须在Adapter内完成。

下面是适配器代码,我在其中更改编辑文本值。

public class CustomAdapter_cart extends BaseAdapter { 
ArrayList<String> list_name = new ArrayList<String>(); 
ArrayList<String> list_price = new ArrayList<String>(); 
ArrayList<String> list_images = new ArrayList<String>(); 
ArrayList<String> list_model = new ArrayList<String>(); 
ArrayList<String> list_productid = new ArrayList<String>(); 
ArrayList<Integer> ids = new ArrayList<Integer>(); 
CustomAdapter_cart cart_refresh; 
Bitmap b; 

Context context; 
AddToCart cart; 
String value,name,price,image_new,model,product,qty; 
private static LayoutInflater inflater = null; 
Cursor cu; 
String quant; 
Holder holder = new Holder(); 
SharedPreferences sharedpreferences; 
ArrayList<String>listMessages = new ArrayList<String>(new LinkedHashSet<String>()); 

ArrayList<String> quant_items = new ArrayList<String>(); 
Cursor mCursor; 
ContentValues data=new ContentValues(); 
String model_item; 
String id; 
int id_final; 


public CustomAdapter_cart(Context context, ArrayList<String> list_name, ArrayList<String> list_price, ArrayList<String> bitmapArray, ArrayList<String> list_model, ArrayList<String> list_productid,ArrayList<String> qty, ArrayList<Integer>ids) { 
    this.context = context; 
    this.list_name = list_name; 
    this.list_price = list_price; 
    this.list_images = bitmapArray; 
    this.list_model = list_model; 
    this.list_productid = list_productid; 
    this.quant_items = qty; 
    this.cart_refresh = this; 
    this.ids = ids; 

    inflater = LayoutInflater.from(context); 
} 

@Override 
public int getCount() { 
    return list_name.size(); 
} 

@Override 
public Object getItem(int position) { 
    return position; 
} 

@Override 
public long getItemId(int position) { 
    return position; 
} 



public class Holder { 
    TextView tv_name, tv_price, tv_model,tv_product,tv_id; 
    ImageView image; 
    Button delete; 
    EditText quantity; 

} 


@Override 
public View getView(final int position, View convertView, final ViewGroup parent) { 

    View rowView = convertView; 

    if (convertView == null) { 

     // TODO Auto-generated method stub 
     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 


     rowView = inflater.inflate(R.layout.list_items_cart, null); 


     holder.tv_name = (TextView) rowView.findViewById(R.id.name_cart); 
     holder.tv_price = (TextView) rowView.findViewById(R.id.price_cart); 
     holder.image = (ImageView) rowView.findViewById(R.id.image_cart); 
     holder.tv_model = (TextView) rowView.findViewById(R.id.model_cart); 
     holder.tv_product = (TextView) rowView.findViewById(R.id.product_cart); 
     holder.delete = (Button) rowView.findViewById(R.id.delete); 
     holder.quantity = (EditText) rowView.findViewById(R.id.quantity); 
     holder.tv_id = (TextView)rowView.findViewById(R.id.ids); 
     rowView.setTag(holder); 



    } 
    else 

     holder = (Holder) rowView.getTag(); 

    holder.tv_name.setText(list_name.get(position)); 
    name = holder.tv_name.getText().toString(); 
    holder.tv_price.setText(list_price.get(position)); 
    price = holder.tv_price.getText().toString(); 
    holder.tv_model.setText(list_model.get(position)); 
    model = holder.tv_model.getText().toString(); 
    holder.tv_product.setText(list_productid.get(position)); 
    product = holder.tv_product.getText().toString(); 
    holder.quantity.setText(quant_items.get(position)); 

    quant = holder.quantity.getText().toString(); 
    holder.tv_id.setText(Integer.toString(ids.get(position))); 


    id = holder.tv_id.getText().toString(); 
    id_final = Integer.parseInt(id); 





    holder.image.setImageBitmap(loadImageFromStorage(list_images.get(position))); 
    image_new = holder.image.toString(); 


    final View finalRowView1 = rowView; 

      holder.quantity.addTextChangedListener(new TextWatcher() { 
       @Override 
       public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

       } 

       @Override 
       public void onTextChanged(CharSequence s, int start, int before, int count) { 



       } 

       @Override 
       public void afterTextChanged(Editable s) { 
        value = s.toString(); 
        quant.replace(quant, value); 
        updateTable(); 

        Toast.makeText(finalRowView1.getContext(), "Updating Table", Toast.LENGTH_SHORT).show(); 




       } 


      }); 




    final View finalRowView = rowView; 
    rowView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      String name_item = ((TextView) finalRowView.findViewById(R.id.name_cart)).getText().toString(); 
      String price_item = ((TextView) finalRowView.findViewById(R.id.price_cart)).getText().toString(); 
      model_item = ((TextView) finalRowView.findViewById(R.id.model_cart)).getText().toString(); 

      Intent in = new Intent(context, AddCart_FullImage.class); 
      in.putExtra("model", model_item); 
      in.putExtra("name", name_item); 
      in.putExtra("price", price_item); 
      context.startActivity(in); 
     } 
    }); 
    return rowView; 
} 




private Bitmap loadImageFromStorage(String path) { 

    try { 
     File f = new File(path, ""); 
     f.canRead(); 
     b = BitmapFactory.decodeStream(new FileInputStream(f)); 
     return b; 


    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 

    return null; 
} 


public void updateTable() { 
    final DatabaseHandler db = new DatabaseHandler(context); 

    SQLiteDatabase db1 = db.getWritableDatabase(); 
    try { 
     db.updateContact(new Cart(id_final,name, price, image_new, model, product, value)); 
     Log.v("LOG", "After text change value db " + value); 



    } catch (Exception e) { 

    } 



} 
} 

问题

当我尝试更新的编辑文本值,它在数据库被更新,但它再次更改为默认值“1”。

我不知道我哪里出错了。

任何帮助都会非常棒。

谢谢。

+0

你是什么意思与“但又改变为默认值”?你在谈论用户界面? – thetonrifles

+0

默认值是已经存储在数据库中的值。在我的情况下,它是1. – Ramz

+0

呃...所以我不明白...你的意思是说,数据库没有更新?对不起,如果我做了愚蠢的问题,但我不明白,如果你的问题涉及存储或用户界面。 – thetonrifles

回答

0

每次系统想要重新绘制ListView时,它都会调用适配器的getView(...)方法。以下行设置您的数量的EditText的值:

holder.quantity.setText(quant_items.get(position)); 

当用户更改的EditText值时,您执行以下操作:

quant.replace(quant, value); 
updateTable(); 

正如你所看到的,你的适配器的数据源(quant_items)没有更新。 由于您正在从适配器内更新数据库,因此您可以更新quant_items ArrayList,它应该可以做到这一点。

+0

嘿,我试过'quant_items.set(position,quant);''afterTextChanged'内''。但它不起作用。你能帮我解决这个问题吗? – Ramz

+0

嗨'quant_items.set(position,value);'发挥了诀窍。谢谢您的帮助。你节省了我的时间。 :) – Ramz

0

如果您更改其数据(并且需要使用新值更新list_name),则需要通知您的适配器。 通知您的适配器的最简单方法是在更新数据后使用notifyDatasetChanged()

相关问题