How to Delete Item From Listview on Android - java

I want to delete item when I long clicked. First ı want to get position and then ı want to remove any item.
My MainActivity
public class MainActivity extends AppCompatActivity {
final List<Kisi> kisiler = new ArrayList<Kisi>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
kisiler.add(new Kisi("Ahmet Yılmaz", false, 0));
kisiler.add(new Kisi("Ayşe Küçük", true, 1));
kisiler.add(new Kisi("Fatma Bulgurcu", true, 2));
kisiler.add(new Kisi("İzzet Altınmeşe", false, 3));
kisiler.add(new Kisi("Melek Subaşı", true, 4));
kisiler.add(new Kisi("Selim Serdilli", false, 5));
kisiler.add(new Kisi("Halil İbrahim", false, 6));
final ListView listemiz = (ListView) findViewById(R.id.liste);
final ozelAdapter adaptorumuz = new ozelAdapter(this, kisiler);
listemiz.setAdapter(adaptorumuz);
}
}
My Adapter:
package com.endroidteam.customlistview;
public class ozelAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private List<Kisi> mKisiListesi;
public ozelAdapter(Activity activity, List<Kisi> kisiler) {
mInflater = (LayoutInflater) activity.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
mKisiListesi = kisiler;
}
#Override
public int getCount() {
return mKisiListesi.size();
}
#Override
public Object getItem(int position) {
return mKisiListesi.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View satirView;
satirView = mInflater.inflate(R.layout.satir, null);
TextView textView =
(TextView) satirView.findViewById(R.id.isimsoyisim);
ImageView imageView =
(ImageView) satirView.findViewById(R.id.simge);
Kisi kisi = mKisiListesi.get(position);
textView.setText(kisi.getIsim());
if (kisi.isKadinMi()) {
imageView.setImageResource(R.drawable.android_icon_big_13);
}
else {
imageView.setImageResource(R.drawable.android_icon_big_4);
}
return satirView;
}
}
My GetterSetter:
private String isim;
private boolean kadinMi;
private int konum;
public Kisi(String isim, boolean kadinMi, int konum) {
super();
this.isim = isim;
this.kadinMi = kadinMi;
this.konum = konum;
}
#Override
public String toString() {
return isim;
}
public String getIsim() {
return isim;
}
public void setIsim(String isim) {
this.isim = isim;
}
public int getKonum() {
return konum;
}
public void setKonum(int konum) {
this.konum = konum;
}
public boolean isKadinMi() {
return kadinMi;
}
public void setKadinMi(boolean kadinMi) {
this.kadinMi = kadinMi;
}
}
Please help me how can I remove the selected item from the following listview:

I assume you want to remove an item on the list when it gets button press - In the function where the button press gets called, get item position and remove the item like so:
this.getListView().setLongClickable(true);
this.getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
//Do some
return true;
}
});
kissiler.remove(position)
and then call
adapter.notifyDataSetChanged()
That will solve your problem.
Full code:
this.getListView().setLongClickable(true);
this.getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
//Do some
kissiler.remove(position);
adapter.notifyDataSetChanged();
return true;
}});
EDIT: I made a terrible mistake and used id instead of position - obviously you'll have to use the position variable in the functions parameters as it gives an integer where the item is located in your listview.

remove the elements from your adapter and notify the listView with
adapter.notifyDataSetChanged()

Related

onClick event badly managed with three recyclerviews

