Adding an onclicklistener to listview (android) - android

I've managed to implement a great listview that I found here http://www.learn-android.com/2011/11/22/lots-of-lists-custom-adapter/comment-page-1/
but I can't seem to add an onclicklistener
I just want to be able to do an action when I click on the row, with the data that the row contains of course
any ideas?
thanks
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.liste);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
// Setup the list view
final ListView prestListView = (ListView) findViewById(R.id.list);
final prestationAdapterEco prestationAdapterEco = new prestationAdapterEco(this, R.layout.prestation);
prestListView.setAdapter(prestationAdapterEco);
// Populate the list, through the adapter
for(final prestationEco entry : getPrestations()) {
prestationAdapterEco.add(entry);
}
prestListView.setClickable(true);
prestListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
Object o = prestListView.getItemAtPosition(position);
String str=(String)o;//As you are using Default String Adapter
Toast.makeText(getApplicationContext(),str,Toast.LENGTH_SHORT).show();
}
});
}

listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Object o = prestListView.getItemAtPosition(position);
prestationEco str = (prestationEco)o; //As you are using Default String Adapter
Toast.makeText(getBaseContext(),str.getTitle(),Toast.LENGTH_SHORT).show();
}
});

If your Activity extends ListActivity, you can simply override the OnListItemClick() method like so:
/** {#inheritDoc} */
#Override
protected void onListItemClick(ListView l, View v, int pos, long id) {
super.onListItemClick(l, v, pos, id);
// TODO : Logic
}

The prestListView.getItemAtPosition(position); returns the UI widget: Text, Icon, ...
Try this instead:
Object o = prestationAdapterEco.getItemAtPosition(position);
or
Object o = arg0.getItemAtPosition(position);
Get the object from the adapter. Not from the list-view.
2.
Object o is a prestationEco object. Not a String.

list.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});

You are doing
Object o = prestListView.getItemAtPosition(position);
String str=(String)o;//As you are using Default String Adapter
The o that you get back is not a String, but a prestationEco so you get a CCE when doing the (String)o

Try this:
list.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3)
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});

Related

listView filter search

I was looking to filter a listView by an editText.
My ListView is populate by an ArrayString, and when I filter it everything goes right. But I have a problem to recognize the element clicked. When I filter the listView and I click on the item filtered I want to have the number of the row in the original arrayString in order to make an Intent. Whit my code when I click the item in the filtered arrayString the row is 0 and it's not correct. If I search "Anche Se Pesa" I want to return the row 2 like the position in the original ArrayString.
<string-array name="titoli">
<item>An Val Dondona</item>
<item>Anche Mio Padre</item>
<item>Anche Se Pesa</item>
<item>Andremo In Francia</item>
<item>Aprite Le Porte</item>
<item>Arda Berghem</item>
<item>Ascoltate Amici Cari</item>
<item>Ave, O Vergine</item>
<item>Aver na Figlia Sola Soletta</item>
<item>Ancora sti quatro</item>
<item>Andouma Prou</item>
</string-array>
Here Is the code
public class activity_titoli extends AppCompatActivity {
Button btt_backHome;
ListView lV_titoli;
EditText eT_search;
private ArrayAdapter<CharSequence> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_titoli);
btt_backHome = (Button) findViewById(R.id.btt_backHome);
lV_titoli = (ListView) findViewById(R.id.lV_titoli);
eT_search = (EditText) findViewById(R.id.eT_search);
adapter = ArrayAdapter.createFromResource(this, R.array.titoli, android.R.layout.simple_list_item_1);
lV_titoli.setAdapter(adapter);
final Intent to_Home = new Intent (this , Activity_Main.class);
eT_search.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable arg0) {
// 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 onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
(activity_titoli.this).adapter.getFilter().filter(arg0);
}
});
btt_backHome.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(to_Home);
}
});
lV_titoli.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Log.e("Element: " , Integer.toString(i));
}
});
}
}
Try this code :
lV_titoli.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Resources res = getResources();
List<CharSequence> list = Arrays.asList(res.getStringArray(R.array.titoli));
Log.e("Element: " , Integer.toString(list.indexOf(adapter.filgetItem(i))));
}
});
Hope this helps

How to get the value of the selected item from listview

I want to retrieve all the data of the selected row from listview.
this is what I have tried so far.....
listview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Object listItem = listview.getItemAtPosition(arg2);
}
});
now i don't know, how to get the elements from the Object.
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
Object listItem = (Object)parent.getItemAtPosition(position);
}
Usually the Object depends on what type of data are you sending into your adapter.

same item change listener for multiple spinners

