mardi 31 mars 2015

Get Selected Items from ListView with CheckBox

Thank you all in advance


As they look at the code, I get the chechbox selected via onItemClick, and put it in a table (I keep the text)


I have several questions: 1) As I can do to give an id to each checkbox 2) What you want to do is: * When you run the app do a query on the database, I get the text and search the checkbox which coicidan with the supplied text and mark them as selected


Empresa1.java



package com.example.php_mysql_sqlite;


import java.util.ArrayList;
import java.util.HashMap;
import helper.CheckBoxClick;
import helper.SQLiteHandler;
import helper.SessionManager;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import app.Configuracion;


public class Empresa1 extends Activity {
private ListView listview;
private CheckBoxClick objCbc;
private SQLiteHandler sqlite;
private SessionManager session;
private Configuracion conf;
private ArrayAdapter<String> adapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_empresa1);

objCbc = new CheckBoxClick();
sqlite = new SQLiteHandler(getApplicationContext());
session = new SessionManager(getApplicationContext());
listview = (ListView)findViewById(R.id.listView1);
conf = new Configuracion(this);

//string array
String[] foody = {"pizza", "burger", "chocolate", "ice-cream", "banana", "apple","pizza", "burger", "chocolate", "ice-cream", "banana", "apple"};
// set adapter for listview
adapter = new ArrayAdapter<String>(this, R.layout.list_item, foody);

listview.setAdapter(adapter);
listview.setItemsCanFocus(false);
// we want multiple clicks
listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listview.setOnItemClickListener(objCbc);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.empresa1, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

public void regresar(View V){
Intent intent = new Intent(Empresa1.this, MainActivity.class);
startActivity(intent);
finish();
}

public void set_checekd()
{

}

public void guardar(View v){
HashMap<String, String> arrayUser = new HashMap<String, String>();
String cadena = "";

cadena = objCbc.getCadena_check();
Log.d("cadena",cadena);
String email = conf.getUserEmail();
sqlite.insertChbox(cadena, email);
arrayUser = sqlite.getUserDetails(email);

set_checekd();
}

}


CheckBoxClick.java



package helper;

import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CheckedTextView;

public class CheckBoxClick implements OnItemClickListener {

private String cadena_check = "";

public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
CheckedTextView ctv = (CheckedTextView) view;
if (ctv.isChecked()) {
cadena_check += ctv.getText() + "-";
} else {
Log.e("error", ctv.toString());
}
}

public String getCadena_check() {
return cadena_check;
}

public void setCadena_check(String cadena_check) {
this.cadena_check = cadena_check;
}

public void eraseCadena_check()
{
cadena_check="";
}

}


I have the method set_checekd () which is where the action would make touring the checkbox and mark them as selected


PS: Please if you give me negative points, leave a comment of that, as it has happened to me in the past, to know that I am wrong


Thanks to all





Aucun commentaire:

Enregistrer un commentaire