android expandable list need help - android

expListAdapter = new ColorAdapter(this, GrouppList, colors);
// setListAdapter(expListAdapter);
exlv1=(ExpandableListView) findViewById(R.id.expandableListView1);
this.exlv1.setAdapter(expListAdapter);
//exlv1.setAdapter(expListAdapter);
this.exlv1.setOnItemClickListener(new OnItemClickListener() {
#Override //THIS NOT WORKING
public void onItemClick(AdapterView<?> aview, View v,
int position, long id) {
Toast.makeText(FindFilesByType.this, "position "+position, Toast.LENGTH_LONG).show();
// TODO Auto-generated method stub
}
});
this.exlv1.setOnChildClickListener(new OnChildClickListener() {
#Override//THIS NOT WORKING
public boolean onChildClick(ExpandableListView arg0, View arg1, int arg2,
int arg3, long arg4) {
// TODO Auto-generated method stub
Toast.makeText(FindFilesByType.this, "position 1 : "+arg3, Toast.LENGTH_LONG).show();
return false;
}
});
into the above code i wat to know which item has clicked from the list but i am not able to know that
i want to know its position i've used expandable list what is i am missing?
thaks in advance..

Hi Please check below link
http://about-android.blogspot.com/2010/04/steps-to-implement-expandablelistview.html
http://techdroid.kbeanie.com/2010/09/expandablelistview-on-android.html

Related

set spinner item selected value in onitemclick android

Hello friends i have spinner as following
When i click on any item click event than it should be set like following
it means when i select Afghnistan at that time it should be set value as AF and rest of country value remain as it is my code is as follow
On Spinner item click
mSpinnerCountry.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
mStringCountryID=mArrayListCountryDatas.get(position).getId();
mSpinnerCountry.setPrompt(mArrayListCountryDatas.get(position).getIso_alpha()+"
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
When i run above code it is not set value any idea how can i make it possible ?
mSpinnerCountry.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
int mStringCountryID = s1.getSelectedItemPosition(); Toast.makeText(getBaseContext(),
"You have selected item : " + presidents[mStringCountryID],
Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
Try this... variable mStringCountryID will hold your specific complete value do what ever you want to do with this value.

How to Change background color of list item in Android?

How to change the background color onlongclick of list item in android, now iam getting the position of the list item onlongclick by using below code.
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int pos, long id) {
// TODO Auto-generated method stub
Log.v("long clicked","pos: " + pos);
return true;
}
});
Any help will be appriciated,, thank you
I found a possible solution in this post: Changing background color of ListView items on Android
private static int save = -1;
public void onListItemClick(ListView parent, View v, int position, long id) {
parent.getChildAt(position).setBackgroundColor(Color.BLUE);
if (save != -1 && save != position){
parent.getChildAt(save).setBackgroundColor(Color.BLACK);
}
save = position;
}
Change the method to onItemLongClick().
I hope this help!
Try this,
listView.setOnItemLongClickListener(new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View view,
int arg2, long arg3) {
view.setBackgroundColor(Color.parseColor("#222222"));
Log.v("Long Click", "Working");
return false;
}
});
Try this in your onItemLongClick()
public View view1;// should be declared as global
if (view1 != null) {
view1.setBackgroundResource(R.color.orange);
}
view1 = v;
v.setBackgroundResource(R.color.transparent_green);

Adding an event listener to listView

