listview with edittext ,checkbox and textView in android - android

i have created a listview with checkbox,edittext and textview.
data is populated in listview with sql server. but i am not being able to use onCheckedChangedListener on checkbox. so that on clicking the checkbox corresponding data of textview and edittext is not being saved..
i know i am doing mistake somewhere in my adapter class..
How to save data and what logic should i use in onCheckedChangedListener in my adapter class?
code for pojo class
public class Model {
public String name="";
public boolean selected=false;
public String score="";
public Model(String name) {
this.name = name;
selected = false;
score="";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
public String getScore() {
return score;
}
public void setScore(String score) {
this.score = score;
}
}
code for Adapter class
public class MyAdapter extends ArrayAdapter<Model> {
private final List<Model> list;
private final Activity context;
public MyAdapter(Activity context, List<Model> list)
{
super(context, R.layout.row, list);
this.context = context;
this.list = list;
}
static class ViewHolder
{
protected TextView text;
protected CheckBox checkbox;
protected EditText scores;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View view = null;
if (convertView == null)
{
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.row, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) view.findViewById(R.id.label);
viewHolder.scores=(EditText) view.findViewById(R.id.txtAddress);
viewHolder.scores.addTextChangedListener(new TextWatcher()
{
public void onTextChanged(CharSequence s, int start, int before, int count) {
Model element=(Model)viewHolder.scores.getTag();
element.setScore(s.toString());
}
public void beforeTextChanged(CharSequence s, int start, int count,int after)
{
}
public void afterTextChanged(Editable s)
{
}
});
viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
Model element = (Model) viewHolder.checkbox.getTag();
element.setSelected(buttonView.isChecked());
}
});
viewHolder.checkbox.setTag(list.get(position));
viewHolder.scores.setTag(list.get(position));
view.setTag(viewHolder);
}
else
{
view = convertView;
((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
((ViewHolder) view.getTag()).scores.setTag(list.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.text.setText(list.get(position).getName());
holder.scores.setText(list.get(position).getScore());
holder.checkbox.setChecked(list.get(position).isSelected());
return view;
}
}
code for MainActivity class
public class MainActivity extends Activity {
ListView listView;
Button btnSave;
Connection connect;
ArrayAdapter<Model> adapter;
MyConnection mycon;
List<Model> list = new ArrayList<Model>();
List<String>data=new ArrayList<String>();
List<String>data2=new ArrayList<String>();
StringBuilder sb;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
mycon=new MyConnection(getApplicationContext());
listView = (ListView) findViewById(R.id.my_list);
btnSave = (Button)findViewById(R.id.btnSave);
sb=new StringBuilder();
adapter = new MyAdapter(this,getModel());
listView.setAdapter(adapter);
if(list.isEmpty()){
Toast.makeText(getApplicationContext(), "kuldeep", Toast.LENGTH_LONG).show();
}
btnSave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0; i < list.size(); i++) {
Toast.makeText(getBaseContext(), "Name : "+list.get(i).getName() +" Selected: "+list.get(i).isSelected()+"address: "+list.get(i).getScore().toString(), Toast.LENGTH_SHORT).show();
}
}
});
}
private List<Model> getModel() {
list.clear();
try{
Statement smt=mycon.connection().createStatement();
ResultSet rs=smt.executeQuery("WORKINGTYPE");
while(rs.next()){
list.add(new Model(rs.getString("FIELD_NAME")));
}
}catch(Exception e){
e.printStackTrace();
}
/* list.add(new Model("kuldeep"));
list.add(new Model("sandeep"));
list.add(new Model("ravi"));
list.add(new Model("pradeep"));
list.add(new Model("praveena"));
list.add(new Model("ruchi"));
list.add(new Model("vikas"));
list.add(new Model("sonu"));
list.add(new Model("ashu"));
*/
return list;
}
}
for saving data of textview and EditText what logic should i use and where in Adapter clss i should write it..

May not be a solution, but a suggestion.
Prefer not to declare your Listeners inside an 'if' condition. What I meant is,
IF convertview == null
find views
ELSE
getTag()
Rest of the codes

