in the App im developing, i got a List of messages with titel datum etc.
When clicking on the Messagetitel i want to sliding expand this entry and show the Details, and some new buttons like "reply" , "delete" and "forward".
like:
- message
- message (clicked message)
details buttons delete , forward , reply
text
- message
- message
Here is what i did so far:
first my Model Class for the messages:
public class MyMessage {
private String from;
private String subject;
private String date;
private String message;
public MyMessage(String f, String s, String d, String m){
from= f;
subject= s;
date = d;
message= m;
}
public String getFrom() {
return from;
}
public String getSubject() {
return subject;
}
public String getDate() {
return date;
}
public String getMessage() {
return message;
}
}
my Custom Adapter:
public class NewAdapter extends BaseExpandableListAdapter {
public ArrayList<MyMessage> mMessage;
public LayoutInflater minflater;
public Activity activity;
public NewAdapter(ArrayList<MyMessage> message){
mMessage = message;
}
public void setInflater(LayoutInflater mInflater, Activity act) {
this.minflater = mInflater;
activity = act;
}
#Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
TextView text = null;
// TODO Auto-generated method stub
if (convertView == null) {
convertView = minflater.inflate(R.layout.message_details, null);
}
text = (TextView) convertView.findViewById(R.id.tv_message_text);
text.setText(mMessage.get(childPosition).getMessage());
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return 0;
}
#Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return null;
}
#Override
public int getGroupCount() {
// TODO Auto-generated method stub
return 0;
}
#Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
TextView from= null;
TextView date = null;
TextView subject= null;
// TODO Auto-generated method stub
if(convertView == null){
convertView = minflater.inflate(R.layout.message_head, null);
}
from= (TextView) convertView.findViewById(R.id.tv_from);
date = (TextView) convertView.findViewById(R.id.tv_date);
subject= (TextView) convertView.findViewById(R.id.tv_subject);
from.setText(mMessage.get(groupPosition).getfrom());
date.setText(mMessage.get(groupPosition).getDate());
subject.setText(mMessage.get(groupPosition).getSubject());
return convertView;
}
#Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return false;
}
}
my main Class:
public class Test extends ExpandableListActivity{
public ArrayList<MyMessage> message;
public ExpandableListView eListView;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
eListView = getExpandableListView();
message = createMessages();
NewAdapter nAdapter = new NewAdapter(message);
nAdapter
.setInflater(
(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE),
this);
getExpandableListView().setAdapter(nAdapter);
}
private ArrayList<MyMessage> createMessages() {
ArrayList<MyMessage> m = new ArrayList<MyMessage>();
MyMessage test1 = new MyMessage("test1", "testbetreff", "08.11.2013 / 09:44", "gaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaanz viel text und so");
MyMessage test2 = new MyMessage("test2", "testbetreff", "08.11.2013 / 09:45", "gaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaanz viel text und so gaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaanz viel text und so");
MyMessage test3 = new MyMessage("test3", "testbetreff", "08.11.2013 / 09:46", "gaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaanz viel text und so gaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaanz viel text und so gaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaanz viel text und so");
MyMessage test4 = new MyMessage("test4", "testbetreff", "08.11.2013 / 09:47", "gaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaanz viel text und so gaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaanz viel text und so gaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaanz viel text und so gaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaanz viel text und so");
m.add(test1);
m.add(test2);
m.add(test3);
m.add(test4);
return m;
}
}
i dont get any errors but my activity is blank. I tried to debug this but it never calls getchildview() or getgroupview() and i dont know why.
am im doing any big mistakes with the costum adapter ?
thanks in advance
#Override
public int getChildrenCount(int groupPosition) {
return 1; //just one children?
}
#Override
public Object getGroup(int groupPosition) {
return mMessage.get(groupPosition);
}
#Override
public int getGroupCount() {
return mMessage.size();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
Try this... maybe it's not called because you said that the adapter has no items at all...
Related
I'm not expert in android listview and Edittext. However, I am stuck with some annoying problem regarding this issue. I am fetching values in listview without any problem but when I input something in Edittext and scroll down the value of EditText changes it's position. Here is my code.
//product class
public class products {
public String prod_sl;
public String prod_code;
public String product_name;
public String product_desc;
public String prod_qnty;
public String prod_uom;
public String prod_price;
boolean ShowName;
public products(String psl, String pcode,String Name, String Desc, String UOM) {
this.prod_sl = psl;
prod_code = pcode;
prod_qnty="";
prod_price ="";
product_name=Name;
product_desc=Desc;
prod_uom =UOM;
}
/* public boolean isShowName() {
return ShowName;
}
public void setShowName(boolean showName) {
ShowName = showName;
}*/
//sl
public String getSl() { return prod_sl; }
public void setSl(String psl) { this.prod_sl = psl; }
//product code
public String getCode() { return prod_code; }
public void setCode(String pcode) { this.prod_code = pcode; }
//product Name
public String getName() { return product_name; }
public void setName(String product_name) { this.product_name = product_name; }
//product desc
public String getDesc() { return product_desc; }
public void setDesc(String product_desc) { this.product_desc = product_desc; }
//product UOM
public String getUom() { return prod_uom; }
public void setUom(String prod_uom) { this.prod_uom = prod_uom; }
// product quantity
public String getQnty() {
return prod_qnty; }
public void setQnty(String prod_qnty) {
this.prod_qnty = prod_qnty; }
//product price
public String getPrice() {
return prod_price; }
public void setPrice(String prod_price) {
this.prod_price = prod_price; }
}
And here is the adapter class
public class ListViewAdapter extends ArrayAdapter<products> {
Context mContext;
View v;
private String[] arrTemp;
LayoutInflater inflater;
ArrayList<products> arrayproducts;
public String[] scoresToUpdate=new String[1000];
//public String Array scoresToUpdate =scoresToUpdate[];
public static EditText edit_qnty,prod_price;
public static HashMap<Integer,String> myList=new HashMap<Integer,String>();
public ListViewAdapter(Context context, int resource, ArrayList<products> arrayproducts) {
super(context, resource);
this.mContext = context;
this.arrayproducts = arrayproducts;
this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
try {
final int pos=position;
final ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_adapter_view, null);
holder = new ViewHolder();
holder.prod_sl = (TextView) convertView.findViewById(R.id.prod_sl);
holder.prod_code = (TextView) convertView.findViewById(R.id.prod_code);
holder.txtTitle = (TextView) convertView.findViewById(R.id.adapter_text_title);
holder.txtDescription = (TextView) convertView.findViewById(R.id.adapter_text_description);
holder.prod_uom = (TextView) convertView.findViewById(R.id.prod_uom);
holder.prod_qnty = (EditText) convertView.findViewById(R.id.prod_qnty);
holder.prod_price = (EditText) convertView.findViewById(R.id.prod_price);
// edit_qnty = (EditText) convertView.findViewById(R.id.prod_qnty);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
// holder.prod_qnty.setText(scoresToUpdate[pos]);
}
holder.ref = position;
products prod = arrayproducts.get(position);
holder.prod_sl.setText("" + position);
holder.prod_code.setText(prod.getCode());
holder.txtTitle.setText(prod.getName());
holder.txtDescription.setText(prod.getDesc());
holder.prod_uom.setText(prod.getUom());
Log.e("row values",""+position+"\t-"+prod.getCode()+""+prod.getName()+""+prod.getDesc()+""+prod.getUom());
// holder.prod_qnty.setText(arrTemp[position]);
holder.prod_qnty.setText(scoresToUpdate[position]);
holder.prod_qnty.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
arrayproducts.get(pos).setQnty(holder.prod_qnty.getText().toString().trim());
myList.put(pos,arg0.toString().trim());
if (!arg0.equals("0")){
scoresToUpdate[pos] = arg0.toString();
Log.e("On text Change","Pos"+pos+"\tqnty:"+holder.prod_qnty.getText().toString().trim()+"\t Args: "+arg0.toString());
}
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
Log.e("After Text change","Pos"+holder.ref+"\tqnty:"+holder.prod_qnty.getText().toString().trim());
// arrTemp[holder.ref] = arg0.toString();
}
});
//holder.prod_qnty.setText(myList.get(position));
holder.prod_qnty.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Log.e("Current Qnty",edit_qnty.getText().toString().trim());
if(holder.prod_qnty.getText().toString().trim().equals("0")){
holder.prod_qnty.setText("");
}
String inttext = (""+holder.prod_qnty.getText().toString().trim());
if (!inttext.equals("0")){
holder.prod_price.setText("");
}
return false;
//return true;
}
});
//Using setOnclickListener not setOnCheckedChangeListener
//we need to update adapter once we finish with editing
/* holder.prod_qnty.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
final int pos = v.getId();
final EditText Qnty = (EditText) v;
Log.e("Qnty For the positon","POS: "+pos+"\tQnty: "+Qnty.getText().toString().trim());
arrayproducts.get(pos).setQnty(Qnty.getText().toString().trim());
//holder.prod_qnty.setText(Caption.getText().toString().trim());
}
}
});*/
/* holder.prod_price.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
final int position = v.getId();
final EditText Caption = (EditText) v;
arrayproducts.get(position).setPrice(Caption.getText().toString().trim());
}
}
});
*/
return convertView;
}
catch(Exception e){
Toast.makeText(mContext, "!!!", Toast.LENGTH_SHORT).show();
e.printStackTrace();
Log.e("Exception:",""+e.toString());
}
return convertView;
}
/* #Override
public int getCount() {
return arrayproducts.size();
}*/
#Override
public int getCount() {
// TODO Auto-generated method stub
if(arrayproducts != null && arrayproducts.size() != 0){
return arrayproducts.size();
}
return 0;
}
/*#Override
public Objects getItem(int position) {
// TODO Auto-generated method stub
return arrayproducts[position];
}*/
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public void getQnty(){
if(edit_qnty.getText().toString().trim().equals("0"))
edit_qnty.setText("");
}
static class ViewHolder {
TextView prod_sl;
TextView prod_code;
TextView txtTitle;
TextView txtDescription;
TextView prod_uom;
EditText prod_qnty,prod_price;
TextWatcher qtyWatcher;
TextWatcher priceWatcher;
int ref;
}
}
Please help me, With regards
This works for me !
if (holder.qtyWatcher != null) {
holder.txtFourth.removeTextChangedListener(holder.qtyWatcher);
}
// Create the TextWatcher corresponding to this row
holder.qtyWatcher = new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
if(s.toString().equals("")||s.toString().equals("0")){
arrayproducts.get(position).setQnty("0");
}
else{
arrayproducts.get(position).setQnty(s.toString());
}
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
};
not sure that listview and recycleview can be used like this as they involve a recycling mechanism to reuse created views and rebind data from arrays or cursors as users scroll down/up to see more data. in other words, the row views created are with limited number to only fill the screen (with few extra from both sides) and they are recycled and reused to show data, if you insert a price or quantity for one product line, how the recycling process will rebind to show same data you inserted or show it on same position, I doubt this is possible.
I suggest that you restructure your application on parent/child base. show on list parent data, for example fixed product data, when users click one product line another screen opens so users insert new data, for example the price for that particular product line, the layout of your child screen will contains the edittext and any other data input views, in such case you need to integrate SQLite database so that you persist the new data provided by users.
hope this may provide you with some useful ideas to help you achieve your target.
When I am trying to set value to the list view using custom adapter it shows only the last entered value in the hash map. hash map is static.I dont know why I am not getting all the values in the hashmap for that keys I am used in hashmap.
here my code
public class Nextclass extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nextclass);
list = (ListView)findViewById(R.id.list1);
SelectItems();
ViewItems();
}
private List<Map<String, String>> SelectItems() {
// TODO Auto-generated method stub
try {
datas = new ArrayList<Map<String, String>>();
DatabaseHelper dbHelper = new DatabaseHelper(this.getApplicationContext());
newDB = dbHelper.getWritableDatabase();
Cursor c = newDB.rawQuery("select distinct code ,desc ,name "
+ " from item", null);
while (c.moveToNext()){
String cod = c.getString(c.getColumnIndex("code"));
datanums.put("code", cod);
String desc1 = c.getString(c.getColumnIndex("desc"));
datanums.put("desc", desc1);
String name = c.getString(c.getColumnIndex("name"));
datanums.put("name", name);
datas.add(datanums);
}
} catch (SQLiteException se ) {
Log.e(getClass().getSimpleName(), "Could not create or Open the database");
}
return datas;
}
private void ViewItems() {
// TODO Auto-generated method stub
arrTemp = new String[datas.size()];
MyListAdapter myListAdapter = new MyListAdapter();
list.setAdapter(myListAdapter);
Log.v("list itemm",datas.toString());
}
public class MyListAdapter extends BaseAdapter{
#Override
public int getCount() {
// TODO Auto-generated method stub
if(datas != null && datas.size() != 0){
return datas.size();
}
return 0;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return datas.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inflater = Orders.this.getLayoutInflater();
convertView = inflater.inflate(R.layout.order_simple_row, null);
holder.textView1 = (TextView) convertView.findViewById(R.id.name);
holder.textView2 = (TextView) convertView.findViewById(R.id.code);
holder.textview3 = (TextView) convertView.findViewById(R.id.desc);
holder.editText1 = (EditText) convertView.findViewById(R.id.cases);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.ref = position;
holder.textView1.setText(datanums.get("name"));
holder.textview3.setText(datanums.get("code"));
holder.textView2.setText(datanums.get("desc"));
holder.editText1.setText(arrTemp[position]);
holder.editText1.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
arrTemp[holder.ref] = arg0.toString();
}
});
return convertView;
}
private class ViewHolder {
public Object list;
TextView textView1,textView2,textview3;
EditText editText1,editText2;
int ref;
}
return convertView;
}
private class ViewHolder {
public Object list;
TextView textView1,textView2,textview3;
EditText editText1;
int ref;
}
please help.I used alternate methods,but not worked.thanks in advance
holder.textView1.setText(datas.get(position).get("name"));
holder.textview3.setText(datas.get(position).get("code"));
holder.textView2.setText(datas.get(position).get("desc"));
you are adding hash map in the array list but in the adapter you are trying to get data from the hash map, instead of that you need to take value from arraylist which will return hashmap object and from that object you need to get value for your keys.
hope this will help.
Problem is with checkbox. When user click on child, app should show toast message on which child user clicked. It works fine if I click on textview of child, but when i click on checkbox, nothing heppend. Also when I click on child item, I want that my checkbox change state. Do you see something that I can't see. Here is my code for main activity
private ExpandableAdapter adapter;
private ExpandableListView expandableList;
private List<Pitanja> pitanjas = new ArrayList<Pitanja>();
private ArrayList<Pitanja> listaPitanja = new ArrayList<Pitanja>();
private List<Odgovor> odgovors = new ArrayList<Odgovor>();
public static HashMap<Integer, Integer> pitanjaa;
private Intent intent;
private ProgressDialog pDialog;
private TextView output;
private List<Ispit> ispitList;
private String pozicija;
private Button posalji;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ispit);
output = (TextView) findViewById(R.id.ispit_datum);
output.setMovementMethod(new ScrollingMovementMethod());
pitanjaa = new HashMap<Integer, Integer>();
posalji = (Button) findViewById(R.id.posaljiIspitButton);
posalji.setOnClickListener(this);
generateData();
initEx();
intent = getIntent();
RequestPackage p = new RequestPackage();
p.setUri("http://arka.foi.hr/WebDiP/2012_projekti/WebDiP2012_085/AIR/ispit.php");
p.setMethod("POST");
p.setParam("id_kolegij", intent.getStringExtra("id_predmeta"));
Log.i("Saljem podatke ", intent.getStringExtra("id_predmeta"));
MyTask task = new MyTask();
task.execute(p);
}
private void updateDisplay(){
Collections.shuffle(ispitList);
output.setText(String.valueOf(ispitList.get(0).getId()));
MyRequest task = new MyRequest();//this works fine
RequestPackage r = new RequestPackage();//works
r.setMethod("POST");//works
r.setUri("http://arka.foi.hr/WebDiP/2012_projekti/WebDiP2012_085/AIR/pitanja.php");//works
r.setParam("id_kolegij", output.getText().toString());//works
task.execute(r);
}
private void initEx(){
adapter = new ExpandableAdapter(IspitActivity.this, listaPitanja);
expandableList = (ExpandableListView) findViewById(R.id.expandableIspitListView);
expandableList.setAdapter(adapter);
for (int i=0;i<adapter.getGroupCount();i++){
expandableList.collapseGroup(i);
}
expandableList.setOnChildClickListener(new OnChildClickListener(){
#Override
public boolean onChildClick(ExpandableListView arg0, View arg1,
int arg2, int arg3, long arg4) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Klikno si na " + adapter.getChild(arg2, arg3).getText() + " " + adapter.getChild(arg2, arg3).getTocan_netocan(), Toast.LENGTH_SHORT).show();
if (Integer.parseInt(adapter.getChild(arg2, arg3).getTocan_netocan()) == 0)
pitanjaa.put(arg2, arg3);
Log.i("pitanja koja si odgovorio su", pitanjaa.toString());
adapter.getChild(arg2, arg3).setSelected(true);
return true;
}
});
}
private void generateData(){
Pitanja p;
for (int i=0;i<pitanjas.size();i++){
ArrayList<Odgovor> od = new ArrayList<Odgovor>();
for (int z=0;z<odgovors.size();z++){
if (odgovors.get(z).getId_pitanja().contains(String.valueOf(pitanjas.get(i).getId()))){
od.add(odgovors.get(z));
}
}
pozicija = pitanjas.get(i).getText();
p = new Pitanja(i, pozicija, od);
listaPitanja.add(p);
}
}
private class MyTask extends AsyncTask<RequestPackage, String, List<Ispit>>{
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(IspitActivity.this);
pDialog.setMessage("Dobavljam podatke...");
pDialog.setCancelable(false);
pDialog.show();
}
#Override
protected List<Ispit> doInBackground(RequestPackage... params) {
String content = HttpManager.getData(params[0]);
ispitList = JSONParser.parseIspit(content.substring(1, content.length()-1));
Log.i("Parsirano izgleda sljedeci", content.substring(1, content.length()-1));
return ispitList;
}
#Override
protected void onPostExecute(List<Ispit> result) {
super.onPostExecute(result);
pDialog.dismiss();
updateDisplay();
}
}
#Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), PregledActivity.class);
startActivity(intent);
}
}
Here is my expandablelistview adapter
public class ExpandableAdapter extends BaseExpandableListAdapter{
LayoutInflater inflater;
private List<Pitanja> groups;
public ExpandableAdapter(Context context,List<Pitanja> groups) {
super();
this.groups=groups;
inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void addItem(Odgovor child,Pitanja group) {
if(!groups.contains(group)) {
groups.add(group);
}
int index=groups.indexOf(group);
ArrayList<Odgovor> ch=groups.get(index).getOdgovors();
ch.add(child);
groups.get(index).setOdgovors(ch);
}
public Odgovor getChild(int groupPosition, int childPosition) {
ArrayList<Odgovor> ch=groups.get(groupPosition).getOdgovors();
return ch.get(childPosition);
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public int getChildrenCount(int groupPosition) {
ArrayList<Odgovor> ch=groups.get(groupPosition).getOdgovors();
return ch.size();
}
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
Odgovor child= (Odgovor) getChild(groupPosition,childPosition);
TextView childName=null;
CheckBox cb = null;
if(convertView==null) {
convertView=inflater.inflate(R.layout.child_item, null);
}
childName=(TextView) convertView.findViewById(R.id.textview_child_item);
childName.setText(child.getText());
cb = (CheckBox) convertView.findViewById(R.id.checkbox_child_item);
if (child.isSelected())
cb.setChecked(true);
return convertView;
}
public Pitanja getGroup(int groupPosition) {
return groups.get(groupPosition);
}
public int getGroupCount() {
return groups.size();
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
TextView groupName = null;
Pitanja group=(Pitanja) getGroup(groupPosition);
if(convertView==null) {
convertView=inflater.inflate(R.layout.parent_item, null);
}
groupName=(TextView) convertView.findViewById(R.id.parent_item);
groupName.setText(group.getText());
return convertView;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public boolean hasStableIds() {
return true;
}
}
my child view
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<CheckBox
android:id="#+id/checkbox_child_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false" />
<TextView
android:id="#+id/textview_child_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
If I changle focusable of my checkbox, it doesn't work too. Someone have idea
The CheckBox will consume the click event before passing it on to the ExpandableListView click event. You'll need to manually attached a click listener to the CheckBox in the getChildView() method if you want to see those click events.
I have an expandablelistview which contain n number of group's. Each group contains single child that contains diffrent counts of textview like , Group 1 s child contains 10 textview ,
Groups 2 child contains 4 textview etc. My issue is that when i show notes(Textviews) of Group 1 on expand Groups 2 child also showing the same notes. Can any one tell me how to differentiate between groups child or add different items to different child's ?
Sorry i cant paste the full code here
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context mContext;
private ExpandableListView mExpandableListView;
private List<GroupEntity> mGroupCollection;
private int[] groupStatus;
private ArrayList<HashMap<String, String>> data;
public ExpandableListAdapter(Context pContext,
ExpandableListView pExpandableListView,
List<GroupEntity> pGroupCollection, ArrayList<HashMap<String, String>> d, String jobID2 ,String userId, String totalTasks) {
mContext = pContext;
mGroupCollection = pGroupCollection;
mExpandableListView = pExpandableListView;
data=d;
jobId =jobID2;
groupStatus = new int[mGroupCollection.size()];
totalNoTask=totalTasks;
userKey=userId;
setListEvent();
}
private void setListEvent() {
mExpandableListView
.setOnGroupExpandListener(new OnGroupExpandListener() {
#Override
public void onGroupExpand(int arg0) {
// TODO Auto-generated method stub
groupStatus[arg0] = 1;
}
});
mExpandableListView
.setOnGroupCollapseListener(new OnGroupCollapseListener() {
#Override
public void onGroupCollapse(int arg0) {
// TODO Auto-generated method stub
groupStatus[arg0] = 0;
}
});
}
#Override
public String getChild(int arg0, int arg1) {
// TODO Auto-generated method stub
return mGroupCollection.get(arg0).GroupItemCollection.get(arg1).Name;
}
#Override
public long getChildId(int arg0, int arg1) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getChildView(int arg0, int arg1, boolean arg2, View arg3,
ViewGroup arg4) {
// TODO Auto-generated method stub
HashMap<String, String> song = new HashMap<String, String>();
song = data.get(arg0);
for (String key : song.keySet()) {
String tId =key;
String value = song.get("id");
taskID= value;
}
final EditText textView ;
ImageButton imGbtn ;
final ChildHolder childHolder;
if (arg3 == null) {
}
return arg3;
}
#Override
public int getChildrenCount(int arg0) {
// TODO Auto-generated method stub
return mGroupCollection.get(arg0).GroupItemCollection.size();
}
#Override
public Object getGroup(int arg0) {
// TODO Auto-generated method stub
return mGroupCollection.get(arg0);
}
#Override
public int getGroupCount() {
// TODO Auto-generated method stub
return mGroupCollection.size();
}
#Override
public long getGroupId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
}
i try to build a ExpandableListView with a own ExpandableListAdapter but the ExpandableListView is not visible in my activity :-(
Below you find my Source-Code.
Thanks for your help :-)
Greetings,
Kangee
Here the Code from the activity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.group_main);
onCreateDBAndDBTabled();
mAddGroupButton = (Button) findViewById(R.id.Button_Add_Group);
mAddGroupButton.setOnClickListener(this);
mExpandableList = (ExpandableListView) findViewById(R.id.ExpandableListView01);
ArrayList<ExpandableListItem> valueTree = new ArrayList<ExpandableListItem>();
ExpandableListItem gp1 = new ExpandableListItem();
gp1.putProperty("name", "e");
ExpandableListItem gp11 = new ExpandableListItem();
gp11.putProperty("name", "l");
gp1.addChild(gp11);
ExpandableListItem gp2 = new ExpandableListItem();
gp2.putProperty("name", "A");
ExpandableListItem gp22 = new ExpandableListItem();
gp22.putProperty("name", "B");
ExpandableListItem gp23 = new ExpandableListItem();
gp23.putProperty("name", "A1");
ExpandableListItem gp24 = new ExpandableListItem();
gp24.putProperty("name", "A3");
ExpandableListItem gp25 = new ExpandableListItem();
gp25.putProperty("name", "A4");
gp2.addChild(gp22);
gp2.addChild(gp23);
gp2.addChild(gp24);
gp2.addChild(gp25);
valueTree.add(gp1);
valueTree.add(gp2);
Log.d("onCreate", "hasChild " + gp1.hasChilds());
Log.d("onCreate", "hasChild " + gp2.hasChilds());
MyColoredExpandableListAdapter adapter = new MyColoredExpandableListAdapter(this, valueTree);
mExpandableList.setAdapter(adapter);
}
Code MyColoredExpandableListAdapter:
private class MyColoredExpandableListAdapter extends MyExpandableListAdapter{
public MyColoredExpandableListAdapter(Context context,
ArrayList<ExpandableListItem> valueTree) {
super(context, valueTree);
Log.d("MyColoredExpandableListAdapter", "Group Count" + this.getGroupCount());
for(int i = 0; i < this.getGroupCount(); i++)
Log.d("MyColoredExpandableListAdapter", "Child Count" + this.getChildrenCount(i));
// TODO Auto-generated constructor stub
}
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Log.d("getChildView", "GO INTO");
if(convertView == null){
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(R.layout.group_exp_childs, null);
}
TextView text = (TextView) convertView.findViewById(R.id.TextView_Group_EXP_Childs);
text.setText(">>" + this.mValueTree.get(groupPosition).getChild(childPosition).getProperty("name"));
return convertView;
}
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Log.d("getGroupView", "GO INTO");
if(convertView == null){
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(R.layout.group_exp_childs, null);
}
TextView text = (TextView) convertView.findViewById(R.id.TextView_Group_EXP_Childs);
text.setText(">" + this.mValueTree.get(groupPosition).getProperty("name"));
return convertView;
}
}
Code MyExpandableListAdapter:
public abstract class MyExpandableListAdapter extends BaseExpandableListAdapter {
protected Context mContext;
protected ArrayList<ExpandableListItem> mValueTree;
public MyExpandableListAdapter(Context context,
ArrayList<ExpandableListItem> valueTree)
{
mContext = context;
mValueTree = valueTree;
}
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
if(mValueTree.get(groupPosition).hasChilds())
return mValueTree.get(groupPosition).getChild(childPosition);
return null;
}
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition; // WE NEED NO SPECIAL ID
}
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
if(mValueTree.get(groupPosition).hasChilds())
return mValueTree.get(groupPosition).sizeOfChilds();
return 0;
}
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return mValueTree.get(groupPosition);
}
public int getGroupCount() {
// TODO Auto-generated method stub
return mValueTree.size();
}
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition; // WE NEED NO SPECIAL ID
}
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
}
Code ExpandableListItem:
public class ExpandableListItem {
private HashMap<String,String> properties = new HashMap<String,String>();
public void clearProperties() {
properties.clear();
}
public boolean containsPropertyKey(String key) {
return properties.containsKey(key);
}
public boolean containsPropertyValue(String value) {
return properties.containsValue(value);
}
public String getProperty(String key) {
return properties.get(key);
}
public boolean hasProperties() {
return !properties.isEmpty();
}
public String putProperty(String key, String value) {
return properties.put(key, value);
}
public String removeProperty(String key) {
return properties.remove(key);
}
public int sizeOfProperties() {
return properties.size();
}
public String[] propertiesKeys()
{
int count = 0;
String[] result = new String[sizeOfProperties()];
for(String key : this.properties.keySet())
result[count++] = key;
return result;
}
private ArrayList<ExpandableListItem> childs = new ArrayList<ExpandableListItem>();
public boolean addChild(ExpandableListItem object) {
return childs.add(object);
}
public void clearChilds() {
childs.clear();
}
public ExpandableListItem getChild(int index) {
return childs.get(index);
}
public boolean hasChilds() {
return !childs.isEmpty();
}
public ExpandableListItem removeChild(int index) {
return childs.remove(index);
}
public int sizeOfChilds() {
return childs.size();
}
}
The Code of the Layouts (Without Tag):
R.id.TextView_Group_EXP_Childs :
?xml version="1.0" encoding="utf-8"?
LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="20dp" android:id="#+id/TextView_Group_EXP_Childs" android:text=">>"/TextView
/LinearLayout