getSherlockActivity().findViewById(android.R.id.list);
new OnItemClickListener() {
#Override
public void onItemClick(AdapterView <? > arg0, View arg1, int position,
long id) {
// TODO Auto-generated method stub
Toast.makeText(getSherlockActivity(),
"You selected :", Toast.LENGTH_SHORT).show();
}
};
I am trying to add an event listener to my activity, but the toast message is not shown at all. I am calling this piece of code from onCreateView method
It doesn't seem like you are actually setting the click listener on your list, you are just creating it, and not even keeping a reference to it.
try like this:
ListView lv = (ListView)(getSherlockActivity().findViewById(android.R.id.list));
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView <? > arg0, View arg1, int position,
long id) {
// TODO Auto-generated method stub
Toast.makeText(getSherlockActivity(),
"You selected :", Toast.LENGTH_SHORT).show();
}
});
getSherlockActivity().findViewById(android.R.id.list).setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView <? > arg0, View arg1, int position,
long id) {
// TODO Auto-generated method stub
Toast.makeText(getSherlockActivity(),
"You selected :", Toast.LENGTH_SHORT).show();
}});
EDITED
((ListView)getSherlockActivity()).findViewById(android.R.id.list).setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView <? > arg0, View arg1, int position,
long id) {
// TODO Auto-generated method stub
Toast.makeText(getSherlockActivity(),
"You selected :", Toast.LENGTH_SHORT).show();
}});
Add this line inside onCreate().
I do not see you adding anything. All you did was findViewById() for no purpose (as you did not used return value. Also you just created new OnItemClickListener for no reason as you do not use it either. And no, putting two lines of code next to each other won't make then automagically interact in any way...
Right approach is:
ListView v = (ListView)getSherlockActivity().findViewById(android.R.id.list);
v.setOnItemClickListener( new OnItemClickListener() {
...
...
Couple of weeks ago, I found same situation. I didn't use Sherlock but, it may help to set following line.
listView.setItemsCanFocus(false);
Also make sure to set clickable false to any Button in your row item.
Only add
ListView lv = (ListView)getSherlockActivity().findViewById(android.R.id.list);
lv.setOnItemClickListener(this);
#Override
public void onItemClick(AdapterView <? > arg0, View arg1, int position,
long id) {
}
and Activity should implement OnItemClickListener
or
ListView lv = (ListView)getSherlockActivity().findViewById(android.R.id.list);
lv.setOnItemClickListener( new OnItemClickListener() {
#Override
public void onItemClick(AdapterView <? > arg0, View arg1, int position,
long id) {
}

error help! syntax and OnItemSelectedListener

OK, so I'm pretty new to this, but I'm getting it. I just ran into this error and I have seen other people have had the same problem, but the standard fix (clean) doesn't work for me. I have no clue how to fix these errors! HELP PLEASE!
first error:
on sp2.setOnItemSelectedListener(new OnItemSelectedListener() { in my else if statement I keep geting this error:
The type new AdapterView.OnItemSelectedListener(){} must implement the inherited abstract method AdapterView.OnItemSelectedListener.onNothingSelected(AdapterView)
I have the onNothingSelected there, and it works in my if statement, I mean all I did was copy and paste and edit.
second error:
on }); at the end of my else if statement i get the error:
Syntax error, insert ";" to complete Statement
but everything is there. the statement is completed!
sp1.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelecteds(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
String reg_select = (sp1.getSelectedItem().toString());
if (reg_select.contentEquals("Southwest")){
sp2.setAdapter(sw_cit_adp);
sp2.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
String sw_cit_select = (sp2.getSelectedItem().toString());
if (sw_cit_select.contentEquals("Lake Charles")){
sp3.setAdapter(sw_lake_charles_adp); }
else if (sw_cit_select.contentEquals("Iowa")){
sp3.setAdapter(sw_iowa_adp); }
;}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
};});}
else if (reg_select.contentEquals("South Central")){
sp2.setAdapter(sc_cit_adp);
sp2.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
String sc_cit_select = (sp2.getSelectedItem().toString());
if (sc_cit_select.contentEquals("Lafayette")){
sp3.setAdapter(sc_lafayette_adp); }
else if (sc_cit_select.contentEquals("Gueydan")){
sp3.setAdapter(sc_gueydan_adp); }
;}
public void onNothingSelected(
AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
;
});
}
You have that method in the wrong place. You can't have an item selected and nto select anything, that's non-sensical.
You need to put the method under the listener, like the onItemSelected, but not inside onItemSelected.
This is what it should (basically) look like:
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// Your code to do something with the selected item
}
});
Oh, and you need to use the exact method name... it's onItemSelected not onItemSelecteds

Check selected item of ListView

I have a ListView and need to check, which item has been clicked.
The way I have it, it only shows my the selected item when I use the scrollwheel of my mouse (when testing it in the Eclipse emulator). When I test it on a real mobile device, it doesn't react at all.
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems);
listView.setAdapter(adapter);
listView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
myFunction(listView.getSelectedItem());
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Use setOnItemClickListener instead of setOnItemSelectedListener
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
//get value for listItems using position
String str =listItems[position];
myFunction(str);
}
});
I think better you need to use on onListItemClick listener
public void onListItemClick(ListView parent, View v, int position, long id)
{
parent.setItemChecked(position, parent.isItemChecked(position));
Toast.makeText(this, "You have selected" + items[position],30).show();
}

Categories

Resources