Is it that your checkbox event is not firing?
I use setOnClickListener for my Checkbox events:
CheckBox checkbox = (CheckBox)view.findViewById(R.id.item_checkbox);
checkbox.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Mark model as selected/not selected
if (model.isSelected())
model.markAsNotSelected();
else
model.markAsSelected();
}
});

I know it's an old question, but if anyone else is facing same problem, replace the Adapter class with the following code.
public class MyAdapter extends ArrayAdapter<Model> implements TextWatcher{
private final List<Model> list;
private final Context context;
public MyAdapter(Context context, List<Model> list)
{
super(context, R.layout.row, list);
this.context = context;
this.list = list;
}
static class ViewHolder
{
protected TextView text;
protected CheckBox checkbox;
protected EditText scores;
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder viewHolder = null;
if (convertView == null){
LayoutInflater inflator = LayoutInflater.from(context);
convertView = inflator.inflate(R.layout.row, null);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) view.findViewById(R.id.label);
viewHolder.scores=(EditText) view.findViewById(R.id.txtAddress);
viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
viewHolder.checkbox.setTag(list.get(position));
viewHolder.scores.setTag(list.get(position));
convertView.setTag(viewHolder);
}
else
{
convertView = (ViewHolder) convertView.getTag();
}
viewHolder.scores.addTextChangedListener(this)
viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
Model element = (Model) viewHolder.checkbox.getTag();
element.setSelected(buttonView.isChecked());
}
});
ViewHolder holder = (ViewHolder) view.getTag();
holder.text.setText(list.get(position).getName());
holder.scores.setText(list.get(position).getScore());
holder.checkbox.setChecked(list.get(position).isSelected());
return view;
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Model element=(Model)viewHolder.scores.getTag();
element.setScore(s.toString());
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,int after)
{
}
#Override
public void afterTextChanged(Editable s)
{
}
});
}

Related

Android - Spinner is not stable in custom ArrayAdapter

I have a custom array adapter with 2 textView, 1 spinner and 1 checkbox. When I run my app and select the item in the spinner and scroll the custom adapter the strings in spinner is not stable. It's changing its position in the customAdapter. The code for my getView(),
public class ListAdap extends ArrayAdapter<List> {
private static final String TAG = "ListAdap";
private final ArrayList<List> nameList;
public ListAdap(Context context, int resource, ArrayList<List> list) {
super(context, resource, list);
this.nameList = list;
}
public static class ViewHolder {
ArrayList excuseList;
TextView number;
Spinner excuse;
TextView name;
CheckBox isPaid;
ArrayAdapter adapter;
}
#Override
public View getView(final int position, #Nullable View convertView, #NonNull ViewGroup parent) {
ViewHolder viewHolder = null;
final List currentNameList = getItem(position);
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.pending_list, parent, false);
viewHolder = new ViewHolder();
final ArrayList excuseList = new ArrayList();
excuseList.add("");
excuseList.add("Tommorow");
excuseList.add("Sunday");
excuseList.add("Monday");
excuseList.add("Tuesday");
excuseList.add("Wednesday");
excuseList.add("Thursday");
excuseList.add("Friday");
excuseList.add("Next Week");
viewHolder.excuse = (Spinner) listItemView.findViewById(R.id.excuseSpinner);
ArrayAdapter adapter = new ArrayAdapter(getContext(), android.R.layout.simple_list_item_1, excuseList);
viewHolder.excuse.setAdapter(adapter);
final ViewHolder finalViewHolder = viewHolder;
viewHolder.excuse.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
currentNameList.get(position).setPos(String.valueOf(pos));
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
viewHolder.excuse.setSelection(Integer.parseInt(currentNameList.get(position).getPos()));
viewHolder.number = (TextView) listItemView.findViewById(R.id.number);
viewHolder.number.setText(currentNameList.getNumber());
viewHolder.name = (TextView) listItemView.findViewById(R.id.name);
viewHolder.name.setText(currentNameList.getName());
viewHolder.isPaid = (CheckBox) listItemView.findViewById(R.id.checkBox);
listItemView.setTag(viewHolder);
final ViewHolder finalViewHolder1 = viewHolder;
viewHolder.isPaid.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
List namelist = (List) cb.getTag();
if (String.valueOf(cb.isChecked()) == "true"){
Log.v("ConvertView", String.valueOf(cb.isChecked()) + finalViewHolder1.name.getText());
Toast.makeText(getContext(),finalViewHolder1.name.getText() +" Paid", Toast.LENGTH_SHORT).show();
cb.setChecked(true);
}
namelist.setSelected(cb.isChecked());
}
});
} else {
viewHolder = (ViewHolder) listItemView.getTag();
}
List list = nameList.get(position);
viewHolder.number.setText(currentNameList.number);
viewHolder.name.setText(currentNameList.name);
viewHolder.isPaid.setChecked(list.ispaid());
viewHolder.isPaid.setTag(list);
viewHolder.excuse.setTag(list);
return listItemView;
}
public class MultiSelectSpinner {
private String pos;
public String getPos()
{
return pos;
}
public void setPos(String pos)
{
this.pos = pos;
}
}
}
List.class
public class List{
String number;
String name;
Boolean paid = false;
public List(String number, String name, boolean paid) {
this.number = number;
this.name = name;
this.paid = paid;
}
public String getNumber() {
return number;
}
public String getName() {
return name;
}
public Boolean ispaid() {
return paid;
}
public void setSelected(boolean selected)
{
this.paid = selected;
}
}

