Hide specific Childen in ExpandableListView - hide

I have 3 kinds of children in my expandableListview.
For simplicity I want to let the user choose which one is visible and which is not.
public void setMenuItem(MenuItem item){
if(item.isChecked())
item.setChecked(false);
else item.setChecked(true);
switch(item.getItemId()){
case R.id.type0:
if(item.isChecked())adapter.type0=View.VISIBLE;
else adapter.type0=View.GONE; break;
case R.id.type1:
if(item.isChecked())adapter.type1=View.VISIBLE;
else adapter.type1=View.GONE; break;
case R.id.type2:
if(item.isChecked())adapter.type2=View.VISIBLE;
else adapter.type2=View.GONE;
break;
}
adapter.notifyDataSetChanged();
}
The getView in the expandable List custom adapter looks like this:
#Override
public View getChildView(int groupPosition, final int childPosition, boolean
isLastChild, View view, ViewGroup parent) {
ArrayList<Exercises> child = (ArrayList<Exercises>)childtems
.get(groupPosition);
if (view == null) {
LayoutInflater infalInflater = (LayoutInflater)context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
view = infalInflater.inflate(R.layout.expandchild, null);
}
TextView tv = (TextView) view.findViewById(R.id.expandChildText);
tv.setText(child.get(childPosition).toString());
switch(child.get(childPosition).type){
case 0 : view.setVisibility(type0);break;
case 1 : view.setVisibility(type1);break;
case 2 : view.setVisibility(type2);break;
}
return view;
}
What happens is, expandable List view shows still all children, whether visibility is set to .gone or not!
If it's gone, it just shows an empty child, without any textview etc, but I want to hide the child completely!
What's the trick there?
One Solution would be to remove specific data from adapter of course, but I think it would be like taking a sledgehammer to crack a nut...

Related

how to keep state when on/off in switch button below images

http://s107.photobucket.com/user/nguyenduydat274/media/onoff.png.html
On into Off: keep state (on/off) and disable listview
Off into On : keep sate (on/off) and enable lliseview.
I'll add small code from what I have understood. The idea is when you turn off / on the 'all saved movies' button, the list view should get enabled and disabled and also maintain the state of the particular movie.
public class UserSettingsMoviesAdapter extends BaseAdapter {
// Stores if the list is enabled or disabled. You need to update this
// value when the 'All saved movies' button is turned on or off.
private Boolean isListEnabled;
// This list will store if the movie is checked.
private List < Boolean > mArrChecked;
#Override
public boolean isEnabled(int position) {
if (isListEnabled) {
return true;
} else {
return false;
}
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
...
boolean isChecked = mArrChecked.get(position);
swOnOff.setChecked(isChecked);
swOnOff.setOnCheckedChangeListener(new OnCheckedChangeListener() {#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mArrChecked.set(currentPos, isChecked);
notifyDataSetChanged();
}
});
...
}
}
Now, to enable or disable the whole list you'll need to change the value of isListEnabled variable. If it returns false, isEnabled() will return false for each item and all items will be disabled by the list view but the state will remain. Make sure you call notifyDataSetChanged() after updating the value of isListEnabled.

Need to set additional information in list view

