I am currently facing difficulty trying to group the switches inside recylerView. I have attached my code below and a snapshot of the screen.
As you can see, I can now toogle any switch but at a given point in time , I want only one switch to be toggled on.
Item_list.xml :-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:orientation="vertical"
android:paddingBottom="#dimen/row_padding_vertical"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/row_padding_vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="87dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="5dp"
android:layout_marginRight="30dp"
card_view:cardCornerRadius="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="95dp"
android:background="#20b2aa"
android:orientation="vertical">
<LinearLayout
android:layout_width="182dp"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp"
android:layout_marginRight="30dp"
android:orientation="horizontal">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffff"
android:textSize="32dp" />
</LinearLayout>
<Switch
android:id="#+id/value"
android:layout_width="59dp"
android:layout_height="3dp"
android:layout_marginLeft="190dp"
android:layout_marginTop="-35dp"
android:layout_marginBottom="100dp"
android:scaleX="1.1"
android:scaleY="1.1"
android:theme="#style/SwitchTheme" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
</RelativeLayout>
ItemAdapter:-
package com.example.loginpage;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Switch;
import android.widget.TextView;
import java.util.List;
public class ItemAdapter extends
RecyclerView.Adapter<ItemAdapter.MyViewHolder> {
private int cn=0;
private List<Item> itemList;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView name;
public Switch value;
public MyViewHolder(View view) {
super(view)
name = (TextView) view.findViewById(R.id.name);
value = (Switch) view.findViewById(R.id.value);
}
}
public ItemAdapter(List<Item> itemList) {
this.itemList = itemList;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int
viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_list, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Item item = itemList.get(position);
holder.name.setText(item.getName());
}
#Override
public int getItemCount() {
return itemList.size();
}
}
My current interface looks like this....
https://www.pastepic.xyz/image/8z5Eh
I suggest that you set a Click listener on all value switches and disable all switches when any switch clicked.
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Item item = itemList.get(position);
holder.name.setText(item.getName());
holder.value.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
for (Item x: itemList){
// x.Selected=false;
// here you make all of your switches to false
}
//itm.Selected = true;
// here you make the current switch to on or true
notifyDataSetChanged();
}
});
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Item item = itemList.get(position);
holder.name.setText(item.getName());
holder.value.setChecked(item.isValueChecked());
holder.value.setOnCheckedChangeListener(new OnCheckedChangeListener(
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
for(Item i: itemList){
i.setValueChecked(false);
}
if(buttonView.isChecked()){
itemList.get(position).setValueChecked(true);
holder.value.setChecked(true);
}
notifyDataSetChanged();
}
});
}
Try this solution, this should work...
Checked Change Listener is set on the switch if any switch is turned ON then all the values of the itemList is made false and current value is made True and switch is set ON, finally, notifyDataSetChanged() is called.
If any switch is turned OFF then all the values in the itemList are made False and notifyDataSetChanged() is called so that the switch state gets updated.
holder.value.setChecked(item.isValueChecked());
This updates the switch state based on the values stored in the itemList.
Related
I created a Java class that should display something like this:
But this is what I get when I run the app:
It doesn't show what I wanted and Android Studio doesn't show any error in Logcat, so I don't know why it's not working.
This is the code for the class SetsActivity:
package com.example.myquiz;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.GridView;
import android.widget.Toolbar;
public class SetsActivity extends AppCompatActivity {
private GridView sets_grid;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sets);
androidx.appcompat.widget.Toolbar toolbar = findViewById(R.id.set_toolbar);
//setSupportActionBar(toolbar);
String title = getIntent().getStringExtra("CATEGORY");
getSupportActionBar().setTitle(title);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
sets_grid = findViewById(R.id.sets_gridview);
SetsAdapter adapter = new SetsAdapter(2);
sets_grid.setAdapter(adapter);
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
if(item.getItemId() == android.R.id.home)
{
SetsActivity.this.finish();
}
return super.onOptionsItemSelected(item);
}
}
I also created an adapter class SetsAdapter so I'm gonna post the code for it as well:
package com.example.myquiz;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class SetsAdapter extends BaseAdapter {
private int numOfSets;
public SetsAdapter(int numOfSets) {
this.numOfSets = numOfSets;
}
#Override
public int getCount() {
return 0;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (convertView == null) {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.set_item_layout, parent, false);
} else {
view = convertView;
}
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(parent.getContext(),QuestionActivity.class);
parent.getContext().startActivity(intent);
}
});
((TextView) view.findViewById(R.id.setNo_tv)).setText(String.valueOf(position + 1));
return view;
}
}
And here is the code for the XML file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SetsActivity"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="#+id/set_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.Dark"
></androidx.appcompat.widget.Toolbar>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sets"
android:textSize="26sp"
android:textStyle="bold"
android:padding="16dp" />
<GridView
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="#+id/sets_gridview"
android:layout_weight="1"
android:gravity="center"
android:horizontalSpacing="16dp"
android:verticalSpacing="16dp"
android:padding="16dp"
android:columnWidth="100dp"
android:numColumns="auto_fit"
></GridView>
</LinearLayout>
Here is the code for the XML file set_item_layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dp"
android:layout_height="100dp"
android:orientation="vertical"
android:gravity="center"
android:background="#drawable/round_corner"
android:backgroundTint="#FFC3C3"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:id="#+id/setNo_tv"
android:textStyle="bold"
android:textColor="#android:color/black"
android:textSize="45sp"></TextView>
</LinearLayout>
The getCount() method in the SetsAdapter should return numOfSets not 0.
#Override
public int getCount() {
return numOfSets;
}
Recyclerview is not displayed in fullscreen. Below is the layout file and the code I've written. After executing, the contents are displayed as shown in the below picture. The height is only the highlighted part. I want the contents to be fullscreen. I remaining contents are within this highlighted area which is scrollable. I want the contents to be displayed in fullscreen. Any help would be helpful.
Layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="#+id/activity_search"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:clickable="true">
<TextView
android:id="#+id/title"
android:textSize="16dp"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/genre"
android:layout_below="#id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/year"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_height="wrap_content" />
</RelativeLayout>
</LinearLayout>
Code:
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
public class setSearchData extends RecyclerView.Adapter<setSearchData.MyViewHolder> {
private List<SearchDisplayContents> moviesList;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title, year, genre;
public MyViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.title);
genre = (TextView) view.findViewById(R.id.genre);
year = (TextView) view.findViewById(R.id.year);
}
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.activity_searchresults, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
SearchDisplayContents movie = moviesList.get(position);
holder.title.setText(movie.getTitle());
holder.genre.setText(movie.getGenre());
holder.year.setText(movie.getYear());
}
#Override
public int getItemCount() {
return moviesList.size();
}
}
Calling function code:
List<SearchDisplayContents> movieList = new ArrayList<>();
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
setSearchData mAdapter = new setSearchData(movieList);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
SearchDisplayContents movie = new SearchDisplayContents("Mad Max: Fury Road", "Action & Adventure", "2015");
movieList.add(movie);
movie = new SearchDisplayContents("Inside Out", "Animation, Kids & Family", "2015");
movieList.add(movie);
mAdapter.notifyDataSetChanged();
You should use different layout for your viewholder. Take the RelativeLayout you have there, cut it out, put it in different xml file then pass it to onCreateViewHolder.
From look at your code - it looks like what is in the preview screenshot is the same as what is within the RelativeLayout you have staticly defined.
There is an issue with your approach. It is that you are referencing the RelativeLayout (within the same xml hierarchy as your RecylerView) as the CellViewHolder in the RecyclerView.Adapter. My approach personally is to create a seperate xml layout for the cell viewholder.
Follow this guide, it is very detailed:
https://guides.codepath.com/android/using-the-recyclerview
You should use different layout for your viewholder. Take the RelativeLayout you have there, cut it out, put it in different xml file then pass it to onCreateViewHolder.
The above coding has something work use different layout mean one for Recycleview and other for contents in list
main_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/uslist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="#dimen/_49sdp"
android:divider="#android:color/transparent" />
</RelativeLayout>
list_content.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:clickable="true">
<TextView
android:id="#+id/title"
android:textSize="16dp"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/genre"
android:layout_below="#id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/year"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_height="wrap_content" />
</RelativeLayout>
Adapter.java
public class yourAdapter extends RecyclerView.Adapter<yourAdapter .SimpleViewHolder> {
ArrayList<UsageRPDetails> mylist;
private Context mContext;
public yourAdapter (Context context, ArrayList<yourArray or model> checklist) {
mContext = context;
mylist = checklist;
}
#Override
public yourAdapter .SimpleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_content, parent, false);
return new yourAdapter .SimpleViewHolder(view);
}
#Override
public void onBindViewHolder(final yourAdapter .SimpleViewHolder holder, final int position) {
holder.title.setText();
holder.year.setText();
holder.genre.setText();
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public int getItemCount() {
return mylist.size();
}
#Override
public int getItemViewType(int i) {
return 0;
}
public static class SimpleViewHolder extends RecyclerView.ViewHolder {
#Bind(R.id.title)
TextView title;
#Bind(R.id.genre)
TextView genre;
#Bind(R.id.year)
TextView year;
public SimpleViewHolder(View itemView) {
super(itemView);
title= (TextView) itemView.findViewById(R.id.title);
genre= (TextView) itemView.findViewById(R.id.genre);
genre= (TextView) itemView.findViewById(R.id.genre);
}
}
}
Also write a java file for Activity that calls this adapter class and call the recycleview in this activity
I have a problem with setting the visibility of an TextView.
I have two xmls. One for the fragment itself and the other for the RecyclerView.
In the fragment_settings.xml I'm setting the RecyclerView. In the settings_list_row.xml I define the RecyclerView row.
When I create a new RecyclerView row I sometimes have a row where twice of the TextView's are filled and sometimes just one. Because of this I want to set the visibility of one TextView to gone if it's empty.
Because my View is the fragment_settings.xml I don't have access to the row view. Is there a solution for this? Thanks a lot!
fragment_settings.xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="de.myfitindustry.myfitindustry.app.SettingsFragment">
<TextView
android:id="#+id/accountTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:textStyle="bold"
android:textColor="#color/colorPrimary"
android:text="#string/accountTitle" />
<android.support.v7.widget.RecyclerView
android:id="#+id/settings_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" />
</RelativeLayout>
settings_list_row.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:focusable="true"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:clickable="true"
android:background="?android:attr/selectableItemBackground"
android:orientation="vertical">
<TextView
android:id="#+id/settingTitle"
android:textColor="#color/colorBlack"
android:layout_width="match_parent"
android:textSize="16dp"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/settingSubtitle"
android:layout_below="#id/settingTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
Fragments onCreateView method
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_settings, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.settings_recycler_view);
sAdapter = new SettingsAdapter(settingList);
RecyclerView.LayoutManager sLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext()) {
// Disable scrolling in the RecyclerView
#Override
public boolean canScrollVertically() {
return false;
}
};
recyclerView.setLayoutManager(sLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
prepareSettingsData();
recyclerView.setAdapter(sAdapter);
// HERE I WANT TO CHANGE THE VISIBILITY
TextView settingSubtitle = (TextView) view.findViewById(R.id.settingSubtitle);
if(!settingSubtitle.getText().equals("")) {
settingSubtitle.setVisibility(TextView.GONE);
}
return view;
}
My RecyclerView Adapter:
package de.myfirstapp.app;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
/**
* Created by Johannes on 19.01.2017.
*/
public class SettingsAdapter extends RecyclerView.Adapter<SettingsAdapter.MySettingHolder> {
private List<Settings> settingList;
public class MySettingHolder extends RecyclerView.ViewHolder {
public TextView settingTitle, settingSubtitle;
public MySettingHolder(View view) {
super(view);
settingTitle = (TextView) view.findViewById(R.id.settingTitle);
settingSubtitle = (TextView) view.findViewById(R.id.settingSubtitle);
}
}
public SettingsAdapter (List<Settings> settingList) {
this.settingList = settingList;
}
#Override
public MySettingHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.settings_list_row, parent, false);
return new MySettingHolder(itemView);
}
#Override
public void onBindViewHolder(MySettingHolder holder, int position) {
// Setting for one entry
Settings setting = settingList.get(position);
holder.settingTitle.setText(setting.getSettingTitle());
holder.settingSubtitle.setText(setting.getSettingSubtitle());
}
#Override
public int getItemCount() {
return settingList.size();
}
}
Why you don't do it in SettingsAdapter onBindViewHolder method? Imagine, you have bunch of items in your RecyclerView and bunch of settingSubtitle, how system will understand which TextView do you really want?
Update
#Override
public void onBindViewHolder(MySettingHolder holder, int position) {
// Setting for one entry
Settings setting = settingList.get(position);
if (setting.getSettingSubtitle().equals("")) {
holder.settingTitle.setText(setting.getSettingTitle());
holder.settingSubtitle.setVisibility(View.GONE);
} else {
holder.settingSubtitle.setVisibility(View.VISIBLE);
holder.settingTitle.setText(setting.getSettingTitle());
holder.settingSubtitle.setText(setting.getSettingSubtitle());
}
}
I've been searching for hours and hours on trying to understand the relationship between Fragments and Activities and I still cant get it right with my code.
I have a mainScreen class that loads actvity_main.xml. This main screen has a layout with a graph and sidebar etc.
I have a button that is on the side bar that is supposed to launch a sidebar fragment that has a checkbox listview in it so I can select data to show on my graph etc. But that fragment is not showing no matter what I do. It shows the "Bing~!" Toast but no other view is launched.
Perhaps I shouldn't be using a fragment? I don't want to start a new activity as the checkbox is supposed to interact with the graph and another fragment with a dynamic table in it and it will interact back and forth and so on.
I'm not too sure what to do here. I have about 3 weeks of Android experience so I'm not exactly knowledgeable about the whole shebang of it just yet. Really appreciate any ideas or help I can get. I am completely stumped and hence had to post my own question.
I really really appreciate any kind of help! Thanks!
mainScreen.java
public class mainScreen extends Activity {
private TextView text_display;
private Button button_list;
private Button button_table;
private String[] dataHolder;
public boolean listClicked = false;
public void loadData () {
//loaddata stuff here
}
public void createGraph () {
//graph create stuff here
}
public void buttonClick () {
button_list.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
sideFragment sf = new sideFragment();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.side_fragment, sf);
ft.commit();
Toast.makeText(mainScreen.this, "Bing~!", Toast.LENGTH_SHORT).show();
}
}
);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent i = getIntent();
dataHolder = i.getStringArrayExtra("dataHolder");
button_list = (Button)findViewById(R.id.button_list);
loadData();
createGraph();
buttonClick();
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".mainScreen"
android:id="#+id/mainlayout"
android:orientation="horizontal"
android:weightSum="1">
<LinearLayout
android:orientation="vertical"
android:layout_width="85dp"
android:layout_height="match_parent">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="85dp"
android:layout_height="85dp"
android:text="Select"
android:id="#+id/button_list" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="85dp"
android:layout_height="85dp"
android:text="Table"
android:id="#+id/button_table" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:text=" "
android:id="#+id/text_display"
android:textSize="26dp"
android:layout_margin="5dp" />
<com.jjoe64.graphview.GraphView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/graph" />
</LinearLayout>
<fragment
android:name="xabre.mobileicip.sideFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/side_fragment" />
</LinearLayout>
sideFragment.java
public class sideFragment extends Fragment implements android.widget.CompoundButton.OnCheckedChangeListener {
ListView listviewFrag;
ArrayList<sideFrag> sideFragList;
sideFragAdapter sfAdapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.side_fragment, container, false);
listviewFrag = (ListView) view.findViewById(R.id.side_listview);
//displayList();
return view;
}
private void displayList() {
sideFragList = new ArrayList<sideFrag>();
sideFragList.add(new sideFrag("SPO2"));
sideFragList.add(new sideFrag("O2 Flow Rate"));
sideFragList.add(new sideFrag("Resp."));
sideFragList.add(new sideFrag("Cardiac Output"));
sideFragList.add(new sideFrag("Cardiac Index"));
sideFragList.add(new sideFrag("SVR"));
sideFragList.add(new sideFrag("Wedge Pressure"));
listviewFrag.setAdapter(sfAdapter);
}
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int pos = listviewFrag.getPositionForView(buttonView);
if (pos != ListView.INVALID_POSITION) {
sideFrag sf = sideFragList.get(pos);
sf.setSelected(isChecked);
Toast.makeText(getActivity(),"" + sf.getName(), Toast.LENGTH_SHORT).show();
//Toast.makeText(sideFragment.this, "Clicked on sideFrag: " + sf.getName() + ". State: is " + isChecked, Toast.LENGTH_SHORT).show();
}
}
}
side_fragment.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".sideFragment"
android:id="#id/side_fragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="HELLO WORLD"
android:id="#+id/helloTester"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp">
<ListView
android:id="#+id/side_listview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
</LinearLayout>
side_list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<CheckBox android:id="#+id/check_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onCheckboxClicked"
android:layout_marginBottom="15dp" />
<TextView
android:id="#+id/check_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/check_box"
android:textStyle="bold"/>
</RelativeLayout>
sideFragAdapter.java
package xabre.mobileicip;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import java.util.List;
class sideFrag {
String name;
boolean selected = false;
public sideFrag(String name) {
super();
this.name = name;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class sideFragAdapter extends ArrayAdapter<sideFrag> implements CompoundButton.OnCheckedChangeListener {
private List<sideFrag> sideList;
private Context context;
public sideFragAdapter (List<sideFrag> sideList, Context context) {
super(context, R.layout.side_list, sideList);
this.sideList = sideList;
this.context = context;
}
private static class sideHolder {
public CheckBox check_box;
public TextView check_name;
/* public CheckBox check_O2FR;
public CheckBox check_Resp;
public CheckBox check_Carout;
public CheckBox check_Carind;
public CheckBox check_svr;
public CheckBox check_resprate;
public CheckBox check_Peep;
public CheckBox check_O2AF;
public CheckBox check_FIO2;
public CheckBox check_PO2;
public CheckBox check_HCO3;
public CheckBox check_Urea;
public CheckBox check_Potassium;
public CheckBox check_Sodium;
public CheckBox check_Creatinine;
public CheckBox check_FluidIn;
public CheckBox check_FluidOut;
*/
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
sideHolder holder = new sideHolder();
if(convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.side_list, parent, false);
holder.check_box = (CheckBox) v.findViewById(R.id.checkbox);
holder.check_name = (TextView) v.findViewById(R.id.check_name);
holder.check_box.setOnCheckedChangeListener(this);
} else {
holder = (sideHolder) v.getTag();
}
sideFrag p = sideList.get(position);
holder.check_name.setText(p.getName());
holder.check_box.setChecked(p.isSelected());
holder.check_box.setTag(p);
return v;
}
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
}
}
You can only replace elements added from code. side_fragment is added in XML and as such is part of, let's say "read only" structure. You need to remove <fragment> element it from XML, and add it from code if you want to replace it later.
i'm pretty new to android programming and tried to implement a basic expandable listview showing a list of Courses which should show some additional details when clicked. i feel like the problem is very basic as i don't include anything fancy in any of the views, but i still could not find a solution online, most of the other questions are a lot more specific.
The first part works fine(displaying the list of courses), the problem is that the list is not clickable, meaning it does not expand.
what i have determined right now is the following:
1. the getChildView is never called(debugger never stops at breakpoint)
2. there is data for the children as it's the same data that's used for the groupheading which works fine.
I have also tried to set an onGroupClickListener which was never called, so i removed it again.
my code:
Group heading layout:
<TextView
android:id="#+id/CourseName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="bottom"
android:text="CourseName"
android:layout_weight="7"
android:layout_marginRight="25dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/CourseDate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="bottom"
android:layout_weight="3"
android:text="CourseDate" />
<CheckBox
android:id="#+id/checkBox"
style="?android:attr/starStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
child_row layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Button" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/button1"
android:text="Button" />
<TextView
android:id="#+id/phases"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:gravity="center"
android:layout_below="#+id/button1"
android:text="phases:" />
<TextView
android:id="#+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/phases"
android:gravity="center"
android:text="information:" />
</RelativeLayout>
activity layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/sportName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:gravity="center"
android:text="Sport Name"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="#dimen/profile_title" />
<RatingBar
android:id="#+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:numStars="5"
android:stepSize="1"
android:layout_centerInParent="true" />
<ExpandableListView
android:id="#+id/expandableListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/sportName" >
</ExpandableListView>
</RelativeLayout>
Adapter java:
package ch.unibe.unisportbern.views.details;
import java.util.ArrayList;
import com.example.unisportbern.R;
import ch.unibe.unisportbern.support.Course;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
public class SportsAdapter extends BaseExpandableListAdapter {
private Context context;
private ArrayList<Course> courseList;
public SportsAdapter(Context context, ArrayList<Course> courseList) {
this.context = context;
this.courseList = courseList;
}
#Override
public Object getChild(int index, int stub) {
return courseList.get(index);
}
#Override
public long getChildId(int index, int stub) {
return stub;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null){
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.child_row, null);
}
TextView phases = (TextView) convertView.findViewById(R.id.phases);
TextView info = (TextView) convertView.findViewById(R.id.info);
phases.setText("phases:\n" + courseList.get(groupPosition).getPhases());
info.setText(courseList.get(groupPosition).getInformation());
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
return 1;
}
#Override
public Object getGroup(int groupPosition) {
return courseList.get(groupPosition);
}
#Override
public int getGroupCount() {
return courseList.size();
}
#Override
public long getGroupId(int groupPosition) {
return courseList.get(groupPosition).getId();
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inf.inflate(R.layout.group_heading, null);
}
TextView courseName = (TextView) convertView.findViewById(R.id.CourseName);
TextView courseDate = (TextView) convertView.findViewById(R.id.CourseDate);
courseName.setText(courseList.get(groupPosition).getName());
courseDate.setText(courseList.get(groupPosition).getDay() + courseList.get(groupPosition).getTime());
return convertView;
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
activity java:
package ch.unibe.unisportbern.views.details;
import java.util.ArrayList;
import com.example.unisportbern.R;
import ch.unibe.unisportbern.support.Course;
import ch.unibe.unisportbern.support.DBMethodes;
import ch.unibe.unisportbern.support.Sport;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ExpandableListView;
public class DActivity extends Activity{
public final static String NAME = "SportName";
public final static String ID = "SportID";
private Sport sport;
private ArrayList<Course> courses;
private SportsAdapter sportsadapter;
private ExpandableListView myList;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
getSport();
getCourses();
setContentView(R.layout.details_layout);
myList = (ExpandableListView) findViewById(R.id.expandableListView);
sportsadapter = new SportsAdapter(this, courses);
myList.setAdapter(sportsadapter);
}
private void getCourses() {
DBMethodes dbMethodes = new DBMethodes(this);
try {
courses = dbMethodes.getAllCourses(sport);
} catch (Exception e) {
}
}
private void getSport() {
Intent intent = this.getIntent();
int id = intent.getIntExtra(ID, 0);
String name = intent.getStringExtra(NAME);
this.sport = new Sport(id, name);
}
}
thanks a lot in advance!
EDIT:
i found it by chance. the checkbox inside the groupheader was focusable, stealing all the clicks away from the list. to solve this problem simply set the focusable attribute of the checkbox/button in your list to false.
I found it by chance. the checkbox inside the groupheader was focusable, stealing all the clicks away from the list. to solve this problem simply set the focusable attribute of the checkbox/button in your list to false with either:
in Java:
checkbox.setFocusable(false);
OR, in xml:
android:focusable="false"
i was facing the same issue but what i added the below line to my Parent view
android:descendantFocusability="blocksDescendants"
and it worked for me.