簡體   English   中英

如何在android中從數據庫獲取的文本視圖中設置值?

[英]How to set the value in text view getting from database in android?

我根據 Sqlite 數據庫中的位置存儲值

我正在從數據庫中檢索值並且該值顯示正確。但是當我在文本視圖中顯示該值時它變為零。

請幫忙。

這是我的代碼:

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.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
        }

這些 try 塊是一些如何拋出異常,進而導致文本視圖的 catch 塊值變為零。 請檢查您的 try 塊代碼,為什么它們會拋出異常。

編輯 2:如果您的HashMaplaunDry ”或“ stremIron ”不包含您通過Integer.parseInt(ll2.getTag().toString().trim())傳遞的鍵,那么它將返回 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
     }

也許您正在從非 ui 線程設置文本字段的值。 如果您不在 UI 線程中設置 textField 的值,它將不會更改。 更改行:

   int count1 =Integer.parseInt(countVal1);
                tvVal.setText("" + count1);

 final int count1 =Integer.parseInt(countVal1);
        runOnUiThread(new new Runnable() {
                        public void run() {
                            tvVal.setText("" + count1);
                        }
                    });

或者,如果您無法訪問 runOnUiThread,則使用此 postToUI 方法:

import android.os.Handler;
import android.os.Looper;

public class UITools {

    public static void postToUI(Runnable r) {
        Handler handler = new Handler(Looper.getMainLooper());
        handler.post(r);
    }
}

像這樣嘗試您的查詢,看看輸出是什么:

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

這是一個像這樣的單引號pid='"+pid+"'",null);不要放任何空格

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM