Put your problem here if it does not fit any of the other categories.

Postby bluefloyd8 » Mon Jan 28, 2008 10:20 am

Sweet code. Im using it to make a list of checkbox items.
My problem is that I cannot figure out how to implement a "Select All" type function to make all the checkboxes either selected or unselected.

Heres my code snipets:

Main code:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  itla = new CheckBoxifiedTextListAdapter(this);
  2.  
  3.         itla.addItem(new CheckBoxifiedText("Surf Web", true));
  4.         itla.addItem(new CheckBoxifiedText("Blah Blah", false));
  5.         itla.addItem(new CheckBoxifiedText("Whoopy", true));
  6.         // Display it
  7.         setListAdapter(itla);
  8. ...
  9.  
  10.     private void selectAll()
  11.     {
  12.         // this function gets called when "select all" menu item is clicked. itla is defined above
  13.         // assigment of itla: itla = new CheckBoxifiedTextListAdapter(this);
  14.         for(int i = 0 ; i<itla.getCount(); i++)
  15.         {
  16.        
  17.                 itla.setChecked(true, i);
  18.         }
  19.     }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


CheckBoxifiedTextListAdapter.java:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.      public void setChecked(boolean value, int position)
  2.      {
  3.          Log.v("CHECKBOX", "CH1");
  4.          CheckBoxifiedText temp = mItems.get(position);
  5.          Log.v("CHECKBOX", "CH2");
  6.          temp.setChecked(value);
  7.          Log.v("CHECKBOX", "CH3");
  8.          return;
  9.      }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4

A null pointer exception is happening. CH1 and CH2 print to the log. CH3 does not.

CheckBoxifiedText.java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class CheckBoxifiedText implements Comparable<CheckBoxifiedText>{
  2.    
  3.      private String mText = "";
  4.      private CheckBox mCheckBox;
  5.      private boolean mSelectable = true;
  6.      
  7.      public CheckBoxifiedText(String text, boolean checked) {
  8.    
  9.        // DO I NEED TO MAKE mCheckBox point to the check box??????!!!?!?!
  10.    
  11.           mCheckBox = ????;
  12.           mText = text;
  13.      }
  14.      public void setChecked(boolean value)
  15.      {
  16.          //mCheckBox.setChecked(value);
  17.          getCheckBox().setChecked(value);
  18.      }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


I hope that is enough code to show what im trying to do. I included the parts i thought were important. CheckBoxified class is almost exact clone of Iconified.

I think the problem is with the line " mCheckBox = ????;" in the CheckBoxifiedText.java file. I never set up a pointer to the actual check box (perhaps???) so it has nothing to return when called upon in the code further up.
User avatar
bluefloyd8
Experienced Developer
Experienced Developer
 
Posts: 70
Joined: Tue Jan 22, 2008 3:57 am
Location: Indiana, USA

Top

Postby plusminus » Mon Jan 28, 2008 3:36 pm

Hello bluefloyd8,

where does the checkbox actually get created :?:
what does getCheckBox() return.

With this tutorial the actual Views get created within the IconifiedtextListAdapeter.getView(...) ...
Almost like this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. return new IconifiedTextView(mContext, mItems.get(position));
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


What I want to say is, that you do not need to handle a CheckBox (just the boolean) "down in your CheckBoxifiedText's".

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby bluefloyd8 » Mon Jan 28, 2008 8:44 pm

I am assuming that the checkbox gets created when the class is created:
private CheckBox mCheckBox;
Is that not when it happens?

getCheckBox() just returns a pointer to mCheckBox. It isnt a necessary function right now


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class CheckBoxifiedText implements Comparable<CheckBoxifiedText>{
  2.    
  3.      private String mText = "";
  4.      private CheckBox mCheckBox;
  5.      private boolean mSelectable = true;
  6.  
  7.      public CheckBoxifiedText(String text, boolean checked) {
  8.           mText = text;
  9.      }
  10.      public void setChecked(boolean value)
  11.      {
  12.          //mCheckBox.setChecked(value);
  13.          getCheckBox().setChecked(value);
  14.      }
  15.      public CheckBox getCheckBox() {
  16.           return mCheckBox;
  17.      }
  18.  
Parsed in 0.038 seconds, using GeSHi 1.0.8.4
User avatar
bluefloyd8
Experienced Developer
Experienced Developer
 
Posts: 70
Joined: Tue Jan 22, 2008 3:57 am
Location: Indiana, USA

Postby bluefloyd8 » Mon Jan 28, 2008 8:51 pm

Ok. im not sure where the checkbox is actually created versus where a pointer to it is created. Here is part of my CheckBoxifiedTextView.java file. Im new to OOP so i might not be understanding something here.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class CheckBoxifiedTextView extends LinearLayout {
  2.      
  3.      private TextView mText;
  4.      private CheckBox mCheckBox;
  5.      
  6.      public CheckBoxifiedTextView(Context context, CheckBoxifiedText aCheckBoxifiedText) {
  7.           super(context);
  8.  
  9.           this.setOrientation(HORIZONTAL);
  10.           Log.v("MY ACTIVITY", "About to create new checkbox");
  11.           mCheckBox = new CheckBox(context);
  12.           mCheckBox.setPadding(0, 0, 20, 0); // 5px to the right
  13.          
  14.           Log.v("MY ACTIVITY", "Checkbox: addview");
  15.           addView(mCheckBox,  new LinearLayout.LayoutParams(
  16.                     LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Parsed in 0.039 seconds, using GeSHi 1.0.8.4
User avatar
bluefloyd8
Experienced Developer
Experienced Developer
 
Posts: 70
Joined: Tue Jan 22, 2008 3:57 am
Location: Indiana, USA

Postby plusminus » Tue Jan 29, 2008 1:29 am

Hello bluefloyd8,

no the CheckBox doesn't get Constructed, just declared here:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. private CheckBox mCheckBox;
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


I would do it the same way as in the IconifiedText. Just store the Information not the View "down there" :!:
Just as you did it in your last post.

So the following probably solves your problem:
In CheckBoxifiedText:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.      public void setChecked(boolean value){
  2.  
  3.       this.checked = value;
  4.  
  5.      }
  6.  
  7.      public boolean getChecked(){
  8.  
  9.       return this.checked;
  10.  
  11.      }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


and in CheckboxifiedTextView:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. //....
  2.  
  3. mCheckBox = new CheckBox(context);
  4.  
  5. mCheckBox.setChecked(aCheckBoxifiedText.getChecked());
  6.  
  7. //....
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


Hope I could push you further :)

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby bluefloyd8 » Tue Jan 29, 2008 2:53 am

Unfortunately that did not work. If you dont mind, I will post all of my code so you can see what is happening.

There are 4 files. The first 3 are VERY similar to the IconifiedText class from the tutorial:
CheckBoxifiedText.java
CheckBoxifiedTextView.java
CheckBoxifiedTextViewAdapter.java

The last file (Activity Code Example.txt) is some sample code of the activity that is creating the checkbox list. In that file, you can see my selectAll() function.

Thanks. Once I get this finished and working, I will give everybody the code to use.
Attachments
CheckBoxifiedText.zip
(6.44 KiB) Downloaded 142 times
User avatar
bluefloyd8
Experienced Developer
Experienced Developer
 
Posts: 70
Joined: Tue Jan 22, 2008 3:57 am
Location: Indiana, USA

Top

Postby plusminus » Tue Jan 29, 2008 11:10 am

Hello bluefloyd,

So in CheckBocifiedTextListAdapter alter the getView(..)-function to :
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.      /** @param convertView The old view to overwrite, if one is passed
  2.       * @returns a CheckBoxifiedTextView that holds wraps around an CheckBoxifiedText */
  3.      public View getView(int position, View convertView, ViewGroup parent){
  4.           CheckBoxifiedTextView btv;
  5.           if (convertView == null) {
  6.                btv = new CheckBoxifiedTextView(mContext, mItems.get(position));
  7.           } else { // Reuse/Overwrite the View passed
  8.                // We are assuming(!) that it is castable!
  9.                CheckBoxifiedText src = mItems.get(position);
  10.                btv = (CheckBoxifiedTextView) convertView;
  11.                btv.setCheckBoxState(src.getChecked());
  12.                btv.setText(src.getText());
  13.                btv.setText(mItems.get(position).getText());
  14.           }
  15.           return btv;
  16.      }
Parsed in 0.039 seconds, using GeSHi 1.0.8.4

... and alter the setChecked(..)-function to :
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.      public void setChecked(boolean value, int position){
  2.          mItems.get(position).setChecked(value);
  3.      }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4

and also in CheckBocifiedTextListAdapter add this function for the selectAll() Functionality:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.      public void selectAll(){
  2.          for(CheckBoxifiedText cboxtxt: mItems)
  3.                  cboxtxt.setChecked(true);
  4.          /* Things have changed, do a redraw. */
  5.          this.notifyDataSetInvalidated();
  6.      }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


and finally in CheckBoxifiedText remove everything related to the private CheckBox mCheckBox;.

Hope I forgot no change I made. :)

Why don't you turn this into a kind of tutorial or sample-code. :)

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby bluefloyd8 » Wed Jan 30, 2008 5:08 am

plusminus, thanks for the help. Its working now!

I am going to spend a couple of days perfecting this class and getting all the functionality that I want from it. Once Ive got it done, I'll post a tutorial explaining it all.
User avatar
bluefloyd8
Experienced Developer
Experienced Developer
 
Posts: 70
Joined: Tue Jan 22, 2008 3:57 am
Location: Indiana, USA

Postby plusminus » Wed Jan 30, 2008 10:47 am

Hello bluefloyd8,

the community will thank you for it :)

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby Cybermat » Wed Feb 20, 2008 6:54 am

With the new sdk, the orange color covers up the icons and the text. Is there a way to fix that?

Thanks in advance.
Cybermat
Junior Developer
Junior Developer
 
Posts: 11
Joined: Mon Dec 03, 2007 5:42 am

Postby plusminus » Wed Feb 20, 2008 10:52 am

Not aware of a fix.

Perhaps apply the Theme.light . :roll:

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby Jesmiatka » Wed Feb 20, 2008 11:00 am

Cybermat wrote:With the new sdk, the orange color covers up the icons and the text. Is there a way to fix that?

Thanks in advance.


I think the selection is f**ked up at the moment.
A backgroundcolor of an item overrides the orange selection color as well...

@See:
custom_made_list_selection_color_-und-gt_list_row-t898.html
Jesmiatka
Senior Developer
Senior Developer
 
Posts: 164
Joined: Mon Feb 04, 2008 3:37 pm
Location: Netherlands

Postby bluefloyd8 » Wed Feb 20, 2008 5:59 pm

I just installed the M5 and am working on fixing some other problems right now. (Like they no longer support functions that were in M3). Once I get that taken care of, ill look at this problem and post a solution here.
User avatar
bluefloyd8
Experienced Developer
Experienced Developer
 
Posts: 70
Joined: Tue Jan 22, 2008 3:57 am
Location: Indiana, USA

Postby nithin.warier » Wed Apr 16, 2008 7:08 am

I am trying checkbox inside List, but getting null pointer exception,

Here is the code


import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.widget.CheckBox;
import android.content.Context;
import java.util.ArrayList;
import java.util.List;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CheckBoxifiedList extends ListActivity {

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
Context mContext = this;
CheckBox c1 = new CheckBox(mContext);
CheckBox c2 = new CheckBox(mContext);
CheckBox c3 = new CheckBox(mContext);
CheckBox c4 = new CheckBox(mContext);

CheckTextListAdapter btla = new CheckTextListAdapter(this);
CheckText bt1 = new CheckText("Google", c1);
CheckText bt2 = new CheckText("Mail", c2);
CheckText bt3 = new CheckText("Explorer", c3);
CheckText bt4 = new CheckText("Messenger", c4);
btla.addItem(bt1);
btla.addItem(bt2);
btla.addItem(bt3);
btla.addItem(bt4);
setListAdapter(btla);
}

class CheckText extends Object {

private String mText = "";
Context mContext = this.mContext;
CheckBox cBullet = new CheckBox(mContext);
private boolean mSelectable = true;

public CheckText(String text, CheckBox bullet) {
cBullet = bullet;
mText = text;

}

public boolean isSelectable() {
return mSelectable;
}

public void setSelectable(boolean selectable) {
mSelectable = selectable;
}

public String getText() {
return mText;
}

public void setText(String text) {
mText = text;
}

public void setBullet(CheckBox bullet) {
cBullet = bullet;
}

public CheckBox getBullet() {
return cBullet;
}
}

class CheckTextListAdapter extends BaseAdapter {

private Context mContext;
private List<CheckText> mItems;

public CheckTextListAdapter(Context context) {
mContext = context;
mItems = new ArrayList<CheckText>();
}

void addItem(CheckText bt) {
mItems.add(bt);

}

void setListItems(List<CheckText> bti) {
mItems = bti;
}

public int getCount() {
return mItems.size();
}

public Object getItem(int position) {
return mItems.get(position);
}

@Override
public boolean areAllItemsSelectable() {
return false;
}

@Override
public boolean isSelectable(int position) {
return mItems.get(position).isSelectable();
}

public long getItemId(int position) {
return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
CheckTextView btv;
if (convertView == null) {
btv = new CheckTextView(mContext, mItems.get(position).getText(),
mItems.get(position).getBullet());
} else {
btv = (CheckTextView) convertView;
btv.setText(mItems.get(position).getText());
btv.setBullet(mItems.get(position).getBullet());
}
return btv;
}
}

class CheckTextView extends LinearLayout {

private TextView mText;
private CheckBox mBullet;

public CheckTextView(Context context, String text, CheckBox bullet) {
super(context);
this.setOrientation(HORIZONTAL);
mBullet = new CheckBox(context);
mBullet = bullet;
addView(mBullet, new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
mText = new TextView(context);
mText.setText(text);
addView(mText, new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}

public void setText(String words) {
mText.setText(words);
}

public void setBullet(CheckBox c) {
mBullet = c;
}
}
}

help me,

thanks
Nithin
nithin.warier
Experienced Developer
Experienced Developer
 
Posts: 87
Joined: Thu Feb 28, 2008 12:05 pm
Location: Malappuram Kerala India

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Exabot [Bot] and 22 guests