Using Listview, I deleted item if check box is checked.
Although it has been implemented through googling, it does not work when you press the checkbox and then press the delete button.
I wrote it like the source below, but nothing reacts. What should I fix?
<MainActivity.kt>
lv.choiceMode = ListView.CHOICE_MODE_MULTIPLE
val del = binding.delBtn
val lv = binding.lvMainMusic
del.setOnClickListener {
val count: Int = mainAdapter.count
if (count > 0) {
val checked: Int = lv.checkedItemPosition
if (checked > -1 && checked < count) {
playList.removeAt(checked)
mainAdapter.notifyDataSetChanged()
lv.clearChoices()
}
}
}
}
<Music_items.xml>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/album_img"
android:layout_width="75dp"
android:layout_height="75dp"
android:paddingLeft="10dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/song"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:text="title"
android:textSize="15dp" />
<TextView
android:id="#+id/singer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:text="singer" />
</LinearLayout>
<CheckBox
android:id="#+id/choose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingTop="10dp"
android:paddingRight="10dp" />
So what I understood from the question is that whenever the user checks an item and then clicks on delete, you want to remove that checked item from the list and update the UI accordingly in listView. Generally, when you want to remove that item you have to remove it from the mainAdapter's list. Something like
mainAdapter.remove(checked)
And in mainAdapter you have a method defined that removes the item at that position from its list like
fun remove(position: Int){
playList.remove(position)
notifyDataSetChanged()
notifyItemRangeChanged(0, playList.size)
}
Sorry if I misunderstood the question..
Related
i make dynamic radio button list inside each row in recyclerview but when i random scroll all radio button uncheked i use model to store radiobutton condition,notifyItemChanged,initialize first cheked,using layout instead radiogroup , and use selection condition to check spesific radiobutton, but i cant solve this problem
following the first picture all radio button is checked
but when scrolling down and back up all radiobutton is not checked
this my item layout inside recyclerview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayoutlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="2">
<LinearLayout
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/questionText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sumber Data" />
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:orientation="vertical">
<CheckBox
android:id="#+id/tidak_ada"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Tidak Ada" />
</LinearLayout>
</TableRow>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<RadioGroup
android:id="#+id/radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"/>
<LinearLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"></LinearLayout>
</LinearLayout>
</LinearLayout>
my code to create radiobutton inside adapter in onBindViewHolder
var rprms = InputFieldHolder.radiogroup?.layoutParams
if(rprms != null && question.pilihan_jawabans.count() > 0){
InputFieldHolder.radiogroup?.removeAllViews()
question.pilihan_jawabans.forEachIndexed {i,e->
val rdbtn = RadioButton(context)
rdbtn.id = question.pilihan_jawabans[i].id
rdbtn.text = question.pilihan_jawabans[i].isi.toString()
rprms = RadioGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
InputFieldHolder.radiogroup?.addView(rdbtn, rprms)
val size = InputFieldHolder.radiogroup?.childCount?.minus(1)
if(question.pilihan_jawabans[i].checked){
for (x in 0..size!!) {
if((InputFieldHolder.radiogroup?.getChildAt(x) is RadioButton)){
if((InputFieldHolder.radiogroup?.getChildAt(x) as RadioButton).id != question.pilihan_jawabans[i].id){
(InputFieldHolder.radiogroup?.getChildAt(x) as RadioButton).setChecked(false)
}
}
}
}
rdbtn.setChecked(question.pilihan_jawabans[i].checked)
rdbtn.setTag(Integer(question.pilihan_jawabans[i].id))
rdbtn.setOnCheckedChangeListener({ group, checked ->
question.pilihan_jawabans.forEachIndexed {ii,ee->
if(checked){
question.pilihan_jawabans[ii].checked = false
}
}
question.pilihan_jawabans[i].checked = checked
notifyItemChanged(position)
})
}
}
my model to store radiobutton checked state
#Parcelize
class QuestionSelection(
var id : Int,
var isi : String,
var keterangan : String,
var checked : Boolean = false
) : Parcelable
NOTE : my radiobutton is dynamic so not just 2 option but more than it,depending on the data that I took from the API
after few hour to solve , i finally got what cause the problem.i always call InputFieldHolder.radiogroup?.removeAllViews() to destroy all radiobutton and make new with new satet but radio group always save state from previous radiobutton has been destroy so in last i just call InputFieldHolder.radiogroup?.clearCheck() after destroy radio button to clear all check state inside the radio group
I am in a weird situation in my existing project. In my project i want to inflate one layout over the other as a stack dynamically. At present in my project i managed to show the layouts one below the other vertically by appending them, this approach has been chosen by me because even though i am trying to show it as stack only one image is appearing at the front during runtime instead of a bunch. In order to understand my problem please go through the following image links and code
desired result
acquired_result_when_tried_to_acheive_desired_result
approached way
Parent layout:
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/imagegallery"
android:orientation="vertical"></LinearLayout>
child layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="#+id/relativeImage"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_below="#+id/rl1"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="15dp" >
<RelativeLayout
android:id="#+id/parent"
android:layout_width="wrap_content"
android:layout_height="250dp"
android:orientation="vertical" >
<Button
android:id="#+id/left1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:background="#drawable/close" />
<Button
android:id="#+id/right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="#drawable/right" />
<ImageView
android:id="#+id/image"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:adjustViewBounds="true"
android:background="#drawable/deelpizza"
android:paddingBottom="15dp"
android:paddingEnd="0dp"
android:paddingStart="0dp"
android:scaleType="fitCenter" />
<RelativeLayout
android:id="#+id/left"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/image"
android:background="#drawable/text_bg" >
<ImageView
android:id="#+id/imageprew"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="15dp"
android:background="#drawable/icon_3" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/imageprew"
android:text="This is an exclusive Deel good for today only clime this deel before its close!"
android:textColor="#FFFFFF" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="15dp"
android:layout_below="#+id/imageprew" >
</RelativeLayout>
</RelativeLayout>
<!--
<Button
android:id="#+id/left"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:background="#drawable/text_bg"
android:layout_alignTop="#+id/image"
/>
-->
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/relativeDetails"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/relativeImage"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#drawable/rectangle" >
<TextView
android:id="#+id/locationText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/descText"
android:layout_marginLeft="10dp"
android:layout_marginTop="8dp"
android:text="woodfire pizza, Los Gatos CA\nLimited time offer, redeemable today only."
android:textColor="#000000" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="10dp"
android:layout_below="#+id/locationText" />
<TextView
android:id="#+id/descText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="8dp"
android:text="1 large, 2 topping pizza"
android:textColor="#000000"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:text="$15"
android:textColor="#FF0000"
android:textSize="25dp" />
</RelativeLayout>
JavaCode:
private void addImagesToThegallery() {
LinearLayout imageGallery = (LinearLayout)findViewById(R.id.imagegallery);
//imageGallery.setPadding(0, 0, 0, 30);
LayoutInflater layoutInfralte=(LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
List views=new ArrayList();
View view;
for(int j=0; j<=5; j++)
{
view=layoutInfralte.inflate(R.layout.newdeels, null);
view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
views.add(view);
}
for(int i = 0; i<views.size(); i++)
{
imageGallery.addView((View) views.get(i));
}
Please go through my code and let me know where am i going wrong and let me know if i am unclear anywhere
Change the margin top to make views come down a bit
for(int j=0; j<=5; j++)
{
view=layoutInfralte.inflate(R.layout.newdeels, null);
LinearLayout.LayoutParams lp=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
lp.setMargins(left,5*j, right, bottom);
view.setLayoutParams(lp);
views.add(view);
}
for(int i = 0; i<views.size(); i++)
{
imageGallery.addView((View) views.get(i));
}
Checkout following
Github : Material Recent Layout
Usage
RecentsList recents = (RecentsList) findViewById(R.id.recents);
recents.setAdapter(new RecentsAdapter() {
#Override
public String getTitle(int position) {
return "Item "+position;
}
#Override
public View getView(int position) {
ImageView iv =new ImageView(RecentsActivity.this);
iv.setImageResource(R.drawable.mazda);
iv.setBackgroundColor(0xffffffff);
return iv;
}
#Override
public Drawable getIcon(int position) {
return getResources().getDrawable(R.mipmap.ic_launcher);
}
#Override
public int getHeaderColor(int position) {
return colors[random.nextInt(colors.length)];
}
#Override
public int getCount() {
return 10;
}
}
Note
You need to modify as per your requirement.
Try FrameLayout in your parent layout. Set different margins in child views to achive an effect of stack.
Child views are drawn in a stack, with the most recently added child on top
check out : github.com/kikoso/Swipeable-Cards
As per your response :
hi thank you for your response ur response actually worked. But now i am dealing with another situation where-in i would like to update details based on swipe for eg: if a card is swiped right it should be gestured as like where as if a card is swiped left it should be gestured as dislike so how to actually code these swipe gestures can you please help me out
#ashwin for that you should have a like and dislike drawable or a custom view.
card.setOnCardDimissedListener(new CardModel.OnCardDismissedListener() {
#Override
public void onLike() {
Log.d("Swipeable Card", "I liked it");
}
#Override
public void onDislike() {
Log.d("Swipeable Card", "I did not liked it");
}
});
Use above snippet to show or hide the view of like or dislike in respective methods that is onLike() and onDislike().
Edit 1:
I figured out the problem what you are facing, If you are simply implementing the Sample Code. Then it clearly shows that CardContainer has SimpleCardStackAdapter as adapter and CardModel are added to it using new operator. Except the last element of CardModel that is added separately and cardModel Listener is applied on last element only. If you want to apply it on all elements. Then you should have to add the elements to SimpleCardStackAdapter by ArrayList of CardModel.
I am trying a calendar application. I used Baseadapter and Gridview for work the calendar. I want, when an user clicks an item in calendar, clicked items change background color.
I found several methods for this. setSelection method, requestFocus method etc. Then I decided using the requestFocus.
My Problem
When I click an item in calendar, It sends requestFocus and everything is okay. Then I click another item and It works okay. Main problem starts here, When I click an item that I've already clicked on, It doesn't work. So an item works only once.
I hope you can understand my question.
Please suggest a solution to me.
thank you.
PlaceholderFragment.java
calendarAdapter = new CalendarAdapter( whole_list, getActivity() );
calendarGridView = ( GridView ) rootView.findViewById( R.id.calendarGridView );
calendarGridView.setAdapter( calendarAdapter );
calendarGridView.setOnItemClickListener( new AdapterView.OnItemClickListener() {
#Override
public void onItemClick( AdapterView< ? > parent, View view, int position, long id ) {
DaysSettings get_spesific = whole_list.get( position );
RelativeLayout r = ( RelativeLayout ) view.findViewById( R.id.calendar_border );
r.setFocusable( true );
r.setFocusableInTouchMode( true );
r.requestFocus();
}
} );
calendar_inflate.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
android:background="#drawable/selector"
android:layout_marginTop="#dimen/calendar_rl_margin"
android:layout_marginBottom="#dimen/calendar_rl_margin"
android:layout_width="#dimen/calendar_circle_wh"
android:layout_height="#dimen/calendar_circle_wh"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:id="#+id/calendar_border">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/calendarparentlayout"
android:layout_margin="1dp"
>
<ImageView
android:contentDescription="Cinsel ilişki olduğunda kalp işareti çıkması için kullanılır."
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/had_sex"
android:src="#drawable/heart"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="#drawable/circle_white"
android:padding="3dp"
android:visibility="gone"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10sp"
android:text="0"
android:id="#+id/calendarTextiew"
android:textColor="#color/main_color_one"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#id/calendarTextiew"
android:gravity="center_horizontal">
<ImageView
android:contentDescription="İlaç hatırlatırıcı resmidir."
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/medicine_reminder"
android:src="#drawable/pill"
android:layout_marginRight="2dp"
android:layout_marginLeft="2dp"
android:visibility="gone"
/>
<ImageView
android:contentDescription="Note eklendi resmidir."
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/adding_note"
android:src="#drawable/plus"
android:layout_marginRight="2dp"
android:layout_marginLeft="2dp"
android:visibility="gone"
/>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
I have a custom ListView with a radiogroup in each row.
When I change the checked radio button, I call a dialog with some edittext fields (using the onCheckedChanged() method). But, when i focused an edittext to write something, I lose all the checked radiobuttons which are covered by keyboard, and the group returns to the default option selected.
can someone help me?
List adapter
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ContractItemHolder cih = new ContractItemHolder();
if (row == null){
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(R.layout.row_proposals_item, parent, false);
cih.setTvItemTitle((TextView)row.findViewById(R.id.textViewItemTitle));
cih.setRgItemStatus((RadioGroup)row.findViewById(R.id.radioGroupStatus));
row.setTag(cih);
}else {
cih=(ContractItemHolder)row.getTag();
}
final ContractItem ci = list.get(position);
cih.getRgItemStatus().setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
groupSel = group;
int selected = group.getCheckedRadioButtonId();
Dialog d;
switch (selected) {
case R.id.radioAccepted:
d = createDialog(context, ACCEPTED_CODE, ci, selected);
d.show();
break;
case R.id.radioRefused:
d = createDialog(context, REFUSED_CODE, ci, selected);
d.show();
break;
default:
break;
}
}
});
cih.getTvItemTitle().setText(ci.getDescItem());
return row;
}
List item layout (the row..)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp"
android:gravity="center">
<TextView
android:id="#+id/textViewItemTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Large Text"
android:layout_weight="3"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<RadioGroup
android:id="#+id/radioGroupStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1"
android:showDividers="middle">
<RadioButton
android:id="#+id/radioNull"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Non Proposto"
android:checked="true"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<RadioButton
android:id="#+id/radioPending"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="In trattativa"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/Blue"/>
<RadioButton
android:id="#+id/radioAccepted"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Accettato"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/Green"/>
<RadioButton
android:id="#+id/radioRefused"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rifiutato"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/Red"/>
</RadioGroup>
</LinearLayout>
Dialog impl
private Dialog createDialog(Context context, final int code, ContractItem item,final int selected){ //type: refused, accepted
d = new Dialog(context);
d.setTitle(item.getDescItem());
d.setContentView(R.layout.layout_dialog_prop);
d.getWindow().setLayout(900, LayoutParams.WRAP_CONTENT);
d.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
Button btnOK = (Button)d.findViewById(R.id.buttonPropOK);
Button btnCancel = (Button)d.findViewById(R.id.buttonPropCancel);
btnCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
d.dismiss();
}
});
btnOK.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
d.dismiss();
}
});
return d;
}
Dialog layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="50dp" >
<EditText
android:id="#+id/editTextDiscount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:ems="10"
android:hint="Sconto proposto"
android:inputType="number"
android:textAppearance="?android:attr/textAppearanceLarge" >
<requestFocus />
</EditText>
<Spinner
android:id="#+id/spinnerScuse"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:visibility="gone" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="30dp"
android:ems="10"
android:gravity="top"
android:hint="Note"
android:inputType="textMultiLine"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<View
android:id="#+id/view2"
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginTop="30dp"
android:background="#android:color/holo_blue_light" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/buttonPropCancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:color/transparent"
android:text="Annulla"
android:textColor="#android:color/holo_blue_light" />
<View
android:id="#+id/view1"
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#android:color/holo_blue_light" />
<Button
android:id="#+id/buttonPropOK"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:color/transparent"
android:text="Ok"
android:textColor="#android:color/holo_blue_light" />
</LinearLayout>
</LinearLayout>
You are partially aware of the list-item view recycling process.
The ListView component doesn't generate view for each item in the list. So, there won't be 15 of them in your case. There will be just as many as can fit on the screen. When you scroll the list, the old items, which are no longer visible, are recycled. The getView is then called with convertView != null and the adapter is giving you an opportunity to update this recycled item view. This is done for performance reasons - just imagine an adapter having 10000 items (not a rare thing in commercial applications). Should it create all 10000 list item views? Imagine the performance you would have while scrolling such a list...
In your getView() code, you update an item view only partially - you always set item title in this line:
cih.getTvItemTitle().setText(ci.getDescItem());
When an fresh item view is created (i.e. the convertView == null) the radio group has a default selection, which may be fine in your case.
However, when the item view is recycled (i.e. the convertView != null), then you:
set a change listener in this line:
cih.getRgItemStatus().setOnCheckedChangeListener(...);
set item title:
cih.getTvItemTitle().setText(ci.getDescItem());
But you never set the checked radio group item. That means, it will have a value which was last set for this instance of item view - not for that position. You should store that information - probably in ContractItem, update it when the radio group item is selected and finally - retrieve it when convertView != null and set selected item of the radio group to the correct value.
You probably see this defect when you open up a dialog - the visible area of a ListView becomes smaller as the soft keyboard opens. This causes the ListView to remove unnecessary (technically: no longer visible) item views. When you hide the soft keyboard, the ListView area becomes larger again thus causing it to create missing item views. Unfortunately, you don't save and restore the last selected item of the radio group and so, after creation the newly visible items have the default item selected in the radio group.
I need to insert GroupData and ChildData directly into ExpandableListView in xml file itself without using java. Java file should only for showing xml file not for inserting data.
Our output will be :
Group 1 -> Child 11, Child 12, Child 13
Group 2 -> Child 21, Child 22, Child 23
Group 3 -> Child 31, Child 32, Child 33
Thanks in Advance
I don't think so it is possible . You must have to write code for generating listview using java file.
Look Here for shortest way to generate expandable listview.
If you don't want to use ExpandableListview because of java code to set adapter and all then You can use a cardview which is expand and collapse.
This is the simplest way for static(XML) collapse and expand feature.
<android.support.v7.widget.CardView
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:elevation="3dp"
app:cardCornerRadius="0dp">
<LinearLayout
android:id="#+id/group_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/group_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:lines="1"
android:text="Group 1"
android:textStyle="bold" />
<LinearLayout
android:id="#+id/child_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:orientation="vertical">
<TextView
android:id="#+id/child_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:lines="1"
android:text="Child 11" />
<TextView
android:id="#+id/child_two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:lines="1"
android:text="Child 12" />
<TextView
android:id="#+id/child_three"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:lines="1"
android:text="Child 13" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
Write the following code in onClickListener
cardView = (CardView) findViewById(R.id.card_view);
childContainer = (LinearLayout) findViewById(R.id.child_container);
boolean flag = false;
cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (flag == true) {
TransitionManager.beginDelayedTransition(cardView);
childContainer.setVisibility(View.GONE);
flag = false;
} else if (flag == false) {
TransitionManager.beginDelayedTransition(cardView);
childContainer.setVisibility(View.VISIBLE);
flag = true;
}
}
});
This is for one group and you can duplicate this code for more groups.