How to get text in EditText with ListView and adapter

I am having problem in getting text from EditText in the listview adapter. For example, when I click "9", it doesn't give me 9. Instead, I always get 3. I am unable to figure the reason behind this. Please inform me where I am going wrong.
This is my code for your perusal. Thank you in advance!
class Planet {
String name;
int distance;
String quantità;
boolean selected = false;
public Planet(String name, int distance, String quantità) {
super();
this.name = name;
this.distance = distance;
this.quantità = quantità;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getDistance() {
return distance;
}
public void setDistance(int distance) {
this.distance = distance;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
public String getQuantità() {
return quantità;
}
public void setQuantità(String quantità) {
this.quantità = quantità;
}
}
public class PlanetAdapter extends ArrayAdapter<Planet>{
private List<Planet> planetList;
private Context context;
ArrayList<Planet> objects;
public PlanetAdapter(List<Planet> planetList, Context context) {
super(context, R.layout.single_listview_item, planetList);
this.planetList = planetList;
this.context = context;
}
public static class PlanetHolder {
public TextView planetName;
public TextView distView;
public CheckBox chkBox;
public EditText edit;
public String quantità="3";
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
final PlanetHolder holder = new PlanetHolder();
if(convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.single_listview_item, null);
holder.planetName = (TextView) v.findViewById(R.id.name);
holder.distView = (TextView) v.findViewById(R.id.dist);
holder.chkBox = (CheckBox) v.findViewById(R.id.chk_box);
holder.edit = (EditText) v.findViewById(R.id.editText);
holder.edit.setVisibility(View.GONE);
holder.edit.setEnabled(false);
holder.chkBox.setOnCheckedChangeListener((MainActivity) context);
//
holder.chkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (holder.chkBox.isChecked()) {
holder.edit.setVisibility(View.VISIBLE);
holder.edit.setEnabled(true);
holder.edit.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
holder.quantità = holder.edit.getText().toString();
System.out.println("leggo questo dalla edittext:"+ holder.edit.getText().toString());
}
#Override
public void afterTextChanged(Editable s) {
}
});
} else {
holder.edit.setVisibility(View.GONE);
holder.edit.setEnabled(false);
holder.edit.setText(null);
}
}
});
} else {
}
Planet p = planetList.get(position);
p.setQuantità(holder.quantità);
holder.planetName.setText(p.getName());
holder.distView.setText("" + p.getDistance());
holder.chkBox.setChecked(p.isSelected());
holder.chkBox.setTag(p);
holder.edit.setEnabled(false);
return v;
}
ArrayList<Planet> getBox() {
ArrayList<Planet> box = new ArrayList<Planet>();
for (Planet p : planetList) {
if (p.selected)
box.add(p);
}
return box;
}
}
Replace your getView() method with this:
View row = convertView;
PlanetHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(R.layout.single_listview_item, parent, false);
holder = new PlanetHolder();
holder.planetName = (TextView) row.findViewById(R.id.name);
holder.distView = (TextView) row.findViewById(R.id.dist);
holder.chkBox = (CheckBox) row.findViewById(R.id.chk_box);
holder.edit = (EditText) row.findViewById(R.id.editText);
row.setTag(holder);
} else {
holder = (PlanetHolder) row.getTag();
}
final Planet p = planetList.get(position);
holder.chkBox.setOnCheckedChangeListener((MainActivity) context);
holder.chkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (holder.chkBox.isChecked()) {
holder.edit.setVisibility(View.VISIBLE);
holder.edit.setEnabled(true);
holder.edit.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
p.setQuantità(holder.edit.getText().toString().trim());
}
});
} else {
holder.edit.setVisibility(View.GONE);
holder.edit.setEnabled(false);
holder.edit.setText(null);
}
}
});
holder.planetName.setText(p.getName());
holder.distView.setText("" + p.getDistance());
holder.chkBox.setChecked(p.isSelected());
holder.chkBox.setTag(p);
holder.edit.setEnabled(false);
return row;
You can get text of your listview by this way, Use this in your activity or fragment. Here items is an arraylist.
for (int i = 0; i < items.size(); i++) {
View wantedView = recyclerView.getChildAt(i);
EditText editqty = (EditText) wantedView.findViewById(R.id.edtTxtQty);
if ((editqty.getText().toString().length() > 0)) {
//store edit text value in any arraylist or array by editqty.getText().toString()
}
}
You have fix value of PlanetHolder class so you always get value '3'
public static class PlanetHolder {
public TextView planetName;
public TextView distView;
public CheckBox chkBox;
public EditText edit;
public String quantità="3";
}
remove public String quantitàfix value.
and Second is you have declare View v = convertView; but in if condition you can check this if(convertView == null) { so replace convertView to v

Android Listview items repeating

My listview items keep on repeating every time the page loads. I think there is an error in my adapter class. Please help. Thank you in advance.
public class MyAdapter extends ArrayAdapter<Model> {
private final List<Model> list;
private final Activity context;
boolean checkAll_flag = false;
boolean checkItem_flag = false;
public MyAdapter(Activity context, List<Model> list) {
super(context, R.layout.rows, list);
this.context = context;
this.list = list;
}
static class ViewHolder {
protected TextView text;
protected CheckBox checkbox;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
convertView = inflator.inflate(R.layout.rows, null);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) convertView.findViewById(R.id.label);
viewHolder.checkbox = (CheckBox) convertView.findViewById(R.id.check);
viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag(); // Here we get the position that we have set for the checkbox using setTag.
list.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
}
});
convertView.setTag(viewHolder);
//convertView.setTag(R.id.label, viewHolder.text);
//convertView.setTag(R.id.check, viewHolder.checkbox);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.checkbox.setTag(position); // This line is important.
viewHolder.text.setText(list.get(position).getName());
viewHolder.checkbox.setChecked(list.get(position).isSelected());
return convertView;
}
}
I am filling the list with this function which i am calling in my fragment's onCreate method
private List getModel() {
list.clear();
for(int i=0;i<list1.size();i++){
String a=list1.get(i);
list.add(new Model(a));
}
return list;
}
This is my fragment class
public class VaccAdmin extends FragmentActivity implements OnItemClickListener{
private Database dbHelper;
private double x,y;
ListView listView;
ArrayAdapter<Model> adapter;
List<Model> list = new ArrayList<Model>();
List<String> list1=new ArrayList<String>();
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.list);
dbHelper=new Database(this);
dbHelper.addVaccine();
listView = (ListView) findViewById(R.id.my_list);
String age = getIntent().getStringExtra("age");
System.out.println(age+"Ageinmo");
double ageinmo=Double.parseDouble(age);
if(ageinmo<=1.5){
x=0.0;
y=1.5;
} else if(ageinmo<=2.25){
x=1.75;
y=2.25;
} else if(ageinmo<=4.25){
x=3.5;
y=4.25;
}else if(ageinmo<=6.0){
x=4.5;
y=6.0;
}else if(ageinmo<=9.0){
x=6.25;
y=9.0;
}else if(ageinmo<=12.0){
x=9.25;
y=12.0;
}else if(ageinmo<=15.0){
x=12.25;
y=15.0;
}else if(ageinmo<=18.0){
x=15.25;
y=18.0;
}else if(ageinmo<=23.0){
x=18.25;
y=23.0;
}else if(ageinmo<=72.0){
x=36.25;
y=72.0;
}else if(ageinmo<=120.0){
x=72.25;
y=120.0;
}else if(ageinmo<=144.0){
x=120.25;
y=144.0;
}else if(ageinmo<=216.0){
x=144.25;
y=216.0;
}
list1=dbHelper.getVacNames(x, y);
int size=list1.size();
System.out.println("Size"+size);
adapter = new MyAdapter(this,getModel());
listView.setAdapter(null);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
TextView label = (TextView) v.getTag(R.id.label);
CheckBox checkbox = (CheckBox) v.getTag(R.id.check);
Toast.makeText(v.getContext(), label.getText().toString()+" "+isCheckedOrNot(checkbox), Toast.LENGTH_LONG).show();
}
private String isCheckedOrNot(CheckBox checkbox) {
if(checkbox.isChecked())
return "is checked";
else
return "is not checked";
}
private List<Model> getModel() {
list.clear();
for(int i=0;i<list1.size();i++){
String a=list1.get(i);
list.add(new Model(a));
}
return list;
}
}
I believe this should be written outide convertview==null
viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag(); // Here we get the position that we have set for the checkbox using setTag.
list.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
}
});

