0
MainActivity.java
public class MainActivity extends AppCompatActivity {

ExpandableListView expandableListView;
ExpandableListAdapter expandableListAdapter;
List<String> expandableListTitle;
HashMap<String, List<String>> expandableListDetail;

@Override
protected void onCreate(Bundle savedInstanceState) {
    try {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
        //expandableListDetail = ExpandableListDataPump.getData();
        //expandableListTitle = new ArrayList<String>(expandableListDetail.keySet());
        expandableListAdapter = new CustomExpandableListAdapter(this, expandableListTitle, expandableListDetail);
        expandableListView.setAdapter(expandableListAdapter);
        expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {

            @Override
            public void onGroupExpand(int groupPosition) {
                Toast.makeText(getApplicationContext(),
                        expandableListTitle.get(groupPosition) + " List Expanded.",
                        Toast.LENGTH_SHORT).show();
            }
        });

        expandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {

            @Override
            public void onGroupCollapse(int groupPosition) {
                Toast.makeText(getApplicationContext(),
                        expandableListTitle.get(groupPosition) + " List Collapsed.",
                        Toast.LENGTH_SHORT).show();

            }
        });

        expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                                        int groupPosition, int childPosition, long id) {
                Toast.makeText(
                        getApplicationContext(),
                        expandableListTitle.get(groupPosition)
                                + " -> "
                                + expandableListDetail.get(
                                expandableListTitle.get(groupPosition)).get(
                                childPosition), Toast.LENGTH_SHORT
                ).show();
                return false;
            }
        });
    }catch (Exception e)
    {
        e.printStackTrace();
        Log.e("/Test","/Excp due to"+e.toString());
    }
}
}

CustomExpandableListAdapter.java

import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.EditText;
import android.widget.TextView;
public class CustomExpandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> expandableListTitle;
private HashMap<String, List<String>> expandableListDetail;

public CustomExpandableListAdapter(Context context, List<String> 
expandableListTitle,
HashMap<String, List<String>> 
expandableListDetail) {
try {
this.context = context;
this.expandableListTitle = expandableListTitle;
this.expandableListDetail = expandableListDetail;
}catch(Exception e)
{
e.printStackTrace();
Log.e("/Test","/Excp due to"+e.toString());
}
}

@Override
public Object getChild(int listPosition, int expandedListPosition) {
return 
this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
.get(expandedListPosition);
}

@Override
public long getChildId(int listPosition, int expandedListPosition) {
return expandedListPosition;
}

@Override
public View getChildView(int listPosition, final int expandedListPosition,
                         boolean isLastChild, View convertView, ViewGroup 
parent) {
try {
final String expandedListText = (String) getChild(listPosition, 
expandedListPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_item, null);
}
EditText expandedListEditText = (EditText) convertView
.findViewById(R.id.expandedListItem);
expandedListEditText.setText(expandedListText);
return convertView;
}catch (Exception e)
{
e.printStackTrace();
Log.e("/Test","/Excp due to"+e.toString());
return null;
}
}

@Override
public int getChildrenCount(int listPosition) {
return 
this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
.size();
}

@Override
public Object getGroup(int listPosition) {
    return this.expandableListTitle.get(listPosition);
}

@Override
public int getGroupCount() {
return this.expandableListTitle.size();
}

@Override
public long getGroupId(int listPosition) {
return listPosition;
}

@Override
public View getGroupView(int listPosition, boolean isExpanded,
                         View convertView, ViewGroup parent) {
    try {
        String listTitle = (String) getGroup(listPosition);
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) this.context.
                    getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.list_group, null);
        }
        TextView listTitleTextView = (TextView) convertView
                .findViewById(R.id.listTitle);
        listTitleTextView.setTypeface(null, Typeface.BOLD);
        listTitleTextView.setText(listTitle);
        return convertView;
    }catch (Exception e)
    {
        e.printStackTrace();
        Log.e("/Test","/Excp due to"+e.toString());
        return null;
    }
}

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

@Override
public boolean isChildSelectable(int listPosition, int expandedListPosition) {
    return true;
}
}

I am trying to add EditText to Expandablelistview but not TextViews. when I am adding adapter to Expandablelistview, Its giving null pointer exception beacause extendableListDetail has nothing. How to overcome this. Tell me how to add Edittexts to expandable listviews. please help me.This is the exception I am getting while debugging code.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
sagar
  • 17
  • 9

0 Answers0