I am trying to created a listview containing filenames. I want to set a additional information like file id with each list items, so when i click a filename, i have to get file id from it. please help me do this.
My sample code:
ListView listview = (ListView) findViewById(R.id.listview);
ArrayAdapter fileListAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, ['one','two','three']);
listview.setAdapter(fileListAdapter);
There are quite a few things involved here, so i'm providing you with an example of how you can achieve this (you can copy-paste and test):
public class MainActivity extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// First we simulate a pool of file names and IDs
String[] fileNames = {"fileName1", "fileName2", "fileName3"};
List<Integer> fileNameIds = new ArrayList<Integer>();
fileNameIds.add(1200);
fileNameIds.add(356);
fileNameIds.add(28);
// We call our custom Adapter
ArrayAdapter<String> adapter = new CustomAdapter(this, fileNames, fileNameIds);
// Finally we set the adapter to our list
setListAdapter(adapter);
}
// This is a custom adapter that uses ArrayAdapter for our purpose
// (as this is just an example you should consider using Base Adapter if you don't want
// to have a pool of filenames and a separate pool of ids)
class CustomAdapter extends ArrayAdapter<String>{
private final LayoutInflater INFLATER;
private final String[] FILE_NAMES;
private final List<Integer> FILE_NAME_IDS;
public CustomAdapter(Context context, String[] fileNames, List<Integer> fileNameIds) {
super(context, R.layout.custom_row, fileNames);
this.INFLATER = LayoutInflater.from(context);
this.FILE_NAMES = fileNames;
this.FILE_NAME_IDS = fileNameIds;
}
// HERE is where you can assign effectively an ID to your rows
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// this is an object that takes advantage of the holder pattern
// it retains the state of our rows in the list
FileNameHolder holder;
if(convertView == null){
convertView = INFLATER.inflate(R.layout.custom_row, null); // inflate your custom row
// now you need to assign specific identifier to the list row that the holder will retain
// for you, so you can always get this id by calling getTag from the View object on your
// item click listeners
holder = new FileNameHolder();
holder.fileName = (TextView) convertView; //since i only have a texView in layout i don't need to call findByView
convertView.setTag(holder); // relate the view to a custom FileNameHolder object that retains file name and its ID
} else{
holder = (FileNameHolder) convertView.getTag();
}
holder.fileName.setText(FILE_NAMES[position]); // PROVIDE the list with file name description
holder.idFileName = FILE_NAME_IDS.get(position); // ASSIGN file name ID
return convertView;
}
}
// This is an example of catching a row clicked and get the custom ID that you assigned,
// from here you can use that ID as you need
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// Here as you can see we obtain the object associated with the row that was clicked
FileNameHolder holder = (FileNameHolder) v.getTag();
// Here i provide a way you can test that you're always getting the correct file name and Id
Toast.makeText(this,
"File Name = " + holder.fileName.getText() +
", File ID = " + holder.idFileName,
Toast.LENGTH_SHORT).show();
}
// This is a class that takes advantage of the Holder Pattern and we use it to
// achieve what you need (remember this is just an example you should consider
// changing class and member access modifiers as you need)
class FileNameHolder{
Integer idFileName;
TextView fileName;
FileNameHolder() {
}
}
}
custom_row.xml is just a TextView (i took it from the simple_list_item_1 layout):
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:paddingStart="?android:attr/expandableListPreferredItemPaddingLeft"
android:textAppearance="?android:attr/textAppearanceMedium"
android:gravity="center_vertical"
/>
Hope it's useful... regards!!

How to work with ids and strings in pagerTabStrip

Super newbie question about accessing information in strings. I have chunked together an app that uses a fragmentpager and the PagerTabStrip.
It is supposed to display titles on the tab, but mine displays nothing...because I am a total newbie hacking his way through. I am so grateful for this community.
From my layout file that calls the content (and once I fix it the title--id#pager_header right?):
<android.support.v4.view.PagerTabStrip
android:id="#+id/pager_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:textColor="#ffffff" />
My code that gets me the swipeable pages of content:
public class Poems extends FragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contentpage);
/** Getting a reference to the ViewPager defined the layout file */
ViewPager pager = (ViewPager) findViewById(R.id.pager);
/** Getting fragment manager */
FragmentManager fm = getSupportFragmentManager();
/** Instantiating FragmentPagerAdapter */
MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(fm);
/** Setting the pagerAdapter to the pager object */
pager.setAdapter(pagerAdapter);
}
}
My code that should get me the titles from the string reference file.
public class MyFragment extends Fragment{
int mCurrentPage;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/** Getting the arguments to the Bundle object */
Bundle data = getArguments();
/** Getting integer data of the key current_page from the bundle */
mCurrentPage = data.getInt("current_page", 0);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.myfragment_layout, container,false);
TextView tv = (TextView ) v.findViewById(R.id.tv);
tv.setMovementMethod(new ScrollingMovementMethod());
switch(mCurrentPage){
case 0:
tv.setText(" ");
break;
case 1:
tv.setText(R.string.content_1);
break;
case 2:
tv.setText(R.string.content_2);
break;
case 3:
tv.setText(R.string.content_3);
break;
}
return tv;
}
}
How do I get pager_header from this?
A sample relevant string is:
<string name="content_1">Welcome to this app!</string>
I am thinking that I need to better understand strings since I'm coming from more of a text content background.
How do I include in the stings.xml file the indicators that allow the app to reference the content associated with pageview so that I can have that show up in the tab? How I do make it so that the content shows "Welcome to this app" but the title in the tab shows "Home"?
I think as if each entry (page) is like a line in a database with one point of reference being the title, one being the content, one being a link or some other content. Each has its own id, right?
Thank you in advance for explaining this to a beginner.
You need to implement getPageTitle(int position) in MyFragmentPagerAdapter. The String that is returned from this method is used to set the title of the PagerTabStrip.
#Override
public CharSequence getPageTitle(int position) {
Log.v(TAG, "getPageTitle");
String tabTitle;
switch(position) {
case 0:
tabTitle = "Tab #0";
break;
case 1:
tabTitle = "Home";
break;
case 2:
tabTitle = "Tab #2";
break;
case 3:
tabTitle = "Tab #3";
break;
default:
tabTitle = "Default Tab Title";
break;
}
return tabTitle;
}
You can make this more dynamic by making the title and content arguments that you pass in using a Bundle by calling setArguments(Bundle args) on your MyFragments that you add to your ViewPager. In MyFragment in onCreateView() you can then set your content based on the passed in content String argument. Then in MyFragmentPagerAdapter you can get a reference to the Fragment at the position and pull get the title directly from MyFragment.

