Listview中的Android EditText在滚动时更改值

时间:2013-12-04 12:07:30

标签: android listview scroll android-edittext

我尝试使用SQLite中的数据填充Custom ListView。当我尝试滚动ListView时,ListView的每一行中的EditText的值恢复为默认值。

我是java和android的新手,请帮帮我。

第一


TextView [0]


TextView [0]


TextView [0]


插入/滚动前


TextView [1]


TextView [3]


TextView [2]


滚动后


TextView [0]


TextView [0]


TextView [0]


这是我的适配器

public class MenuListAdapter extends BaseAdapter {

    Context context;

    ArrayList<Menu12> menuList;
    public MenuListAdapter(Context context, ArrayList<Menu12> list) {
        this.context = context;
        menuList = list;
    }

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

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

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

    class ViewHolder {
        EditText editText;
        ImageButton add;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {
        final Menu12 menu12 = menuList.get(position);

        final ViewHolder holder;

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.list_row, null);

            holder = new ViewHolder();
            holder.editText = (EditText) convertView.findViewById(R.id.value);
            convertView.setTag(holder);

        }else{
            holder = (ViewHolder) convertView.getTag();
        }

        final TextView Id = (TextView)convertView.findViewById(R.id.id); //id
        final TextView KdMenu = (TextView)convertView.findViewById(R.id.description); //KdMenu
        final TextView NmMenu = (TextView)convertView.findViewById(R.id.title); //NmMenu
        final TextView Harga = (TextView)convertView.findViewById(R.id.price); //Harga        
        final TextView KdCat = (TextView)convertView.findViewById(R.id.latitude); //KdCat
        final TextView SngktMenu = (TextView)convertView.findViewById(R.id.longitude); //SngktMenu 
        final TextView Aktif = (TextView)convertView.findViewById(R.id.aktif); //Aktif
        final TextView SoldOut = (TextView)convertView.findViewById(R.id.mart); //SoldOut
        final TextView noPrinter = (TextView)convertView.findViewById(R.id.noprint); //noPrinter

        TextView link_img = (TextView)convertView.findViewById(R.id.link_img);
        ImageView thumb_image =(ImageView)convertView.findViewById(R.id.img);

        Id.setText(String.valueOf(menu12.getID()));
        KdMenu.setText(menu12.getKDMENU());
        NmMenu.setText(menu12.getNMMENU());
        Harga.setText(menu12.getHARGA());
        KdCat.setText(menu12.getKDCAT());
        SngktMenu.setText(menu12.getSNGKTMENU());
        Aktif.setText(menu12.getAKTIF());
        SoldOut.setText(menu12.getSOLDOUT());
        noPrinter.setText(menu12.getNOPRINTER());
        holder.editText.setText(menu12.getJUMLAH());

        link_img.setText(menu12.getSOLDOUT());
        String img = link_img.getText().toString();

        if (img.toLowerCase().equals("t")){
            thumb_image.setImageResource(R.drawable.tidak_aktif);
        }else if(img.toLowerCase().equals("f")){
            thumb_image.setImageResource(R.drawable.aktif);
        }

        holder.editText.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) { 
                if(holder.editText.getText().toString().equals("")||holder.editText.getText().toString().equals("0")){
                }else{
                    MenuHandler db = new MenuHandler(context);
                    SQLiteDatabase dbsql = db.getWritableDatabase();
                    ContentValues values = new ContentValues();
                    values.put("jumlah", holder.editText.getText().toString());
                    dbsql.update("tb_menu", values, "id" + "= ?", new String[] {Id.getText().toString()});
                    dbsql.close();
                    db.close();
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
            }

            @Override
            public void afterTextChanged(Editable s) { 
            }

        });

        return convertView;
    }

}

0 个答案:

没有答案