implementation of Checkbox in Custom ListActivity

In the following code
here is my main Activity Where i choose various product and proceed further
but when I checked multiple or one it pass 0 value;
that is in Toast I am not getting anything as below in image
public class MainActivity extends ListActivity {
ListView list;
Button btn1;
String url="";
private ArrayList <Product> allProducts = new ArrayList<Product>();
private ProductAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
btn1 = (Button) findViewById(R.id.btn);
list = getListView();
url="http://192.168.1.100/test/product.txt?id=";//+d.getInt("id");
try{
ConnectivityManager c =(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo n =c.getActiveNetworkInfo();
if (n!= null && n.isConnected()){
Log.d("url*********",url);
new Background().execute(url);
}
}catch(Exception e){}
adapter = new ProductAdapter(this,allProducts);
setListAdapter(adapter);
getListView().setItemsCanFocus(false);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
StringBuffer responseText = new StringBuffer();
responseText.append("The following were selected...\n");
for(int i=0;i<allProducts.size();i++){
Product product = allProducts.get(i);
if(product.isChecked()){
responseText.append("\n" + product.getProduct_name());
}
}
Toast.makeText(getApplicationContext(),
responseText, Toast.LENGTH_LONG).show();
}
});
}
Here is ProductAdapter
public class ProductAdapter extends ArrayAdapter<Product> {
ArrayList<Product> allProducts;
LayoutInflater vi;
int Resource;
Context context;
public ProductAdapter (Context context ,ArrayList<Product> objects)
{
super(context, R.layout.productrow, objects);
allProducts = objects;
this.context = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
convertView = inflater.inflate(R.layout.productrow, parent, false);
TextView name = (TextView) convertView.findViewById(R.id.price);
CheckBox cb = (CheckBox) convertView.findViewById(R.id.cb1);
int s = allProducts.get(position).getProduct_price();
name.setText(Integer.toString(s));
cb.setText(allProducts.get(position).getProduct_name());
if(allProducts.get(position).isChecked())
cb.setChecked(true);
else
cb.setChecked(false);
return convertView;
}
}
My Product.java file which is my model
public class Product {
int product_id;
private boolean checked = false ;
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
String product_name;
int product_price;
int product_qunatity;
int hotel_id;
public int getProduct_id() {
return product_id;
}
public void setProduct_id(int product_id) {
this.product_id = product_id;
}
public String getProduct_name() {
return product_name;
}
public void setProduct_name(String product_name) {
this.product_name = product_name;
}
public int getProduct_price() {
return product_price;
}
public void setProduct_price(int product_price) {
this.product_price = product_price;
}
public int getProduct_qunatity() {
return product_qunatity;
}
public void setProduct_qunatity(int product_qunatity) {
this.product_qunatity = product_qunatity;
}
public int getHotel_id() {
return hotel_id;
}
public void setHotel_id(int hotel_id) {
this.hotel_id = hotel_id;
}
}
Img Of screen Shot
Ok, there is declaration of checkbox CheckBox cb = (CheckBox) convertView.findViewById(R.id.cb1); but where is the listener?
I assume you need to add listener:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//your code goes here
cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.i(TAG, "Entered onCheckedChanged()");
//other stuff for listener
}
});
}