I'm displaying three recyclerviews inside a fragment. I have recently changed my implementation so I can display some feature on a dropdown menu while long pressing on an item inside one of the three recyclerview.
Since then my app treats the click event on the two first recyclerview incorrectly (out of bound issue) and I know from debbuging that those click events are managed onely by the third adapter.
Here's my fragment code displaying the three recyclerviews:
public class ActivityFragment extends Fragment {
private int Index;
private ForecastRecyclerAdapter FirstView_Adapter;
private ForecastRecyclerAdapter SecondView_Adapter;
private ForecastRecyclerAdapter ThirdView_Adapter;
private CardView FirstCard, SecondCard, ThirdCard;
private LinearLayout SeparatorLayout1, SeparatorLayout2;
private ArrayList<MainViewArrayListRecycler> ListFirstView;
private ArrayList<MainViewArrayListRecycler> ListSecondView;
private ArrayList<MainViewArrayListRecycler> ListThirdView;
public static ActivityFragment newInstance(int Index) {
Bundle args = new Bundle();
ActivityFragment fragment = new ActivityFragment();
args.putInt(GETINDEX, Index);
fragment.setArguments(args);
return fragment;
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View mview = inflater.inflate(R.layout.fragment_mainactivity_forecast, container, false);
if (getArguments() != null) {
Index = getArguments().getInt(GETINDEX);
}
ViewModel viewModel = new ViewModelProvider(this).get(ViewModel.class);
viewModel.getInfo().observe(getViewLifecycleOwner(), Informations -> {
if (Informations != null) {
FillUpArray(Informations.get(Index));
}
});
FirstCard = mview.findViewById(R.id.FirstCard);
SecondCard = mview.findViewById(R.id.SecondCard);
ThirdCard = mview.findViewById(R.id.ThirdCard);
SeparatorLayout1 = mview.findViewById(R.id.SeparatorLayout1);
SeparatorLayout2 = mview.findViewById(R.id.SeparatorLayout2);
RecyclerView FirstView = mview.findViewById(R.id.First_Inside_Recycler);
RecyclerView SecondView = mview.findViewById(R.id.Second_Inside_Recycler);
RecyclerView ThirdView = mview.findViewById(R.id.Third_Inside_Recycler);
FirstView_Adapter = new ForecastRecyclerAdapter();
SecondView_Adapter = new ForecastRecyclerAdapter();
ThirdView_Adapter = new ForecastRecyclerAdapter();
FirstView.setLayoutManager(new LinearLayoutManager(getContext()));
SecondView.setLayoutManager(new LinearLayoutManager(getContext()));
ThirdView.setLayoutManager(new LinearLayoutManager(getContext()));
FirstView.setAdapter(FirstView_Adapter);
SecondView.setAdapter(SecondView_Adapter);
ThirdView.setAdapter(ThirdView_Adapter);
FirstView_Adapter.setOnItemClickListener(new ActivityFragmentAdapter.ActivityFragmentAdapterListener() {
#Override
public void onFirstClick(int position) {
//...
}
#Override
public void onSecondClick(int position) {
//...
}
#Override
public void onThirdClick(int position, boolean isChecked) {
if (isChecked) {
//...
}
}
});
SecondView_Adapter.setOnItemClickListener(new ActivityFragmentAdapter.ActivityFragmentAdapterListener() {
#Override
public void onFirstClick(int position) {
//...
}
#Override
public void onSecondClick(int position) {
//...
}
#Override
public void onThirdClick(int position, boolean isChecked) {
if (isChecked) {
//...
}
}
});
ThirdView_Adapter.setOnItemClickListener(new ActivityFragmentAdapter.ActivityFragmentAdapterListener() {
#Override
public void onFirstClick(int position) {
//...
}
#Override
public void onSecondClick(int position) {
//...
}
#Override
public void onThirdClick(int position, boolean isChecked) {
if (isChecked) {
//...
}
}
});
return mview;
}
private void FillUpArray(Information Info) {
/* Fill up the different arraylist and call setData frome the adapter */
}
}
Here's the recycler adapter class used:
public class ActivityFragmentAdapter extends RecyclerView.Adapter<ActivityFragmentAdapter.InsideRecyclerHolder> {
private ArrayList<MainViewArrayListRecycler> mRecyclerArrayListType;
private static ActivityFragmentAdapterListener mListener;
private String mUnit;
private int mAverageDistance;
private int mCurrentDistance;
private int mPeriodInterval;
public void setData(ArrayList<MainViewArrayListRecycler> RecyclerArrayListType) {
mRecyclerArrayListType = RecyclerArrayListType;
notifyDataSetChanged();
}
#NonNull
#Override
public InsideRecyclerHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater myInflater = LayoutInflater.from(parent.getContext());
View myOwnView = myInflater.inflate(R.layout.forecast_recycler, parent, false);
return new InsideRecyclerHolder(myOwnView, mListener);
}
#Override
public void onBindViewHolder(#NonNull InsideRecyclerHolder holder, int position) {
MainViewArrayListRecycler currentItem = mRecyclerArrayListType.get(position);
String tmpConcatDistance;
holder.t1.setText(currentItem.getTitle()));
holder.t2.setText(currentItem.getText());
}
#Override
public int getItemCount() {
return mRecyclerArrayListType.size();
}
public static class InsideRecyclerHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener, MenuItem.OnMenuItemClickListener{
final TextView t1;
final TextView t2;
final CheckBox cb1;
public InsideRecyclerHolder(#NonNull View itemView, final ForecastRecyclerAdapterListener listener) {
super(itemView);
t1 = itemView.findViewById(R.id.t1);
t2 = itemView.findViewById(R.id.t2);
cb1 = itemView.findViewById(R.id.cb1);
cb1.setOnCheckedChangeListener((buttonView, isChecked) -> {
if (listener != null) {
int position = getBindingAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
listener.onThirdClick(position, isChecked);
cb1.setChecked(false);
}
}
});
itemView.setOnCreateContextMenuListener(this);
}
#Override
public boolean onMenuItemClick(MenuItem item) {
if (mListener != null) {
int position = getBindingAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
switch (item.getItemId()) {
case 1:
mListener.onFirstClick(position);
return true;
case 2:
mListener.onSecondClick(position);
return true;
}
}
}
return false;
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
menu.setHeaderTitle(R.string.dropdown_menu_title);
MenuItem firstItem = menu.add(Menu.NONE, 1, 1, R.string.dropdown_menu_firstItem);
MenuItem secondItem = menu.add(Menu.NONE, 2, 2, R.string.dropdown_menu_secondItem);
firstItem.setOnMenuItemClickListener(this);
secondItem.setOnMenuItemClickListener(this);
}
}
public interface ActivityFragmentAdapterListener {
void onFirstClick(int position);
void onSecondClick(int position);
void onThirdClick(int position, boolean isChecked);
}
public void setOnItemClickListener(ActivityFragmentAdapterListener listener) {
mListener = listener;
}
}
What can be the issue ?

