如何设置从android中的数据库获取的文本视图中的值?

时间:2015-09-10 06:45:11

标签: java android

我根据Sqlite数据库中的位置存储值

我正在从数据库中检索值,并且值显示正确。但是当我在text-view中显示值时,它变为零。

请帮忙。

这是我的代码:

public class BabyWearAdapter extends BaseAdapter {

    ArrayList<String> p_id;
    ArrayList<String> p_name;
    Context context;
    ArrayList<String> imageId;
    ArrayList<String> wash_v;
    ArrayList<String> dry_v;
    ArrayList<String> iron_v;
    Typeface tf;
    ProgressDialog dialog;

    Intent i;
    float washItemCount = 0.0f;
    String iconName;
    Vector<BasketDO> vecBasket = new Vector<BasketDO>();
    int countVal1, countVal2, countVal3;
    public ImageLoader imageLoader;
    int KEY, pid;
    String TNAME;
    DBHandler dbHandler;
    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;

    HashMap<Integer, Integer> launDry = new HashMap<Integer, Integer>();
    HashMap<Integer, Integer> stremIron = new HashMap<Integer, Integer>();
    HashMap<Integer, Integer> dryCleans = new HashMap<Integer, Integer>();
    public static ArrayList<HashMap<Integer, Integer>> mListLaundry = new ArrayList<HashMap<Integer, Integer>>();

    public BabyWearAdapter(Context con, ArrayList<String> pid,
            ArrayList<String> pname, ArrayList<String> pimg,
            ArrayList<String> wash, ArrayList<String> dry,
            ArrayList<String> iron) {
        p_id = pid;
        context = con;
        p_name = pname;
        imageId = pimg;
        wash_v = wash;
        dry_v = dry;
        iron_v = iron;
        imageLoader = new ImageLoader(context);

        sharedPreferences = context.getSharedPreferences("prefName",
                context.MODE_APPEND);
        editor = sharedPreferences.edit();

    }

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

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

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



