I use the below code to create listview without listactivity.I need to know how to get the selected list item from this.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] myList = new String[] {"Hello","TEST","TEST","TEST4"};
ListView lv = new ListView(this);
lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,myList));
setContentView(lv);
}
Use onItemClickListener
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
http://d.android.com/reference/android/widget/AdapterView.html#setOnItemClickListener%28android.widget.AdapterView.OnItemClickListener%29
set listener on the list
lv.setOnItemSelectedListener(this);
then implement it
public void onListItemClick(ListView parent, View v, int position, long id) {
selection.setText(items[position]);
}
Related
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_campaign_list);
Campaign campaign_data[] = new Campaign[]
{
new Campaign(R.drawable.hlbb, "MSIG HLBB PA Extra"),
new Campaign(R.drawable.hlbb, "MSIG HLBB SSP Plus")
};
CampaignAdapter adapter = new CampaignAdapter(this, R.layout.listview_header_row, campaign_data);
listView1 = (ListView)findViewById(R.id.listView1);
listView1.setAdapter(adapter);
listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String item = ((TextView)view).getText().toString();
Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
}
});
}
I can run the code above without setonitemclicklistener function, when i add the setOnItemClickListener, it stopped working.What is the error?? I'm new in Android..Thx
I don't know how is your CampaignAdapter code, but you are trying to cast the row view which you are inflating into TextView. You should use your adapter to access the item using the position of element:
CampaignAdapter adapter = new CampaignAdapter(this, R.layout.listview_header_row, campaign_data);
listView1 = (ListView)findViewById(R.id.listView1);
listView1.setAdapter(adapter);
listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Campaign item = (Campaign) adapter.getItem(position);
// Now you can access to the campaing value that you want
// For instance, item.getText()
Toast.makeText(getBaseContext(), item.getText(), Toast.LENGTH_LONG).show();
}
});
I have a List View which is populated with countries I want to be able to click a an item and have it go to another activity in which i play an audio.
my list View:
public class MainActivity extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_selectable_list_item,getResources()
.getStringArray(R.array.countries)));
}
}
How do I go about adding a onClick just like how you would make one for a button??
ListView listView = getListView(); // hear bind your listview
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_selectable_list_item,getResources().getStringArray(R.array.countries)));
listView.setAdapter(ListAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> listView, View itemView, int itemPosition, long itemId)
{
}
});
}
}
Either use getListView() to access built in list view, and operate on that:
getListView().setOnItemClickListener();
Or, override onListItemClicked() in ListActivity:
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
//--do stuff--
}
override onItemClick() method
#Override
public void onItemClick(AdapterView<?> arg0, View view, int arg2,
long arg3) {
// TODO Auto-generated method stub
Intent intent = new Intent(getApplicationContext(), Activity2.class);
startActivity(intent);
}
Since your activity extends ListActivity
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> listView, View itemView, int itemPosition, long itemId)
{
Intent launchActivity = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(launchActivity);
}
});
U can use context.getlistview() to get the listview. and add setOnItemClickListener to the listview like this.
context.getlistview().setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Do whatever you want here
}
});
To make it handy just implement OnItemClickListener to your class
Add this in onCreate
ListView listView = getListView();
listView.setOnItemClickListener(this);
then override onItemClick like this
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id){
// your code
}
You have to use the setOnItemClickListener method.
Here is the code
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
// do your code here
}
});
I have the below code, but the onItemClickListener doesn't work,
can anyone help me what the problem could be?
I have also added the Override and the setClickable but the problem still exists.
public class Show extends Activity implements AdapterView.OnItemClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.show);
...//Some codes in here
adapter = new MyAdapter(this, hadithList);
list = (ListView) findViewById(R.id.list);
list.setAdapter(adapter);
list.setClickable(true);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), "clickedItemString", Toast.LENGTH_SHORT).show();
});
}
In your Activity, you are first implementing AdapterView.OnItemClickListener and than you are setting AdapterView.OnItemClickListener to your ListView. You should choose one of the methods, not both.
If you choose to stick up implementing OnItemClickListener, you should add this to your Activity :
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), "clickedItemString", Toast.LENGTH_SHORT).show();
}
and add this to your ListView : list.setOnItemClickListener(this);
If you choose to not implement OnItemClickListener just do :
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), "clickedItemString", Toast.LENGTH_SHORT).show();
}
});
This should work properly.
I'm trying to set an OnItemClickListener for my ListView in Android, but i can't get it to work.
This is what i have so far:
public class MenuFragment extends SherlockFragment
{
ArrayList<Item> items = new ArrayList<Item>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
ListView list = (ListView)view.findViewById(R.id.list_mainmenu);
// some code here where i add items to an ArrayList...
// Then i add the ArrayList to an EntryAdapter
EntryAdapter adapter = new EntryAdapter(this.getActivity().getBaseContext(), items);
list.setAdapter(adapter);
list.setClickable(true);
list.setOnItemClickListener(AdapterView.OnItemClickListener()) {
// ...
}
}
But this gives me an error on OnItemClickListener():
The method OnItemClickListener() is undefined for the type AdapterView.
So my qyestion is, how can i set an OnItemClickListener on my ListView??
check this code
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(),
((TextView) view).getText(), Toast.LENGTH_SHORT).show();
}
});
make sure you have imported correct packages:
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
try this
list.setOnItemClickListener(new AdapterView.OnItemClickListener()) {
// ...
}
You should implement a customAdapter for having more control on your listView, Here is the link after visiting this you should be able to do what is required. Or you can have this code to quickly do what you need.
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view, int arg2,long itemID) {
}
});
Try this:
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
// ...
}
});
try this one
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
}
});
I have designed a custom listview. In that view i have one textview and two edittext fields. when i am clicking on the individual rows in listview that perform another activity(i.e another page will open). But i cannot perform onclick action on my listview.
i have wrote some code but its not working. i have shown that code as below.
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.sharefolioedit);
getList();
lv.setAdapter(new EfficientAdapter(this));
lv = getListView();
lv.setClickable(true);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parentView, View childView,
int position, long id) {
Intent intent = new Intent(CategoryList.this,AddSubCategoryList.class);
startActivity(intent);
}
}
This is working fine for me :
this.setListAdapter(new EfficientAdapter(this));
this.getListView().setOnItemClickListener ( new AdapterView. OnItemClickListener ( )
{
public void onItemClick ( AdapterView <?> aView, View v, int position, long id )
{
Intent intent = new Intent(CategoryList.this,AddSubCategoryList.class);
startActivity(intent);
}
});