Android ListView with Switch buttons - only one switch active at a time

I have a list view with two switches. I want the functionality to work were only one switch may be active at a time.
--UPDATED
My Adapter:
public class NotificationsAdapter extends ArrayAdapter<String> {
private Context context;
private String mTitle[];
private boolean onOff[];
public NotificationsAdapter(Context c, String mTitle[], boolean onOff[]) {
super(c, R.layout.adapter_notifications_layout, R.id.notificationsListTv, mTitle);
this.context = c;
this.mTitle = mTitle;
this.onOff = onOff;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.adapter_notifications_layout, parent, false);
Switch notificationSwitch = view.findViewById(R.id.switchNotificationsDaily);
TextView myTitle = view.findViewById(R.id.notificationsListTv);
myTitle.setText(mTitle[position]);
notificationSwitch.setChecked(onOff[position]);
view.setClickable(true);
view.setFocusable(true);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int size = onOff.length;
for (int i = 0; i < size; i++) {
if (i == position) {
break;
}
onOff[i] = false;
}
if (onOff[position]) {
notificationSwitch.setChecked(false);
onOff[position] = false;
} else {
onOff[position] = true;
notificationSwitch.setChecked(true);
}
}
});
return view;
}
My Class:
private String[] mTitle = new String[]{"Once Daily", "Twice Daily"};
private Switch notificationSwitch;
private boolean[] onOff = new boolean[] {false, false};
NotificationsAdapter notificationsAdapter = new NotificationsAdapter(getActivity(), mTitle, onOff);
listView = view.findViewById(R.id.notificationsListView);
listView.setAdapter(notificationsAdapter);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
listView.setItemsCanFocus(true);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
}
Currently with this code I can select both switches to be active at the same time. When I select more than one switch I would like the other to deactivate. Any assistance getting this functionality to work would be greatly appreciated.
In your activity:-
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
resetValues(position);
if (onOff[position]) {
onOff[position] = false;
} else {
onOff[position] = true;
}
notifyDataSetChanged();
}
}
private void resetValues(int selectedPosition){
int size = onOff.length;
for(int i=0; i < size; i++){
if(i == selectedPosition){
continue;
}
onOff[i] = false;
}
}
And in your adapter:-
notificationSwitch.setChecked(onOff[position]);
Also, remove your click listener from adapter.
The logic is:- Making all other values as false except the selected item, then changing the state of the selected item based on its previous state.

