Android ExpandableListView does not work (Black/Empty Screen) - android

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

Related

Android searchView not searching the list until fully scrolled

I have a listview with a custom adapter, and Im trying to use SearchView with a CustomFilter. But the search is not "fully" working.
When I search for something that is on the viewable area of the listview, it is able to search, and all nonviewable area is not being included in the search.
Here is a video on whats going on:
https://youtu.be/2Z9FZMlNmGw
main
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_board_game_list, container, false);
this.listView = (ListView) view.findViewById(R.id.listView);
DatabaseAccess databaseAccess = DatabaseAccess.getInstance(this.getContext());
databaseAccess.open();
List<String> boardgamesNames = databaseAccess.getNames();
List<String> urls = databaseAccess.getUrls();
adapter = new bgAdapter(getContext(), R.layout.row_layout);
adapterOriginal = new bgAdapter(getContext(), R.layout.row_layout);
databaseAccess.close();
listView.setAdapter(adapter);
int i = 0;
for(String name: boardgamesNames) {
boardgameListRow data = new boardgameListRow(urls.get(i), boardgamesNames.get(i));
i++;
adapter.add(data);
adapterOriginal.add(data);
}
listView.setDivider(null);
listView.setDividerHeight(0);
searchView = (SearchView)view.findViewById(R.id.searchId);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
if (newText.length() > 0) {
adapter.getFilter().filter(newText);
}
return false;
}
});
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
#Override
public boolean onClose() {
BoardGameListFragment fragment= new BoardGameListFragment();
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container,fragment);
fragmentTransaction.commit();
//adapter = adapterOriginal;
return true;
}
});
// Inflate the layout for this fragment
return view;
}
}
Here is the Adapter:
https://github.com/Shank09/AndroidTemp/blob/master/bgAdapter.java
I think you need to call notifyDataSetChanged() in onQueryTextChange
I fixed it, I was using the wrong variable in bgAdapter. Please remove this question if possible.
public class Listbyoperator extends Activity {
ListView lstdetail;
Activity act;
EditText search;
ArrayList<DetailModel> detail=new ArrayList<DetailModel>();
DetailaAdapter dadapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listbyoperator);
act=this;
lstdetail=(ListView) findViewById(R.id.Listbyoperator_detaillist);
search=(EditText) findViewById(R.id.editsearch);
search.setPadding(10, 0, 0, 0);
search.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
String text = search.getText().toString().toLowerCase(Locale.getDefault());
dadapter.filter(text);
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
});
DetailModel d=new DetailModel("1","HARDIP","saahi");
detail.add(d);
DetailModel d1=new DetailModel("2","jalpa","sadfsadf");
detail.add(d1);
dadapter=new DetailaAdapter(act, detail);
lstdetail.setAdapter(dadapter);
dadapter.notifyDataSetChanged();
lstdetail.setEnabled(true);
}
}
/*Detail Model*/
public class DetailModel
{
public String d_id,d_name,d_decription;
public String getD_id() {
return d_id;
}
public void setD_id(String d_id) {
this.d_id = d_id;
}
public String getD_name() {
return d_name;
}
public void setD_name(String d_name) {
this.d_name = d_name;
}
public String getD_decription() {
return d_decription;
}
public void setD_decription(String d_decription) {
this.d_decription = d_decription;
}
public DetailModel(String s1,String s2,String s3)
{
this.d_id=s1;
this.d_name=s2;
this.d_decription=s3;
}
}
/*detail adapter */
public class DetailaAdapter extends BaseAdapter{
Context mContext;
private List<DetailModel> data=null;
private ArrayList<DetailModel> arraylist;
private static LayoutInflater inflater=null;
private static String String=null,valid;
public boolean flag=true;
public DetailaAdapter(Context context,List<DetailModel> data)
{
mContext = context;
this.data = data;
inflater = LayoutInflater.from(mContext);
this.arraylist = new ArrayList<DetailModel>();
this.arraylist.addAll(data);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return data.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) {
// TODO Auto-generated method stub
View vi=convertView;
if(convertView==null)
inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
vi = inflater.inflate(R.layout.list_detail, null);
final TextView t1,t2,t3,t4,t5;
t1=(TextView)vi.findViewById(R.id.list_detail_text1);
t2=(TextView)vi.findViewById(R.id.list_detail_textview2);
t3=(TextView)vi.findViewById(R.id.list_detail_text2);
DetailModel da =new DetailModel(String, String,String);
da=data.get(position);
final String a1,a2,a3;
a1=da.d_id;
a2=da.d_name;
a3=da.d_decription;
t2.setText(a3);//description
t3.setText(a2);//name
return vi;
}
public void filter(String charText)
{
charText = charText.toLowerCase(Locale.getDefault());
data.clear();
if (charText.length() == 0) {
data.addAll(arraylist);
}
else
{
for (DetailModel wp : arraylist)
{
if (wp.getD_decription().toLowerCase(Locale.getDefault()).contains(charText) || wp.getD_name().toLowerCase(Locale.getDefault()).contains(charText))
{
data.add(wp);
}
}
}
notifyDataSetChanged();
}
}