    @SuppressLint({ "UseValueOf", "ViewHolder" })
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.dynamic, parent, false);

        final LinearLayout ll1 = (LinearLayout) convertView
                .findViewById(R.id.ll1);
        final LinearLayout ll2 = (LinearLayout) convertView
                .findViewById(R.id.ll2);
        final LinearLayout ll3 = (LinearLayout) convertView
                .findViewById(R.id.ll3);
        final TextView tv1 = (TextView) convertView.findViewById(R.id.tv1);
        final TextView tv2 = (TextView) convertView.findViewById(R.id.tv3);
        final TextView tv3 = (TextView) convertView.findViewById(R.id.tv5);
        final ImageView img = (ImageView) convertView
                .findViewById(R.id.ivImage);

        tv1.setText(wash_v.get(position));
        tv2.setText(iron_v.get(position));
        tv3.setText(dry_v.get(position));
        imageLoader.DisplayImage(imageId.get(position), img);

        dbHandler = new DBHandler(context);

        int pid = Integer.parseInt(p_id.get(position));
        SQLiteDatabase db = null;
        db = dbHandler.getReadableDatabase();
        i = new Intent("com.example.safewash");
        final  TextView tvVal = (TextView) convertView.findViewById(R.id.tvVal);
        final TextView  tvVal1 = (TextView) convertView
                .findViewById(R.id.tvVal1);
        final  TextView tvVal2 = (TextView) convertView
                .findViewById(R.id.tvVal2);
        ll1.setTag(p_id.get(position));
        ll2.setTag(p_id.get(position));
        ll3.setTag(p_id.get(position));


        Cursor c = db.rawQuery("select * from babywear_tbl where pid =" + pid,
                null);

        if (c.moveToFirst()) {


                String countVal1 = c.getString(c.getColumnIndex("laundry_item"));
                String countVal2 = c.getString(c.getColumnIndex("dry_item"));
                String countVal3 = c.getString(c.getColumnIndex("iron_item"));

                final int count1 =Integer.parseInt(countVal1);
                final int count2 =Integer.parseInt(countVal2);
                final int count3 =Integer.parseInt(countVal3);

                tvVal.setText("" + count1);
                tvVal.setVisibility(View.VISIBLE);





                System.out.println("The Value one is "+countVal1);
                Log.i("Count Val One",""+countVal1);

        }
        try {

            int temp = launDry.get(Integer.parseInt(ll1.getTag().toString()
                    .trim()));
            int val = temp + 1;
            tvVal.setText("" + val);
            tvVal.setVisibility(View.VISIBLE);

        } catch (Exception e) {
            tvVal.setText("0");
        }

        try {

            int temp = stremIron.get(Integer.parseInt(ll2.getTag().toString()
                    .trim()));
            int val = temp + 1;
            tvVal1.setText("" + val);
            tvVal1.setVisibility(View.VISIBLE);

        } catch (Exception e) {
            tvVal1.setText("0");
        }

        try {

            int temp = dryCleans.get(Integer.parseInt(ll1.getTag().toString()
                    .trim()));
            int val = temp + 1;
            tvVal2.setText("" + val);
            tvVal2.setVisibility(View.VISIBLE);

        } catch (Exception e) {
            tvVal2.setText("0");
        }

        ll1.setOnClickListener(new OnClickListener() {

            @SuppressLint("NewApi")
            @Override
            public void onClick(final View v) {
                int count = 0;

                String washItem = wash_v.get(position);
                int washItems = Integer.parseInt(washItem);

                if (washItems != 0) {
                    int colorStart = Color.LTGRAY;
                    int colorEnd = 0;

                    ValueAnimator animator = ObjectAnimator.ofInt(v,
                            "backgroundColor", colorStart, colorEnd);
                    animator.setDuration(800);
                    animator.setEvaluator(new ArgbEvaluator());
                    animator.setRepeatCount(0);
                    animator.start();

                    washItemCount = Float.parseFloat(washItem);

                    tvVal.setVisibility(View.VISIBLE);

                    count = Integer.parseInt(tvVal.getText().toString());
                    countVal1 = ++count;

                    i.putExtra("washItemCount", washItemCount);
                    i.putExtra("countVal1", countVal1);
                    context.sendBroadcast(i);
                    try {

                        int temp = launDry.get(Integer.parseInt(ll1.getTag()
                                .toString().trim()));

                        temp++;
                        launDry.put(Integer.parseInt(ll1.getTag().toString()
                                .trim()), temp);

                        long id = dbHandler.insertBabyWearValues(Integer
                                .parseInt(ll1.getTag().toString().trim()),
                                countVal1, countVal2, countVal3);

                        if (id == -1) {
                            long updateid = dbHandler.updateBabyWear(countVal1,
                                    countVal2, countVal3);
                            Toast.makeText(context, "ID value" + updateid,
                                    Toast.LENGTH_SHORT).show();

                        }
                        Toast.makeText(context, "ID value" + id,
                                Toast.LENGTH_SHORT).show();

                        Appconstants.laundryHashmap = launDry;

                        int val = temp + 1;
                        tvVal.setText("" + val);
                    } catch (Exception e) {
                        launDry.put(Integer.parseInt(ll1.getTag().toString()
                                .trim()), 0);
                        tvVal.setText("" + 1);
                        e.printStackTrace();
                    }

                }
            }
        });

        ll2.setOnClickListener(new OnClickListener() {
            @SuppressLint("NewApi")
            @Override
            public void onClick(final View v) {
                int count = 0;
                String washItem = iron_v.get(position);

                washItemCount = Float.parseFloat(washItem);
                int washItems = Integer.parseInt(washItem);
                if (washItems != 0) {
                    int colorStart = Color.LTGRAY;
                    int colorEnd = 0;
                    ValueAnimator animator = ObjectAnimator.ofInt(v,
                            "backgroundColor", colorStart, colorEnd);
                    animator.setDuration(800);
                    animator.setEvaluator(new ArgbEvaluator());
                    animator.setRepeatCount(0);
                    animator.start();
                    washItemCount = Float.parseFloat(washItem);

                    tvVal1.setVisibility(View.VISIBLE);
                    count = Integer.parseInt(tvVal1.getText().toString());
                    countVal2 = ++count;
                    // tvVal1.setText("" + countVal2);
                    i.putExtra("washItemCount", washItemCount);
                    i.putExtra("countVal2", countVal2);
                    context.sendBroadcast(i);

                    try {

                        int temp = stremIron.get(Integer.parseInt(ll2.getTag()
                                .toString().trim()));
                        temp++;
                        stremIron.put(Integer.parseInt(ll2.getTag().toString()
                                .trim()), temp);
                        long id = dbHandler.insertBabyWearValues(Integer
                                .parseInt(ll2.getTag().toString().trim()),
                                countVal1, countVal2, countVal3);
                        Toast.makeText(context, "ID value" + id,
                                Toast.LENGTH_SHORT).show();
                        if (id == -1) {
                            long updateid = dbHandler.updateBabyWear(countVal1,
                                    countVal2, countVal3);
                            Toast.makeText(context, "ID value" + updateid,
                                    Toast.LENGTH_SHORT).show();

                        }
                        Appconstants.ironHashmap = stremIron;
                        int val = temp + 1;
                        tvVal1.setText("" + val);
                    } catch (Exception e) {
                        stremIron.put(Integer.parseInt(ll2.getTag().toString()
                                .trim()), 0);
                        tvVal1.setText("" + 1);
                        e.printStackTrace();
                    }

                }
            }
        });
        ll3.setOnClickListener(new OnClickListener() {

            @SuppressLint("NewApi")
            @Override
            public void onClick(final View v) {

                int count = 0;
                String washItem = dry_v.get(position);
                washItemCount = Float.parseFloat(washItem);
                int washItems = Integer.parseInt(washItem);
                if (washItems != 0) {
                    int colorStart = Color.LTGRAY;
                    int colorEnd = 0;

                    ValueAnimator animator = ObjectAnimator.ofInt(v,
                            "backgroundColor", colorStart, colorEnd);
                    animator.setDuration(800);
                    animator.setEvaluator(new ArgbEvaluator());
                    animator.setRepeatCount(0);
                    animator.start();

                    washItemCount = Float.parseFloat(washItem);

                    tvVal2.setVisibility(View.VISIBLE);
                    count = Integer.parseInt(tvVal2.getText().toString());
                    countVal3 = ++count;
                    // tvVal2.setText("" + countVal3);
                    i.putExtra("washItemCount", washItemCount);
                    i.putExtra("countVal3", countVal3);
                    context.sendBroadcast(i);
                    try {

                        int temp = dryCleans.get(Integer.parseInt(ll3.getTag()
                                .toString().trim()));
                        temp++;
                        dryCleans.put(Integer.parseInt(ll3.getTag().toString()
                                .trim()), temp);
                        long id = dbHandler.insertBabyWearValues(Integer
                                .parseInt(ll3.getTag().toString().trim()),
                                countVal1, countVal2, countVal3);
                        Toast.makeText(context, "ID value" + id,
                                Toast.LENGTH_SHORT).show();
                        if (id == -1) {
                            long updateid = dbHandler.updateBabyWear(countVal1,
                                    countVal2, countVal3);
                            Toast.makeText(context, "ID value" + updateid,
                                    Toast.LENGTH_SHORT).show();

                        }
                        Appconstants.dryCleanHashmap = dryCleans;
                        int val = temp + 1;
                        tvVal2.setText("" + val);
                    } catch (Exception e) {
                        dryCleans.put(Integer.parseInt(ll3.getTag().toString()
                                .trim()), 0);
                        tvVal2.setText("" + 1);
                        e.printStackTrace();

                    }
                }

            }
        });

        return convertView;
    }

}