How to add new item on button press in Base Adapter

I'm trying to crate something that creates a new ListView item on button press, I thing I have somewhere some bug, but as I'm new to this topic I don't know where.
I've tried rewriting the code several times, I've tried to use notifyDataSetChanged(); - it does nothing
tried googling looking on other topics here...
Here is my MainActivity.java:
public Button btn;
private ListView lv;
private CustomeAdapter customeAdapter;
public ArrayList<EditModel> editModelArrayList;
int populateListMaxNum =3;
int listNumber = populateListMaxNum;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.listView);
btn = (Button) findViewById(R.id.btn);
editModelArrayList = populateList();
customeAdapter = new CustomeAdapter(this,editModelArrayList);
lv.setAdapter(customeAdapter);
/* TODO activate button */
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
addToList();
Toast.makeText(getApplicationContext(), "button", Toast.LENGTH_LONG).show();
}
});
}
private ArrayList<EditModel> populateList(){ //this part works perfectly
ArrayList<EditModel> list = new ArrayList<>();
for(int i = 0; i < populateListMaxNum; i++){
EditModel editModel = new EditModel();
//editModel.setEditTextValue(String.valueOf(i));
list.add(editModel);
}
return list;
}
/*TODO make it work = expand */
private void addToList(){ // this part doesn't work nor it doesn't fail
EditModel editModel = new EditModel();
editModelArrayList.add(editModel);
customeAdapter.notifyDataSetChanged();
}
}
Here is my CustomeAdapter.java class:
public class CustomeAdapter extends BaseAdapter {
private Context context;
public static ArrayList<EditModel> editModelArrayList;
public CustomeAdapter(Context context, ArrayList<EditModel> editModelArrayList) {
this.context = context;
CustomeAdapter.editModelArrayList = editModelArrayList;
}
#Override
public int getViewTypeCount() {
return getCount();
}
#Override
public int getItemViewType(int position) {
return position;
}
#Override
public int getCount() {
return editModelArrayList.size();
}
#Override
public Object getItem(int position) {
return editModelArrayList.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.lv_item, null, true);
holder.editText = convertView.findViewById(R.id.editid);
convertView.setTag(holder);
}else {
// the getTag returns the viewHolder object set as a tag to the view
holder = (ViewHolder)convertView.getTag();
}
holder.editText.setText(editModelArrayList.get(position).getEditTextValue());
holder.editText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
editModelArrayList.get(position).setEditTextValue(holder.editText.getText().toString());
}
#Override
public void afterTextChanged(Editable editable) {
}
});
return convertView;
}
private class ViewHolder {
protected EditText editText;
}
}
I expect to create a new list item (EditText + TextView) but nothing happens (except the Toast message)
(After some tweaks the app crashes due to arrayindexoutofboundsexception length=3 index=3 error, but not in this setting)
here are up to all files nessessary: https://gist.github.com/Atingas/52778a247a78131e5b8cb0239fa30965
Main linear layout in lv_item.xml has match_parent height. Try change it to wrap_content. It seems like one row is just taking whole screen.