CheckBox in child of exapndablelistview (android)

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.

Listview with slide expandable details view

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...

filtering a list with base adapter

I have a ListView with a BaseAdapter , I want to make a filter after the filed tipdoc,and my code doesn't work ,I mean nothing is heapen when start to write in the EditText and I can't find why,This is my code:
listView = (ListView) findViewById(android.R.id.list);
adapter = new ImageAndTextAdapter(Documente.this, R.layout.list_row,nume,tipdoc,formatdoc);
listView.setAdapter(adapter);
edt = (EditText) findViewById(R.id.search_box);
edt.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
ArrayList<String> nume_sort = new ArrayList<String>();
ArrayList<String> tipdoc_sort = new ArrayList<String>();
ArrayList<String> formatdoc_sort = new ArrayList<String>();
int textlength = edt.getText().length();
nume_sort.clear();
tipdoc_sort.clear();
formatdoc_sort.clear();
for (int i = 0; i < tipdoc.size(); i++)
{
if (textlength <= tipdoc.get(i).length())
{
if (edt.getText().toString().equalsIgnoreCase((String) tipdoc.get(i).subSequence(0, textlength)))
{
tipdoc_sort.add(tipdoc.get(i));
}
}
}
listView.setAdapter(new ImageAndTextAdapter
(Documente.this, R.layout.list_row,nume_sort,tipdoc_sort,formatdoc_sort));
}
#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
}
});
}
private class ImageAndTextAdapter extends BaseAdapter{
private Context adContext;
public int getCount() {
return nume.size();
}
public ImageAndTextAdapter(Context context,int layout,ArrayList<String> nume,ArrayList<String> tipdoc,ArrayList<String> formatdoc)
{
super();
this.adContext = context;
}
public View getView(int pos, View inView, ViewGroup parent){
View v = inView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater)adContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.list_row, null);
}
String num = String.format(nume.get(pos));
((TextView)v.findViewById(R.id.Nume)).setText(num);
String tdoc = String.format(tipdoc.get(pos));
((TextView)v.findViewById(R.id.TDoc)).setText(tdoc);
if (formatdoc.get(pos).equals("doc")){
((ImageView)v.findViewById(R.id.list_image)).setImageResource(R.drawable.doc);
}
else if (formatdoc.get(pos).equals(".xlsx") || formatdoc.get(pos).equals("xls")) ((ImageView)v.findViewById(R.id.list_image)).setImageResource(R.drawable.xls);
else ((ImageView)v.findViewById(R.id.list_image)).setImageResource(R.drawable.pdf);
return v;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
}
you have to change the value of nume var,because in your code its the same
try this code...
et.addTextChangedListener(new TextWatcher()
{
public void afterTextChanged(Editable s)
{
// Abstract Method of TextWatcher Interface.
}
public void beforeTextChanged(CharSequence s,
int start, int count, int after)
{
// Abstract Method of TextWatcher Interface.
}
public void onTextChanged(CharSequence s,int start, int before, int count)
{
textlength = et.getText().length();
array_sort.clear();
for (int i = 0; i < listview_array_location.length; i++)
{
if (textlength <= listview_array_location[i].length())
{
if(et.getText().toString().equalsIgnoreCase((String)listview_array_location[i].subSequence(0,textlength)))
{
array_sort.add(listview_array[i]);
}
}
}
AppendList(array_sort);
}
});
}
public void AppendList(ArrayList<String> str)
{
listview_arr = new String[str.size()];
listview_arr = str.toArray(listview_arr);
setListAdapter(new bsAdapter(this));
}
public class bsAdapter extends BaseAdapter
{
Activity cntx;
public bsAdapter(Activity context)
{
// TODO Auto-generated constructor stub
this.cntx=context;
}
public int getCount()
{
// TODO Auto-generated method stub
return listview_arr.length;
}
public Object getItem(int position)
{
// TODO Auto-generated method stub
return listview_arr[position];
}
public long getItemId(int position)
{
// TODO Auto-generated method stub
return listview_array.length;
}
public View getView(final int position, View convertView, ViewGroup parent)
{
View row=null;
LayoutInflater inflater=cntx.getLayoutInflater();
row=inflater.inflate(R.layout.search_list_item, null);
TextView tv=(TextView)row.findViewById(R.id.title);
tv.setText(listview_arr[position]);
return row;
}
}