关键是我写了这个逻辑适配器,即getView()。

1 个答案:

答案 0 :(得分:0)

这可能是因为您的值可能是十进制的,在将其解析为整数时,如果数字小于1,则转换为0,即0.88等

更改您的代码
String countVal1 = c.getString(c.getColumnIndex("laundry_item"));
String countVal2 = c.getString(c.getColumnIndex("dry_item"));
String countVal3 = c.getString(c.getColumnIndex("iron_item"));

int count2 =Integer.parseInt(countVal2);
int count3 =Integer.parseInt(countVal3);
int count1 =Integer.parseInt(countVal1);

tvVal.setText("" + count1);
tvVal.setVisibility(View.VISIBLE);

更改了代码

String countVal1 = c.getString(c.getColumnIndex("laundry_item"));
String countVal2 = c.getString(c.getColumnIndex("dry_item"));
String countVal3 = c.getString(c.getColumnIndex("iron_item"));

tvVal.setText(countVal1);
tvVal.setVisibility(View.VISIBLE);

您可以直接在textview中显示您的值,而无需将它们转换为整数,然后使用字符串添加进行显示。

如果有帮助,请检查并告诉我。

修改

检查完代码后,我发现以下行可能会在代码中产生问题,而代码会在文本视图中将您的值转换为零

try {

            int temp = launDry.get(Integer.parseInt(ll1.getTag().toString()
                    .trim()));
            int val = temp + 1;
            tvVal.setText("" + val);
            tvVal.setVisibility(View.VISIBLE);

        } catch (Exception e) {
            tvVal.setText("0"); // Here you are setting it to zero
        }

        try {

            int temp = stremIron.get(Integer.parseInt(ll2.getTag().toString()
                    .trim()));
            int val = temp + 1;
            tvVal1.setText("" + val);
            tvVal1.setVisibility(View.VISIBLE);

        } catch (Exception e) {
            tvVal1.setText("0"); // Here you are setting it to zero
        }

这些尝试块是一些抛出异常的方法,而异常又会导致文本视图的catch块值变为零。请检查您的try块代码,它们为什么抛出异常。

编辑2: 如果您的HashMaplaunDry”或“stremIron”不包含您通过Integer.parseInt(ll2.getTag().toString().trim())的密钥,则它将返回null,整数不能为null。我相信在这种情况下抛出异常。请再次检查。

编辑3: 按要求提供解决方案

try {

        String temp = stremIron.get(Integer.parseInt(ll2.getTag().toString()
                    .trim()));
        if(temp != null){
           int val = Integer.parseInt(temp) + 1;
           tvVal1.setText("" + val);
           tvVal1.setVisibility(View.VISIBLE);
        }
     } catch (Exception e) {
         tvVal1.setText("0"); // Here you are setting it to zero
     }