jeudi 25 février 2016

Checkbox RecycleView SQLQuery

In my Android Studio project i would start an SQLQuery Activity by checking a Checkbox: this should send a message to my database (1) when i check the CheckBox, and should send another (0) when CheckBox is not checked. I'm new in the Android World so I can'understand how to improve the CheckBox in my RecycleView. I'll show you my main and my SQL Activity, hope you can help me.

FrammentoUno.java (My main activity)

public class FrammentoUno extends AppCompatActivity {


private RecyclerView GetAllAllarmiListView;
/**
 * ATTENTION: This was auto-generated to implement the App Indexing API.
 * See http://ift.tt/1Shh2Dk for more information.
 */
private GoogleApiClient mClient;
private Uri mUrl;
private String mTitle;
private String mDescription;

private Button frammentoUno, frammentoDue;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_one);

    addListenerOnButton();

    this.GetAllAllarmiListView = (RecyclerView) this.findViewById(R.id.GetAllAllarmiListView);

    new GetAllAllarmiTask().execute(new ApiConnector());

    mClient = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    mUrl = Uri.parse("android-app://com.example.andrea/http/host_path");
    mTitle = "Standard Poodle";
    mDescription = "The Standard Poodle stands at least 18 inches at the withers";



    LayoutInflater factory = LayoutInflater.from(this);
    final View textEntryView = factory.inflate(R.layout.get_all_allarmi_list_view_cell, null);

    final CheckBox visto = (CheckBox) textEntryView.findViewById(R.id.visto);
    visto.setOnClickListener(btnListener);

    setListAdapter(new JSONArray());


}


private View.OnClickListener btnListener = new View.OnClickListener() {
    @Override
    public void onClick(View visto) {
        if (visto.isClickable()) {
            startActivity(new Intent(FrammentoUno.this, SQL_visto.class));
            Toast.makeText(FrammentoUno.this,
                    "Visto", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(FrammentoUno.this,
                    "Non Visto", Toast.LENGTH_LONG).show();

        }
    }
};

public void addListenerOnButton() {

    frammentoUno = (Button) findViewById(R.id.frammentoUno);
    frammentoDue = (Button) findViewById(R.id.frammentoDue);

    frammentoUno.setOnClickListener(new View.OnClickListener() {

        //Run when button is clicked
        @Override
        public void onClick(View v) {
            Intent i = new Intent(FrammentoUno.this, FrammentoUno.class);

            startActivity(i);
            Toast.makeText(FrammentoUno.this, "Allarmi Generali",
                    Toast.LENGTH_LONG).show();

        }
    });

    frammentoDue.setOnClickListener(new View.OnClickListener() {
        //Run when button is clicked
        @Override
        public void onClick(View v) {
            Intent i = new Intent(FrammentoUno.this, FrammentoDue.class);

            startActivity(i);
            Toast.makeText(FrammentoUno.this, "Controllo Ultimi Allarmi",
                    Toast.LENGTH_LONG).show();

        }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    switch (id) {
        case R.id.MENU_1:
        /*
            Codice di gestione della voce MENU_1
         */
            startActivity(new Intent(this, ControlloSbarre.class));
            return true;

        case R.id.MENU_2:
             /*
            Codice di gestione della voce MENU_2
         */
            startActivity(new Intent(this, LoginActivity.class));
            return true;
    }
    return false;
}


public void setListAdapter(JSONArray jsonArray) {
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
    this.GetAllAllarmiListView.setLayoutManager(layoutManager);
    this.GetAllAllarmiListView.setAdapter(new GetAllAllarmiListViewAdapter(jsonArray, this));
}



private class GetAllAllarmiTask extends AsyncTask<ApiConnector, Long, JSONArray> {
    @Override
    protected JSONArray doInBackground(ApiConnector... params) {
        return params[0].GetAllAllarmi();
    }

    @Override
    protected void onPostExecute(JSONArray jsonArray) {
        setListAdapter(jsonArray);
    }
}}

SQL_visto.java

public class SQL_visto extends FrammentoUno {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    startService(new Intent(SQL_visto.this, BackgroundService.class));
}

public CheckBox visto;

public class SQLMain extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "web2park";

    static final String TABLE_NAME = "allarmi_ingressi";
    static final String COL_id_allarme = "id_allarme";
    static final String COL_id_park = "id_park";
    static final String COL_data_al = "data_al";
    static final String COL_stato = "stato";
    static final String COL_cod_allarme = "cod_allarme";
    static final String COL_descrizione = "descrizione";
    static final String COL_targa = "targa";
    static final String COL_visto = "visto";
    static final String COL_azione = "azione";
    static final String COL_varco = "varco";
    static final String COL_cassa = "cassa";

    private SQLiteDatabase database;

    public SQLMain (Context context) {
        super(context, DATABASE_NAME, null, 1);
        database = getWritableDatabase();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE allarmi_ingressi (id_allarme INTEGER PRIMARY KEY, id_park TEXT, data_al TEXT, stato TEXT, cod_allarme TEXT, descrizione TEXT, targa Text, visto TEXT, azione TEXT, varco TEXT, cassa TEXT );");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        android.util.Log.w(this.getClass().getName(),
                DATABASE_NAME + " database upgrade to version " + newVersion + " old data lost");
        db.execSQL("DROP TABLE IF EXISTS details");
        onCreate(db);
    }

    public final void addListenerOnVisto() {
        final CheckBox visto = new CheckBox(null);
        visto.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                if (((CheckBox) v).isChecked()) {
                    database.execSQL("UPDATE allarmi_ingressi SET visto = '1' WHERE visto = '0' ");
                    visto.setChecked(false);
                }}
        });
    }
}}

Other information: - I have an Adapter for my RecycleView, but i think CheckBox must be declared in main; - When I check CheckBox I get NullPointerException

Thanks for help.




Aucun commentaire:

Enregistrer un commentaire