Retrieve data from SQLite to Expandable list view?

Hi i have one table "TABLE_OBJECTIVE" with 1 question and 4 option which i accept from
EditText. My question is that how to show this Question as parent and 4 option as child
in expandable list view. i use some code but it doesn't display question and display only
one option out of 4.
Here is my sample code.
public class ObjectiveExamActivity extends ExpandableListActivity {
#SuppressWarnings("unchecked")
public void onCreate(Bundle savedInstanceState) {
try{
super.onCreate(savedInstanceState);
setContentView(R.layout.displayobjectiveque);
Intent intent=getIntent();
setResult(RESULT_OK, intent);
SimpleExpandableListAdapter expListAdapter =
new SimpleExpandableListAdapter(
this,
createGroupList(),
R.layout.group_row,
new String[] { "Group Item" },
new int[] { R.id.row_name },
createChildList(),
R.layout.child_row,
new String[] {"Sub Item"},
new int[] { R.id.grp_child}
);
setListAdapter( expListAdapter );
}catch(Exception e){
System.out.println("Errrr +++ " + e.getMessage());
}
}
#SuppressWarnings("unchecked")
private List createGroupList() {
ArrayList result = new ArrayList();
final MySQLiteHelper m=new MySQLiteHelper(getBaseContext());
final ArrayList<ObjectiveWiseQuestion> LocWiseProfile= (ArrayList<ObjectiveWiseQuestion>) m.getAllObjectiveQuestion();
for (final ObjectiveWiseQuestion cn : LocWiseProfile)
{
HashMap m1=new HashMap();
m1.put("Question",cn.getQuestion());
result.add(m1);
}
return (List)result;
}
#SuppressWarnings("unchecked")
private List createChildList()
{
final MySQLiteHelper m=new MySQLiteHelper(getBaseContext());
final ArrayList<ObjectiveWiseQuestion> LocWiseProfile= (ArrayList<ObjectiveWiseQuestion>) m.getAllObjectiveQuestion();
ArrayList result = new ArrayList();
for (final ObjectiveWiseQuestion cn : LocWiseProfile)
{
/* each group need each HashMap-Here for each group we have 4 subgroups */
ArrayList secList = new ArrayList();
for(ObjectiveWiseQuestion cn1: LocWiseProfile)
{
HashMap child = new HashMap();
//child.putAll(cn.getOptionA(),cn.getOptionB(),cn.getOptionC());
child.put( "Sub Item", "Sub Item " + cn.getOptionA() );
child.put("", cn.getOptionB());
child.put("Option C", cn.getOptionC());
child.put("Option D", cn.getOptionD());
secList.add( child );
}
result.add( secList );
}
return result;
}
I really don't understand where the problem in my code.
Please give me some hint or reference.
Here is image of emulator which show only one item.
I have 4 option(Google,Samsung,Nokia,Onida).
Thanks in Advance.
Hi please try this code its working fine for me for one question but as i used array list and hashmap you can use length and size functions for multiple questions
public class ShowQuiz extends Activity {
/** Called when the activity is first created. */
ExpandableListView expandableListView;
HashMap<String, Options> hashMap;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Options options=new Options();
hashMap=new HashMap<String, Options>(1);
hashMap.put("Which is Domestic Animal?", options);
expandableListView=(ExpandableListView) findViewById(R.id.ev);
expandableListView.setAdapter(new QuizQuestionAdapter());
}
class QuizQuestionAdapter extends BaseExpandableListAdapter
{
#Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return hashMap.get(0).fetchOptions().get(childPosition);
}
#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) {
// TODO Auto-generated method stub
TextView textView=new TextView(ShowQuiz.this);
textView.setText(hashMap.get("Which is Domestic Animal?").getQuestion(childPosition).toString());
return textView;
}
#Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return 4;
}
#Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return hashMap.get(0);
}
#Override
public int getGroupCount() {
// TODO Auto-generated method stub
return 1;
}
#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) {
// TODO Auto-generated method stub
TextView textView=new TextView(ShowQuiz.this);
textView.setText("Which is Domestic Animal?");
return textView;
}
#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;
}
}
class Options
{
ArrayList<String> optns;
Options()
{
optns=new ArrayList<String>(4);
optns.add("rat");
optns.add("cat");
optns.add("dog");
optns.add("tiger");
}
public ArrayList<String> fetchOptions()
{
return this.optns;
}
public String getQuestion(int position)
{
return optns.get(position);
}
}
}

Categories

Resources