Android studio imageButton数据库

时间:2018-06-11 13:07:25

标签: java android-studio

我想使用产品的图像按钮从某人的帐户中扣除信用(即产品的费用),这样当他点击图像时,会从他的帐户中扣除信用。问题是我没有想到如何做到这一点,我已经尝试过使用AsyncTask,事情是,我按下了ImageButton,但没有任何反应,就像它甚至不知道我推了它一样。这是代码:

package com.example.battl.qr2;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.vision.barcode.Barcode;
import com.squareup.picasso.Picasso;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Profil extends AppCompatActivity  {

    boolean cof=false;
    TextView textView4;
    ImageView imageView;
    TextView textViewCredits;
    Button btnShop;
    ImageButton imageButton,imageButton2,imageButton3,imageButton4,imageButton5,imageButton7;
    private String credits = "50";
    String url="https://zippy.gfycat.com/LiquidBlindDorking.gif";

//出于安全原因,我用X替换了服务器的实际数据

    private static final String DB_URL= "jdbc:mysql://X";
    private static final String USER = "X";
    private static final String PASS = "X";


    public SharedPreferences settings;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        settings= PreferenceManager.getDefaultSharedPreferences(this);
        setContentView(R.layout.activity_profil);
        imageButton=findViewById(R.id.imageButton);
        imageButton2=findViewById(R.id.imageButton2);
        imageButton3=findViewById(R.id.imageButton3);
        imageButton4=findViewById(R.id.imageButton4);
        //imageButton5=findViewById(R.id.imageButton5);
        //imageButton7=findViewById(R.id.imageButton7);
        textViewCredits= findViewById(R.id.textViewCredits);
        imageView= findViewById(R.id.imageView);
        textView4= findViewById(R.id.textView4);
        btnShop=findViewById(R.id.btnShop);
        loadImageFromUrl(url);
        Picasso.get().load("http://makegoodcoffee.com/coffee-talk/wp-content/uploads/2009/01/espresso.jpg").into(imageButton);
        Picasso.get().load("http://makegoodcoffee.com/coffee-talk/wp-content/uploads/2009/01/espresso.jpg").into(imageButton2);
        Picasso.get().load("http://hurt2healingmag.com/wp-content/uploads/2013/08/coffee-2.jpg").into(imageButton3);
        Picasso.get().load("https://d3mrtwiv4dr09z.cloudfront.net/media/catalog/product/cache/2/image/600x/9df78eab33525d08d6e5fb8d27136e95/1/7/17BRD_BEV_23_EspressoMacchiato_FA_LARGE.png").into(imageButton4);
        //Picasso.get().load("https://www.metro.ro/~/media/RO-Metro/image/service-image/articole-utile/cafea-lapte.jpg?h=379&la=ro-RO&w=460").into(imageButton5);
       // Picasso.get().load("https://www.nespresso.com/ncp/res/uploads/recipes/1900px_Global_OL_ALL_Coffee%20Moment_Recipe_Latte%20Macchiato_2017_2019.jpg").into(imageButton7);
        //final Barcode barcode = getIntent().getParcelableExtra("barcode");

        Intent intent = getIntent();
        Bundle extras = intent.getExtras();


        String getEmail=settings.getString("EMAIL","");
        textView4.setText(getEmail);

        String getCredits=settings.getString("CREDITE","");
        textViewCredits.setText(getCredits);

        int costul=0;


       btnShop.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               System.out.println("ASTA MERGE");
                    Toast.makeText(Profil.this,"Asta e buton", Toast.LENGTH_LONG);
           }
       });

       imageButton.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               System.out.println("ASTA E IMAGINEA");
               cof=true;
           }
       });


    }

    public void btnConn(View view)
    {
        Toast.makeText(Profil.this,"Asta e butonu",Toast.LENGTH_LONG);
    }

    private void loadImageFromUrl(String url)
    {

        Picasso.get().load(url).into(imageView);
    }

    private class Send extends AsyncTask<String,String,String>
    {

        String Msg="";
        @Override
        protected  void onPreExecute() {}

        @Override
        protected String doInBackground(String... strings)
        {
            try
            {
                Class.forName("com.mysql.jdbc.Driver");
                Connection conn = DriverManager.getConnection(DB_URL,USER,PASS);
                if(conn==null){
                    Msg="Connection goes wrong";
                }else{
                    final String email=settings.getString("EMAIL","");
                    String query2 = "SELECT credite FROM reg WHERE email = '" + email + "'";
                    Statement stmt2 = conn.createStatement();
                    final ResultSet r2 = stmt2.executeQuery(query2);


                    r2.next();
                    final int   nrCred=r2.getInt("credite");

                    final String query3="UPDATE reg SET credite='" + (nrCred-10) + " WHERE email='" + email+ "'";
                    Statement stmt3 = conn.createStatement();
                    if(cof=true)
                    {
                        stmt3.executeUpdate(query3);
                                System.out.println("O mers coaiemio");
                    }






                /*    imageButton.setOnClickListener(new View.OnClickListener() {


                        @Override
                        public void onClick(View view) {

                            try {
                                String query30 = "UPDATE reg SET credite='" + (nrCred - costul) + " WHERE email='" + email + "'";
                                stmt3.executeUpdate(query30);
                                Toast.makeText(Profil.this,"Mere Boby",Toast.LENGTH_LONG).show();
                            } catch (SQLException e) {
                                e.printStackTrace();
                            }
                        }
                    });

                    imageButton2.setOnClickListener(new View.OnClickListener() {


                        @Override
                        public void onClick(View view) {
                            try {  String query32 = "UPDATE reg SET credite='" + (nrCred - costul) + " WHERE email='" + email + "'";
                                stmt3.executeUpdate(query32);
                            } catch (SQLException e) {
                                e.printStackTrace();
                            }
                        }
                    });

                    imageButton3.setOnClickListener(new View.OnClickListener() {


                        @Override
                        public void onClick(View view) {
                            try {
                                stmt3.executeUpdate(query3);
                            } catch (SQLException e) {
                                e.printStackTrace();
                            }
                        }
                    });

                    imageButton4.setOnClickListener(new View.OnClickListener() {


                        @Override
                        public void onClick(View view) {
                            try {
                                stmt3.executeUpdate(query3);
                            } catch (SQLException e) {
                                e.printStackTrace();
                            }
                        }
                    });


                    imageButton5.setOnClickListener(new View.OnClickListener() {


                        @Override
                        public void onClick(View view) {
                            try {
                                stmt3.executeUpdate(query3);
                            } catch (SQLException e) {
                                e.printStackTrace();
                            }
                        }
                    });

                    imageButton7.setOnClickListener(new View.OnClickListener() {


                        @Override
                        public void onClick(View view) {
                            try {
                                stmt3.executeUpdate(query3);
                            } catch (SQLException e) {
                                e.printStackTrace();
                            }
                        }
                    });*/
                }
                conn.close();


            }catch (Exception e)
            {
                Msg="Connection goes wrong 2";
                e.printStackTrace();
            }
            return Msg;
        }

        @Override
        protected void onPostExecute(String msg) {}
    }

}

0 个答案:

没有答案