ListView itemId is always 0

The value returned by id parameter of setOnItemClickListener is always 0(zero) no matter which position i click on the value returned is always 0
but the value of position of correct and i have also tried adapterView.getItemIdAtPosition(i) and still the value on clicking each item in list is zero
public class ListFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor>{
ListView onlineList;
FloatingActionButton onlineListFab;
private static final int mLoaderId = 1;
ListAdapter listAdapter;
public ListFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_list, container, false);
initilize(v);
return v;
}
private void initilize(View v) {
onlineList = (ListView)v.findViewById(R.id.onlineList);
onlineListFab = (FloatingActionButton)v.findViewById(R.id.onlineListFab);
onlineListFab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getActivity(),AddItem.class));
}
});
listAdapter = new ListAdapter(getActivity(),null);
onlineList.setAdapter(listAdapter);
onlineList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent details = new Intent(getActivity(),AddItem.class);
details.setData(Uri.withAppendedPath(TableName.mContentUri,String.valueOf(l)));
Toast.makeText(getActivity(),l+"",Toast.LENGTH_SHORT).show();
//startActivity(details);
}
});
loadItems();
}
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
switch (id){
case mLoaderId:
return new CursorLoader(getContext(), TableName.mContentUri,null,null,null,null);
}
return null;
}
#Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
listAdapter.swapCursor(data);
}
#Override
public void onLoaderReset(Loader<Cursor> loader) {
listAdapter.swapCursor(null);
}
private void loadItems() {
if (getActivity().getSupportLoaderManager().getLoader(mLoaderId) == null) {
getActivity().getSupportLoaderManager().initLoader(mLoaderId, null, this).forceLoad();
} else {
getActivity().getSupportLoaderManager().restartLoader(mLoaderId, null, this).forceLoad();
}
}
}
AdapterCode :
public class ListAdapter extends CursorAdapter{
public ListAdapter(Context context, Cursor c) {
super(context, c);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v = LayoutInflater.from(context).inflate(R.layout.item_layout,parent,false);
return v;
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
TextView name,price;
name = (TextView)view.findViewById(R.id.itemName);
price = (TextView)view.findViewById(R.id.itemPrice);
name.setText(cursor.getString(cursor.getColumnIndex(TableName.table1.mName)));
price.setText(String.valueOf(cursor.getInt(cursor.getColumnIndex(TableName.table1.mPrice))));
}
}
You're doing new ListAdapter(getActivity(),null); which is a custom class seeing as you can't do that with an interface. Have you updated the default getItemId method? I'd gamble not.
Off the top of my head, your adapter will need an implementation similar to:
#Override
public long getItemId(int position) {
long result = 0;
if (mCursor.moveToPosition(position)) {
Log.d("SO", DatabaseUtils.dumpCurrentRowToString(mCursor);
result = mCursor.getLong(mCursor.getColumnIndex(TableName.table1.mId));
}
return result;
}
If you can't resolve the problem take a look at the data itself in your cursors using DatabaseUtils

Onclick listner to expandable listview

I have created an expandable listview, but the onclick listener to the child list items could not be attached.
The activity code:
public class MyActivity extends Activity {
private ExpandableListView mExpandableList;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mExpandableList = (ExpandableListView)findViewById(R.id.expandable_list);
ArrayList<Parent> arrayParents = new ArrayList<Parent>();
ArrayList<String> arrayChildren = new ArrayList<String>();
//here we set the parents and the children
for (int i = 0; i < 2; i++){
//for each "i" create a new Parent object to set the title and the children
Parent parent = new Parent();
parent.setTitle("Parent " + i);
arrayChildren = new ArrayList<String>();
for (int j = 0; j < 3; j++) {
arrayChildren.add("Child " + j);
}
parent.setArrayChildren(arrayChildren);
//in this array we add the Parent object. We will use the arrayParents at the setAdapter
arrayParents.add(parent);
}
//sets the adapter that provides data to the list.
mExpandableList.setAdapter(new MyCustomAdapter(MyActivity.this,arrayParents));
}
}
The custom adapter for the lists:
public class MyCustomAdapter extends BaseExpandableListAdapter {
private LayoutInflater inflater;
private ArrayList<Parent> mParent;
public MyCustomAdapter(Context context, ArrayList<Parent> parent){
mParent = parent;
inflater = LayoutInflater.from(context);
}
#Override
//counts the number of group/parent items so the list knows how many times calls getGroupView() method
public int getGroupCount() {
return mParent.size();
}
#Override
//counts the number of children items so the list knows how many times calls getChildView() method
public int getChildrenCount(int i) {
return mParent.get(i).getArrayChildren().size();
}
#Override
//gets the title of each parent/group
public Object getGroup(int i) {
return mParent.get(i).getTitle();
}
#Override
//gets the name of each item
public Object getChild(int i, int i1) {
return mParent.get(i).getArrayChildren().get(i1);
}
#Override
public long getGroupId(int i) {
return i;
}
#Override
public long getChildId(int i, int i1) {
return i1;
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
//in this method you must set the text to see the parent/group on the list
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
if (view == null) {
view = inflater.inflate(R.layout.list_item_parent, viewGroup,false);
}
TextView textView = (TextView) view.findViewById(R.id.list_item_text_view);
//"i" is the position of the parent/group in the list
textView.setText(getGroup(i).toString());
//return the entire view
return view;
}
#Override
//in this method you must set the text to see the children on the list
public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
if (view == null) {
view = inflater.inflate(R.layout.list_item_child, viewGroup,false);
}
TextView textView = (TextView) view.findViewById(R.id.list_item_text_child);
//"i" is the position of the parent/group in the list and
//"i1" is the position of the child
textView.setText(mParent.get(i).getArrayChildren().get(i1));
//return the entire view
return view;
}
#Override
public boolean isChildSelectable(int i, int i1) {
return true;
}
#Override
public void registerDataSetObserver(DataSetObserver observer) {
/* used to make the notifyDataSetChanged() method work */
super.registerDataSetObserver(observer);
}
}
The parent class:
public class Parent {
private String mTitle;
private ArrayList<String> mArrayChildren;
public String getTitle() {
return mTitle;
}
public void setTitle(String mTitle) {
this.mTitle = mTitle;
}
public ArrayList<String> getArrayChildren() {
return mArrayChildren;
}
public void setArrayChildren(ArrayList<String> mArrayChildren) {
this.mArrayChildren = mArrayChildren;
}
}
What should I do to add onclick listener to the child list items?
Add this after you setAdapter of the Expandable list
mExpandableList.setOnChildClickListener(new OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,int groupPosition, int childPosition, long id) {
/* You must make use of the View v, find the view by id and extract the text as below*/
TextView tv= (TextView) v.findViewById(R.id.childTextView);
String data= tv.getText().toString();
return true; // i missed this
}
});
You need to add
ChildClickListener
like this : mExpandableList.setOnChildClickListener
add this line to onCreate method
read here
also this is a good example
Declare your listView with an overridden onChildClick
ExpandableListView listView = getExpandableListView();
listView.setChoiceMode(ExpandableListView.CHOICE_MODE_SINGLE);
listView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,int groupPosition, int childPosition,long id) {
Log.d(TAG,"I got clicked childPosition:["+childPosition+"] groupPosition:["+groupPosition+"] id:["+id+"]");
return true;
}
});

Categories

Resources