I have 8 spinners:
pl1sp = (Spinner) offersdialoglayout.findViewById(R.id.pl1offer);
pl2sp = (Spinner) offersdialoglayout.findViewById(R.id.pl2offer);
pl3sp = (Spinner) offersdialoglayout.findViewById(R.id.pl3offer);
pl4sp = (Spinner) offersdialoglayout.findViewById(R.id.pl4offer);
and
pl1scores = (Spinner) scoresdialoglayout.findViewById(R.id.pl1scores);
pl2scores = (Spinner) scoresdialoglayout.findViewById(R.id.pl2scores);
pl3scores = (Spinner) scoresdialoglayout.findViewById(R.id.pl3scores);
pl4scores = (Spinner) scoresdialoglayout.findViewById(R.id.pl4scores);
I need to set them 2 listeners. One for first 4 spinner and one for last 4. I don't want to repeat code like this:
pl1sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
checkOffersSum(); // same method for first 4 spinners. for last 4 spinners is checkScoresSum()
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub.
}
});
pl2sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
checkOffersSum();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub.
}
});
and so on..
So I need to write listener only twice, first for first 4 spinners and second for last 4 spinners. Thanks
define one listener for first 4 spinner
Spinner[] offerspinners = {R.id.pl1offer, R.id.pl2offer, R.id.pl3offer, R.id.pl4offer};
for(Spinner offer : offerspinners){
offersdialoglayout.findViewById(offer).
setOnItemSelectedListener(offerListener);
}
AdapterView.OnItemSelectedListener offerListener =
new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch(parent.id){
case R.id.pl1offer:
// offer 1 related code
break;
case R.id.pl2offer:
// offer 2 related code
break;
}
// common code
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub.
}
}
than define 2nd listener with same code here
Spinner[] scorespinners = {R.id.pl1scores, R.id.pl2scores, R.id.pl3scores, R.id.pl4scores};
for(Spinner score : scorespinners){
scoresdialoglayout.findViewById(score).
setOnItemSelectedListener(scoreListener);
}
AdapterView.OnItemSelectedListener scoreListener =
new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch(parent.id){
case R.id.pl1scores:
// score 1 related code
break;
case R.id.pl2scores:
// score 2 related code
break;
}
// common code
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub.
}
}
You can manage multiple spinners using a single listener. Please check this answer. It deals with the same issue. Hope it helps.
Follow these steps:
Implement OnItemSelectedListener in your class and override the unimplemented method onItemSelected(AdapterView<?> arg0, View view, int arg2, long arg3).
use pl1sp.setOnItemSelectedListener(this) and do this for all spinners.
Use the overridenmethod onItemSelected(AdapterView<?> arg0, View view, int arg2, long arg3) like this:
#Override
public void onItemSelected(AdapterView<?> arg0, View view, int arg2, long arg3) {
int id = view.getId();
if(id==R.id.pl1offer || id==R.id.pl2offer || id==R.id.pl3offer ||id==R.id.pl4offer){
// do this
}
else{
//do this
}
}
What you can do is create an array containing all your spinners then use a for loop to attach each individual spinner to setItemSelected() method ie
Spinner[] spinner = {pl1sp, pl2sp, pl3sp, pl4sp};
for(Spinner individualSpinner : spinner){
individualSpinner.etOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
//Custom method here
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub.
}
});
Spinner[] spinners = {pl1scores, pl2scores, pl3scores, pl4scores};
for(Spinner individualSpinner2 : spinner){
individualSpinner.etOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
//Custom method here
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub.
}
});

Android - Get Selected Item from Dynamically created Spinner

I create a Spinner depending on some rules of my app. So, its created dynamically. How can I retrieve which item was selected?
Spinner mySpinner = (Spinner)linearMoreInfo.getChildAt(1);
Using this code to retrieve the created dynamically spinner. But got no idea of how I can handle its click/select item.
Any helps?
I hope it works for you:
mySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
{
Object obj = parent.getItemAtPosition(pos);
...
}
public void onNothingSelected(AdapterView<?> parent) { }
});
Try this Code...
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int pos, long arg3) {
// TODO Auto-generated method stub
selected_item_position = pos;
selected_item = spinner.getSelectedItem().toString();
// You can insert your code...
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});

onListItemClick callback isn't activated

public class List_Items extends ListActivity{
private ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_item);
List <ImageAndText> total_list=new ArrayList<ImageAndText>();
ListView lv = (ListView) this.findViewById(android.R.id.list);
lv.setAdapter((ListAdapter) new ImageAndTextListAdapter(this, total_list));
getListView().setTextFilterEnabled(true);
//////////////////////////////////////////////////////
Button btn=(Button) findViewById(R.id.button_sync);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast message=Toast.makeText(getApplicationContext(), "click the list_item", Toast.LENGTH_LONG);
message.show();}
});
}
public void onListItemClick(ListView parent, View v, int position, long id)
{
Toast.makeText(getApplicationContext(), "You have selected " +(position+1)+"th item",
Toast.LENGTH_SHORT).show();}
}
I have this list_item which is extends the listactivity. However, when I click on one of the row, the callback onListItemClick didn't get activated. Why is that? I don't need to anything with the adapter on this right?
}
You have to use the following in all the widgets of xml which you are inflating for your custom ListView.
android:focusable="false"
android:focusableInTouchMode="false"
Hope it helps
u havent register ur onItemClick with Listview.
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
Log.i("List Clicked ....", "List Clicked...");
}
});
or
listView.setOnItemClickListener(listener);
public OnItemClickListener listener = new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Log.i("List Clicked ....", "List Clicked...");
}
};
Can you try this
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Log.i("You clicked the list");
}

Categories

Resources