ListView Position on button click

I am android developer, I have a custom listview with a checkbox. This layput also contain a delete button. I want when I click on cheakbox all the item in a particular row is selected and on click of delete it is deleted.
The problem is when I click on delete button I get a list of +1 row value.
Initially I already define :
int position=0;
btmsgdelete.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("request send for message delete");
for(Message msg:almsg) {
if(msg.isSelected()) {
CheckBox chk = (CheckBox)findViewById(R.id.checkBox1);
System.out.println("msg is selected");
msgid=almsg.get(position).getEmpid();
System.out.println(msgid);
empname=almsg.get(position).getEmpname();
System.out.println(empname);
msgheader=almsg.get(position).getHeader();
System.out.println(msgheader);
}
}
Try this sample code
public class MainActivity extends Activity {
private int textViewResourceId;
private ArrayList<CompareListData> searchResults;
private ListView lst;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
searchResults = GetSearchResults();
lst = (ListView) findViewById(R.id.list);
findViewById(R.id.delete).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0; i < searchResults.size(); i++) {
if (searchResults.get(i).getSelected()) {
searchResults.remove(i);
}
}
lst.setAdapter(new Adapter(MainActivity.this, textViewResourceId,
searchResults));
}
});
System.out.println("size " + searchResults.size());
lst.setAdapter(new Adapter(MainActivity.this, textViewResourceId,
searchResults));
}
private ArrayList<CompareListData> GetSearchResults() {
ArrayList<CompareListData> results = new ArrayList<CompareListData>();
CompareListData sr1 = new CompareListData();
sr1.setName("John Smith");
results.add(sr1);
sr1 = new CompareListData();
sr1.setName("Jane Doe");
results.add(sr1);
sr1 = new CompareListData();
sr1.setName("Steve Young");
results.add(sr1);
sr1 = new CompareListData();
sr1.setName("Fred Jones");
results.add(sr1);
return results;
}
}
CompareListData.java
public class CompareListData {
String name;
boolean selected;
public String getName() {
return name;
}
public void setName(String Name) {
name = Name;
}
public boolean getSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
}
Adapter.java
public class Adapter extends BaseAdapter{
public static int count = 0;
public LayoutInflater inflater;
public static ArrayList<CompareListData> selectedId;
public ArrayList<CompareListData> listObjects;
Context contex;
public Adapter(Context context, int textViewResourceId,
ArrayList<CompareListData> objects) {
super();
this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.listObjects = objects;
this.contex = context;
}
public static class ViewHolder
{
TextView txtViewLoanName;
CheckBox chkSelected;
}
public View getView(final int position, View convertView, ViewGroup parent) {
View view = null;
if(convertView==null)
{
final ViewHolder holder = new ViewHolder();
view = inflater.inflate(R.layout.row_comparelist, null);
holder.txtViewLoanName= (TextView) view.findViewById(R.id.rowcomparelist_tv_loanname);
holder.chkSelected= (CheckBox) view.findViewById(R.id.rowcomparelist_chk_selected);
holder.chkSelected.setId(position);
holder.chkSelected.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
CompareListData element = (CompareListData) holder.chkSelected.getTag();
element.setSelected(buttonView.isChecked());
}
});
view.setTag(holder);
holder.chkSelected.setTag(listObjects.get(position));
}
else{
view = convertView;
((ViewHolder) view.getTag()).chkSelected.setTag(listObjects.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.txtViewLoanName.setText(listObjects.get(position).getName());
holder.chkSelected.setChecked(listObjects.get(position).getSelected());
return view;
}
public int getCount() {
return listObjects.size();
}
public Object getItem(int position) {
return listObjects.get(position);
}
public long getItemId(int position) {return position;
}
}

Categories

Resources