ListView clipping

i working little bit with the ListView from JavaFx2. I´m running into one issue.
Is it possible to turn off the clipping of the ListCell/ListView?
I add an ImageView that has to be wider than the ListView and JavaFx2 shows automatically a scrollbar.
This my code snipped how i add the ImageView to my List:
list.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {
#Override
public ListCell<String> call(ListView<String> param) {
final ListCell<String> blub = new ListCell<String>() {
#Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (item != null) {
StackPane p = new StackPane();
Label label = new Label(item);
p.getChildren().addAll(img, label);
setGraphic(p);
p.setAlignment(Pos.CENTER_LEFT);
}
}
};
blub.setStyle("-fx-background-color:transparent");
return blub;
}
});
Big thanks!
I don't think it's possible.
Maybe try to play with the Skin of the ListView. It seems that the scroll bar are managed in this class. It do not use a scroll pane.
Another solution could be replacing the ListView by a VBox in a ScrollPane.
Finally, you could try to modify img (by the way, where it come from, and what Class is it ?) to only show what you need.
Anyway, I'm interested by the solution you will use.

How to make A Spinner access the other Spinner?

I have Three Spinners= spinState,spinCounty,& spinCity, i would like to select the State spinner then choose a State,then the second spinner would give me the list of Counties within that particular state,then select the County,then the third spinner would give me a list of the cities with in that particular county,such as: (State)Florida,(County)Dade,(City)Miami then after all 3 have been selected pass that information to the next Activity/Class. Can anyone help? here is my code
Spinner spinState,spinCounty,spinCity;
Button bNext;
protected void onCreate(Bundle)
{
//TODO Auto generated method stub
super.oncreate(Bundle)
setContentView(R.layout.info);
Spinner States = (Spinner) findViewById(R.id.spinState);
ArrayAdapter USstates = ArrayAdapter.createFromResource(this,
R.array.States, android.R.layout.simple_spinner_item);
USstates.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
States.setAdapter(USstates);
Spinner Counties = (Spinner) findViewById(R.id.spinCounty);
ArrayAdapter UScounties = ArrayAdapter.createFromResource(this,
R.array.Counties, android.R.layout.simple_spinner_item);
UScounties.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Counties.setAdapter(UScounties);
Spinner Cities = (Spinner) findViewById(R.id.spinCity);
ArrayAdapter UScities = ArrayAdapter.createFromResource(this,
R.array.Cities,android.R.layout.simple_spinner_item);
UScities.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Cities.setAdapter(UScities);
initialize();
bNext.setOnClickListener(this);
}
What code should i use and where?PS. whoever may answer could u use my exact variables so i won't be confused,Thanks in Advance.
countries.setOnItemSelectedListener(new OnItemSelectedListener() {
ArrayAdapter<String> stateadapter=null;
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
switch (pos) {
case 0:
stateadapter = new ArrayAdapter<String>(
activityclass.this,
android.R.layout.simple_spinner_item, Arrays
.asList(getResources().getStringArray(
R.array.USAstate)));
states.setAdapter(stateadapter);
case 1:
stateadapter = new ArrayAdapter<String>(
activityclass.this,
android.R.layout.simple_spinner_item, Arrays
.asList(getResources().getStringArray(
R.array.Indiastate)));
states.setAdapter(stateadapter);
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
Select State
<string-array name="USAstate">
<item>california</item>
<item>texas</item>
<item>virgina</item>
<item>alaska</item>
</string-array>

Resources