I want to create an ExpandableListView with custom adapter, however it only show my title group and when I click on the title (to view the children items) it doesnt do anything
(I want to have only one group item, therefore the item group count is 1).
Please help me to find where am I wrong, thanks alot.
This is my adapter:
class chatListAdapter extends BaseExpandableListAdapter {
private String objectsIdInsideAdapter;
private List<String> commentsInsideAdapter = new ArrayList<String>();
private List<String> FacebookIdInsideAdapter=new ArrayList<String>();
private List<String> NamesInsideAdapter =new ArrayList<String>();
private Context mContext;
chatListAdapter(String objectId,Context ctx,List<String> comments,List<String> face,List<String> names){
objectsIdInsideAdapter=objectId;
mContext=ctx;
commentsInsideAdapter=comments;
FacebookIdInsideAdapter=face;
NamesInsideAdapter=names;
}
#Override
public Object getChild(int arg0, int arg1) {
// TODO Auto-generated method stub
return NamesInsideAdapter.get(arg1);
}
#Override
public long getChildId(int arg0, int arg1) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View v,
ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = getLayoutInflater();
if(v==null){
v = inflater.inflate(R.layout.chat_item, parent, false);
}
String comment =commentsInsideAdapter.get(position);
Button sendCommentBtn =(Button) v.findViewById(R.id.chatitemSendComment);
TextView commentView= (TextView) v.findViewById(R.id.chateitemViewComment);
commentView.setText(comment);
TextView commentName =(TextView) v.findViewById(R.id.chatitemfreindName);
commentName.setText(NamesInsideAdapter.get(position));
EditText commentText= (EditText) v.findViewById(R.id.chatitemEnterComment);
commentText.setVisibility(View.GONE);
sendCommentBtn.setVisibility(View.GONE);
return v;
}
#Override
public int getChildrenCount(int arg0) {
// TODO Auto-generated method stub
return NamesInsideAdapter.size();
}
#Override
public Object getGroup(int arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public int getGroupCount() {
// TODO Auto-generated method stub
return 1;
}
#Override
public long getGroupId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub
View v = convertView;
if (v == null) {
LayoutInflater inflater = getLayoutInflater();
v = inflater.inflate(R.layout.chat_item_group_layout,parent, false);
}
TextView groupName = (TextView) v.findViewById(R.id.groupName);
TextView groupDescr = (TextView) v.findViewById(R.id.groupDescr);
groupName.setText("view comments");
groupDescr.setText("groupDSCR");
return v;
}
#Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean isChildSelectable(int arg0, int arg1) {
// TODO Auto-generated method stub
return false;
}
and this is the code in the main Activity:
ExpandableListView chat =(ExpandableListView) v.findViewById(R.id.ExpList);
String faceids =facebookIdChatInside.get(position);
String comments = commentInside.get(position);
String namesChat =nameChatInside.get(position);
List<String> chatItems = new ArrayList<String>(Arrays.asList(comments.split(",")));
List<String> facebookids = new ArrayList<String>(Arrays.asList(faceids.split(",")));
List<String> namesList = new ArrayList<String>(Arrays.asList(namesChat.split(",")));
chatItems.add(" ");
facebookids.add(id);
namesList.add(name);
chatListAdapter chatA =new chatListAdapter(objectsId.get(position),profileContext,chatItems,facebookids,namesList);
chat.setAdapter(chatA);
I checked it, and those 3 Lists are not empty.
Reading your code i found 3 points that aren't so clear to me:
getGroup you return null? Why don't u return the real Object
getChildId you should return an unique id identifying your child
hasStableIds. You return false. You dont need to override this method.
If you want to have more information on ExpandableListView give a look at my blog here
You are using the ExpandableListView like a simple ListView why dont u consider to you a ListView?
Related
I would like to after clicking on the item child opened my new activity with the same name as the name of the item. That is item the "test" and if I click it open test.java class. I found a code that does this, but I do not know how to add it to my classes. Thank you for any help.
public class MyListAdapter extends BaseExpandableListAdapter {
private Context context;
private ArrayList<Alphabet> alphabetList;
private ArrayList<Alphabet> originalList;
public MyListAdapter(Context context, ArrayList<Alphabet> alphabetList){
this.context = context;
this.alphabetList = new ArrayList<Alphabet>();
this.alphabetList.addAll(alphabetList);
this.originalList = new ArrayList<Alphabet>();
this.originalList.addAll(alphabetList);
}
#Override
public int getGroupCount() {
// TODO Auto-generated method stub
return alphabetList.size();
}
#Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
ArrayList<Waste> wasteList = alphabetList.get(groupPosition).getWasteList();
return wasteList.size();
}
#Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return alphabetList.get(groupPosition);
}
#Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
ArrayList<Waste> wasteList = alphabetList.get(groupPosition).getWasteList();
return wasteList.get(childPosition);
}
#Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
#Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
#Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Alphabet alphabet = (Alphabet) getGroup(groupPosition);
if(convertView == null)
{
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.group_row, null);
}
TextView heading = (TextView) convertView.findViewById(R.id.heading);
heading.setText(alphabet.getName().trim());
return convertView;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Waste waste = (Waste) getChild(groupPosition, childPosition);
if(convertView == null){
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.child_row, null);
}
TextView name = (TextView) convertView.findViewById(R.id.name);
name.setText(waste.getName().trim());
return convertView;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
public void filterData(String query)
{
query = query.toLowerCase();
Log.v("MyListAdapter", String.valueOf(alphabetList.size()));
alphabetList.clear();
if(query.isEmpty())
{
alphabetList.addAll(originalList);
} else {
for(Alphabet alphabet: originalList)
{
ArrayList<Waste> wasteList = alphabet.getWasteList();
ArrayList<Waste> newList = new ArrayList<Waste>();
for(Waste waste: wasteList)
{
if(waste.getName().toLowerCase().contains(query)){
newList.add(waste);
}
}
if(newList.size() > 0)
{
Alphabet nAlphabet = new Alphabet(alphabet.getName(), newList);
alphabetList.add(nAlphabet);
}
}
}
Log.v("MyListAdapter", String.valueOf(alphabetList.size()));
notifyDataSetChanged();
}
}
SegregateWasteActivity.java
public class SegregateWasteActivity extends Activity implements SearchView.OnQueryTextListener, SearchView.OnCloseListener {
private SearchView search;
private MyListAdapter listAdapter;
private ExpandableListView myList;
private ArrayList<Alphabet> alphabetList = new ArrayList<Alphabet>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.segregate_waste_activity);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
search = (SearchView) findViewById(R.id.search);
search.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
search.setIconifiedByDefault(false);
search.setOnQueryTextListener(this);
search.setOnCloseListener(this);
displayList();
expandAll();
}
#Override
public boolean onClose() {
// TODO Auto-generated method stub
listAdapter.filterData("");
expandAll();
return false;
}
#Override
public boolean onQueryTextSubmit(String query) {
// TODO Auto-generated method stub
listAdapter.filterData(query);
expandAll();
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
// TODO Auto-generated method stub
listAdapter.filterData(newText);
expandAll();
return false;
}
private void expandAll()
{
int count = listAdapter.getGroupCount();
for(int i = 0; i < count; i++)
{
myList.expandGroup(i);
}
}
private void displayList()
{
loadSomeData();
myList = (ExpandableListView) findViewById(R.id.expandableList);
listAdapter = new MyListAdapter(SegregateWasteActivity.this, alphabetList);
myList.setAdapter(listAdapter);
}
private void loadSomeData()
{
ArrayList<Waste> wasteList = new ArrayList<Waste>();
Waste waste = new Waste("Aerozol");
wasteList.add(waste);
waste = new Waste("Aaaa");
wasteList.add(waste);
Alphabet alphabet = new Alphabet("A", wasteList);
alphabetList.add(alphabet);
wasteList = new ArrayList<Waste>();
waste = new Waste("Butelka");
wasteList.add(waste);
alphabet = new Alphabet("B", wasteList);
alphabetList.add(alphabet);
}
}
and I want to this fragment throw:
#Override
protected void onListItemClick(ListView lv, View v, int position, long id){
super.onListItemClick(lv, v, position, id);
String openClass = classNames[position];
try{
Class selected = Class.forName("com.odpad.odpadygdansk.waste." + openClass);
Intent selectedIntent = new Intent(this, selected);
startActivity(selectedIntent);
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}
so I can click on an item from the list to go to the new activity with the same name as the class name.
I believe you want something like this:
expListView.setOnChildClickListener(new ExpandableListView
.OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView elv, View view, int i,
int i2, long l) {
TextView tv = view.findViewById(R.id.name);
String name = (String) tv.getText();
try {
// Change package.name to your package
Class cls = Class.forName("package.name." + name);
Intent intent = new Intent(MainActivity.this, cls);
startActivity(intent);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return false;
}
});
The code is pretty much self-explanatory.
You could use the position argument i received in the click callback, to fetch the specific name instead of findViewById(). That would be somewhat more efficient.
I have an ArrayList of Person class containing age and name of the person. In the adapter (extends BaseAdapter), I have two TextViews(for setting the values of age and name) and a checkbox. This is needed to be inflated into alert dialog.
How can I implement this using multichoice of the alert dialog.
Also I did look at some examples but did not understand about the boolean[] values attribute in the alert dialog, this is the code I have so far but it still needs to implement that multichoice mode..
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:choiceMode="multipleChoice">
</ListView>
My alertdialog..
AlertDialog.Builder ab=new AlertDialog.Builder(CustomListActivity.this);
LayoutInflater linf=(LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View v1=linf.inflate(R.layout.dialog_list, null);
ab.setView(v1);
//ab.setTitle("Select a group");
lv=(ListView) v1.findViewById(R.id.listView1);
ma=new MyAdapter(CustomListActivity.this, plist);
lv.setAdapter(ma);
And MYAdapter ..
public class MyAdapter extends BaseAdapter
{
Context ctx;
ArrayList<Person> plist;
LayoutInflater linf;
public PersonHolder ph=null;
public MyAdapter(Context ctx, ArrayList<Person> plist) {
super();
this.ctx = ctx;
this.plist = plist;
}
public class PersonHolder
{
public TextView age;
public TextView name;
public CheckBox check;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return plist.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return plist.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup arg2) {
// TODO Auto-generated method stub
linf=(LayoutInflater) ctx.getSystemService(ctx.LAYOUT_INFLATER_SERVICE);
if(convertView==null)
{
convertView=linf.inflate(R.layout.row_item, null);
ph=new PersonHolder();
ph.age=(TextView) convertView.findViewById(R.id.text1);
ph.name=(TextView) convertView.findViewById(R.id.text2);
ph.check=(CheckBox) convertView.findViewById(R.id.checkBox1);
convertView.setTag(ph);
}
else
{
ph=(PersonHolder) convertView.getTag();
}
Person p=(Person) getItem(position);
ph.age.setText(p.getAge());
ph.name.setText(p.getName());
ph.check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
// if(ph.check.isChecked())
// {
ph.check.setSelected(arg1);
// }
// else
// {
// ph.check.setSelected(false);
// }
}
});
return convertView;
}
}
Change your adapter to following and just call getSelected on adapter.
public class MyAdapter extends BaseAdapter
{
Context ctx;
ArrayList<Person> plist;
LayoutInflater linf;
public PersonHolder ph=null;
private ArrayList<Integer> selected=new ArrayList<Integer>();
public MyAdapter(Context ctx, ArrayList<Person> plist) {
super();
this.ctx = ctx;
this.plist = plist;
}
public class PersonHolder
{
public TextView age;
public TextView name;
public CheckBox check;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return plist.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return plist.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup arg2) {
// TODO Auto-generated method stub
linf=(LayoutInflater) ctx.getSystemService(ctx.LAYOUT_INFLATER_SERVICE);
if(convertView==null)
{
convertView=linf.inflate(R.layout.row_item, null);
ph=new PersonHolder();
ph.age=(TextView) convertView.findViewById(R.id.text1);
ph.name=(TextView) convertView.findViewById(R.id.text2);
ph.check=(CheckBox) convertView.findViewById(R.id.checkBox1);
convertView.setTag(ph);
}
else
{
ph=(PersonHolder) convertView.getTag();
}
Person p=(Person) getItem(position);
ph.age.setText(p.getAge());
ph.name.setText(p.getName());
ph.check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
// if(ph.check.isChecked())
// {
ph.check.setSelected(arg1);
if(arg1){
selected.add(position);
}else{
selected.remove(position);
}
// }
// else
// {
// ph.check.setSelected(false);
// }
}
});
return convertView;
}
public ArrayList<Integer> getSelected(){
return selected;
}
}
I have created a multichoice dialog myself, this is what i am doing, basically you need to catch each click to the listview and memorize whatever it is you selected/set there.
private void startDayPickerDialog() {
String[] days = getActivity().getResources().getStringArray(R.array.dayNames);
days = Arrays.copyOf(days, 7);
final Adapter a = new Adapter(getActivity());
AlertDialog d = new AlertDialog.Builder(getActivity())
.setTitle(R.string.repeat)
.setAdapter(a, new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
}).setPositiveButton(R.string.done, new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
}).create();
d.getListView().setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> lv, View view, int position, long id) {
CheckBox cb = (CheckBox)view.findViewById(R.id.checkbox);
cb.setChecked(!cb.isChecked());
a.getItem(position).setChecked(!a.getItem(position).isChecked());
}
});
d.setCanceledOnTouchOutside(true);
d.show();
}
When I had problems trying to fix the position of my listView items to the desired intent when filtered, and got info I could override the problem using a custom adapter, I have done that but I do not know how to assign the clicks to each items, please check below code:
public class IndexPageActivity extends Activity {
ListView listView;
EditText editTextB;
#Override
protected void onCreate(Bundle savfedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.indexpage);
listView = (ListView) findViewById(R.id.pageList);
editTextB = (EditText) findViewById(R.id.searchB);
listView.setAdapter(new PagesAdapter(this));
listView.setOnItemClickListener((OnItemClickListener) this);
}
}
class SingleRow {
String pagedata;
SingleRow(String pagedata){
this.pagedata=pagedata;
}
}
class PagesAdapter extends BaseAdapter implements OnItemClickListener{
ArrayList<SingleRow> pagelist;
Context context;
PagesAdapter(Context c){
context=c;
pagelist = new ArrayList<SingleRow>();
Resources res = c.getResources();
String [] pagedatas = res.getStringArray(R.array.pages_data);
for (int i=0;i<463;i++){
pagelist.add(new SingleRow(pagedatas[i]));
}
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return pagelist.size();
}
#Override
public Object getItem(int i) {
// TODO Auto-generated method stub
return pagelist.get(i);
}
#Override
public long getItemId(int i) {
// TODO Auto-generated method stub
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewG) {
// TODO Auto-generated method stub
LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row=inflater.inflate(R.layout.single_row,viewG,false);
TextView pagetitle = (TextView) row.findViewById(R.id.textViewRow);
SingleRow temp=pagelist.get(i);
pagetitle.setText(temp.pagedata);
return row;
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int i, long arg3) {
// TODO Auto-generated method stub
}
}
I will appreciate any help given. Thank Yhu!
EDIT
Will this work?
if (index == 0) {
Intent i = new Intent(this, WebViewActivity.class);
i.putExtra("keyHTML", "file:///android_asset/page1.html");
startActivity(i);
} else if (index == 1) {
Intent i = new Intent(this, WebViewActivity.class);
i.putExtra("keyHTML", "file:///android_asset/page2.html");
startActivity(i);
Edited ALL
I just got what you need, I added a filter to your baseAdapter and then on text change within the editText You filter the listView and then you go to the activity needed.
here is the full code but you need to bare in mind that I have changed the following:
I changed the pageList to ArrayList instead of
There is a bug in filtering that when I erase what I wrote in the EditText it doesnt update the ListView, you need to figure out why.
I changed the returned value of the function getItem(int i) from Object to String
Within the onItemClick you have to search for the name instead of the position.
here is the code:
public class IndexPageActivity extends Activity implements OnItemClickListener{
ListView listView;
EditText editTextB;
PagesAdapter adapter1;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.pageList);
editTextB = (EditText) findViewById(R.id.searchB);
adapter1 = new PagesAdapter(this);
listView.setAdapter(adapter1);
adapter1.notifyDataSetChanged();
listView.setOnItemClickListener(this);
editTextB.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2,
int arg3) {
// When user changed the Text
IndexPageActivity.this.adapter1.getFilter().filter(cs.toString());
adapter1.notifyDataSetChanged();
}
#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
}
});
}
#Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
// TODO Auto-generated method stub
Intent i;
String name = adapter1.getItem(position);
Log.d("id", name);
if (name.equals("Item1"))
{
i = new Intent(this, anActivity.class);
startActivity(i);
}
else if (name.equals("Item2"))
{
i = new Intent(this, anActivity2.class);
startActivity(i);
}
}
}
class SingleRow {
String pagedata;
SingleRow(String pagedata){
this.pagedata=pagedata;
}
}
class PagesAdapter extends BaseAdapter implements Filterable{
ArrayList<String> pagelist;
List<String> arrayList;
Context context;
String [] pagedatas;
PagesAdapter(Context c){
context=c;
pagelist = new ArrayList<String>();
Resources res = c.getResources();
pagedatas = res.getStringArray(R.array.pages_data);
for (int i=0;i<463;i++){
pagelist.add(pagedatas[i]);
}
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return pagelist.size();
}
#Override
public String getItem(int i) {
// TODO Auto-generated method stub
return pagelist.get(i);
}
#Override
public long getItemId(int i) {
// TODO Auto-generated method stub
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewG) {
// TODO Auto-generated method stub
LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row=inflater.inflate(R.layout.single_row,viewG,false);
TextView pagetitle = (TextView) row.findViewById(R.id.textViewRow);
String temp=pagelist.get(i);
pagetitle.setText(temp);
return row;
}
public class filter_here extends Filter{
#Override
protected FilterResults performFiltering(CharSequence constraint) {
// TODO Auto-generated method stub
FilterResults Result = new FilterResults();
// if constraint is empty return the original names
if(constraint.length() == 0 ){
Result.values = pagelist;
Result.count = pagelist.size();
return Result;
}
ArrayList<String> Filtered_Names = new ArrayList<String>();
String filterString = constraint.toString().toLowerCase();
String filterableString;
for(int i = 0; i<pagelist.size(); i++){
filterableString = pagelist.get(i);
if(filterableString.toLowerCase().contains(filterString)){
Filtered_Names.add(filterableString);
}
}
Result.values = Filtered_Names;
Result.count = Filtered_Names.size();
return Result;
}
#Override
protected void publishResults(CharSequence constraint,FilterResults results) {
// TODO Auto-generated method stub
pagelist = (ArrayList<String>) results.values;
notifyDataSetChanged();
}
}
#Override
public Filter getFilter() {
// TODO Auto-generated method stub
return new filter_here();
}
}
Two different ways for it:
1) If you want to use something like this at onCreate of your activity;
listView.setOnItemClickListener((OnItemClickListener) this);
You should implement OnItemClickListener to your activity:
IndexPageActivity extends Activity implements OnItemClickListener
and implement its onItemClick method at your activity. (also remove OnItemClickListener interface from your custom adapter)
2) You can simply use below without implementing OnItemClickListener to any class:
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO: handle row clicks here
}
I suggest 2nd option. That is easier.
Edit: This not relevant to your problem but you should reuse your views/rows at listView. Change your getView method to:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (convertView == null) {
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.single_row, parent, false);
} else {
view = convertView;
}
TextView pagetitle = (TextView) view.findViewById(R.id.textViewRow);
SingleRow temp=pagelist.get(i);
pagetitle.setText(temp.pagedata);
return view;
}
Set your setOnItemClickListenerlike this:
#Override
protected void onCreate(Bundle savfedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.indexpage);
listView = (ListView) findViewById(R.id.pageList);
editTextB = (EditText) findViewById(R.id.searchB);
listView.setAdapter(new PagesAdapter(this));
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//Your code here
//int position is the index of the list item you clicked
//use it to manipulate the item for each click
}
});
}
i have an ArrayList and one BaseAdapter to inflate my layout, when one array from arraylist is empty, the value on layout show text "NaN%" it is so annoying, i want handle an empty array from my arraylist with setText as (0%). so how to handle empty value from ArrayList in baseadapter android? this is my code :
public class TabelNABDetail extends BaseAdapter {
Context context;
ArrayList<NabDetail> listData;//stack
int count;
public TabelNABDetail(Context context, ArrayList<NabDetail>listData){
this.context=context;
this.listData=listData;
this.count=listData.size();
}
#Override
public int getCount() {
// TODO Auto-generated method stub
// return count;
return (listData==null) ? 0 : listData.size();
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View v, ViewGroup parent) {
// TODO Auto-generated method stub
View view= v;
ViewHolder holder= null;
if(view==null){
LayoutInflater inflater=(LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
view= inflater.inflate(R.layout.layout_tabel_detailnab, null);
holder=new ViewHolder();
holder.persen_hke1=(TextView)view.findViewById(R.id.varpersenke1);
view.setTag(holder);
}
else{
holder=(ViewHolder)view.getTag();
}
if (position==0) {
holder.persen_hke1.setText("0%");
} else {
holder.persen_hke1.setText(String.format("%.5f",listData.get(position).getpersen_hke1())+"%");
}
return view;
}
static class ViewHolder{
TextView ,persen_hke1;
}
i want to handle if listData.get(position).getpersen_hke1() is empty value, it gonna be holder.persen_hke1.setText("0%") how to solve it? thanks before
Change your getView() like this
#Override
public View getView(int position, View v, ViewGroup parent) {
// TODO Auto-generated method stub
View view= v;
ViewHolder holder= null;
if(view==null){
LayoutInflater inflater=(LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
view= inflater.inflate(R.layout.layout_tabel_detailnab, null);
holder=new ViewHolder();
holder.persen_hke1=(TextView)view.findViewById(R.id.varpersenke1);
view.setTag(holder);
} else {
holder=(ViewHolder)view.getTag();
}
if (position==0) {
holder.persen_hke1.setText("0%");
} else {
**/////// these lines are extra lines add into your code**
NabDetail nabDetail = listData.get(position)
if(!nabDetail.getpersen_hke1().equals(""))
holder.persen_hke1.setText(String.format("%.5f",listData.get(position).getpersen_hke1())+"%");
} else {
holder.persen_hke1.setText("0%");
}
}
return view;
}
I created two Spinners to display the data in my application. In my first Spinner, the first item of the list is always displayed directly, but in the second Spinner nothing is displayed, even if I click on an item on the drop down view. Can anybody explain this behaviour?
Here is the code of the initialisation of the two spinners:
projects = new Spinner(lexs);
projectAdapter = new ProjectAdapter();
projects.setAdapter(projectAdapter);
projects.setMinimumWidth(250);
projects.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
updateSpinners(position);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// do nothing
}
});
projectsList = new Spinner(lexs);
projectsList.setMinimumWidth(250);
listAdapter = new ListAdapter();
projectsList.setAdapter(listAdapter);
projectsListLayer.addView(projectsList);
The Spinner projects is the first Spinner which works correctly. projectsList is the second Spinner which doesn't work correctly. Here are the two implementations of the adapters:
private class ProjectAdapter implements SpinnerAdapter {
#Override
public View getDropDownView(int position, View arg1, ViewGroup arg2) {
TextView text = new TextView(lexs);
text.setText(allProjects.get(position).getName());
return text;
}
#Override
public int getCount() {
return allProjects.size();
}
#Override
public Object getItem(int position) {
return allProjects.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getItemViewType(int arg0) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View arg1, ViewGroup arg2) {
TextView text = new TextView(lexs);
text.setText(allProjects.get(position).getName());
return text;
}
#Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return 0;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isEmpty() {
if (allProjects.size() == 0) {
return true;
} else {
return false;
}
}
#Override
public void registerDataSetObserver(DataSetObserver arg0) {
// TODO Auto-generated method stub
}
#Override
public void unregisterDataSetObserver(DataSetObserver observer) {
// TODO Auto-generated method stub
}
}
private class ListAdapter implements SpinnerAdapter {
#Override
public View getDropDownView(int position, View view, ViewGroup parent) {
TextView text = new TextView(lexs);
text.setText(allLists.get(position).getName());
return text;
}
#Override
public int getCount() {
return allLists.size();
}
#Override
public Object getItem(int position) {
return allLists.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getItemViewType(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView text = new TextView(lexs);
text.setText(allLists.get(position).getName());
return text;
}
#Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return 0;
}
#Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean isEmpty() {
if (allLists.size() == 0) {
return true;
} else {
return false;
}
}
#Override
public void registerDataSetObserver(DataSetObserver observer) {
// TODO Auto-generated method stub
}
#Override
public void unregisterDataSetObserver(DataSetObserver observer) {
// TODO Auto-generated method stub
}
}
Here I attached a picture of the problem:
http://www.freeimagehosting.net/image.php?7684c157b8.png
The only thing I can suggest is to throw in a few log statements and check if allLists.size() > 0 before and after your updateSpinners call.
Also, now that I think about it. You also need to notify the spinner that the data has changed by calling BaseAdapter#notifyDataSetChanged. Otherwise it won't know that it has new data to display. So you should extend BaseAdapter and implement SpinnerAdapter. BaseAdapter will handle the implementation of registerDataSetObserver and unregisterDataSetObserver. You'll probably also need to have your updateSpinners call into your ListAdapter's notifyDataSetChanged.
Check out the ArrayAdapter source.