How to find the position of item in listview - android

I have created a Custom listView where i put some text and image but when i clicked in list view their is no response.I have created a custom Listview.
if any one known then please tell me.
my code is here
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final List<pdf> pdfList;
//the listview
final ListView listView;
//initializing objects
pdfList = new ArrayList<>();
listView = (ListView) findViewById(R.id.list);
//adding some values to our list
pdfList.add(new pdf("Book1", "First Year"));
pdfList.add(new pdf( "Book2", "First Year"));
//creating the adapter
ListAdptot adapter = new ListAdptot(this, R.layout.customlistview, pdfList);
//attaching adapter to the listview
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick (AdapterView < ? > adapter, View view, int position, long arg){
if(position==0)
{
Toast.makeText(MainActivity.this, "Position 0", Toast.LENGTH_SHORT).show();
}
else if(position==1)
{
Toast.makeText(MainActivity.this, "Position 1", Toast.LENGTH_SHORT).show();
}
}
}
);
}
}

Get list item positon click as below
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick (AdapterView < ? > adapter, View view, int position, long arg){
String value = listView.getItemAtPosition(position).toString();
}
}
);

If you have any clickable views inside your list item, then it will consume click event of your ListView item click.
Set android:focusable="false" on your Button or ImageButton if you have any.
Reference SO answer

Related

list item select set bgcolor but scroll down automatically list item set bgcolor changed

particular list item select to set bgcolor for list item ,when scroll down listview to automatically set color for other item, my sample code suggestion and help me.(this code v.setbackgroundcolor(Color.gray) using problem occur,so clear the problem)
public class MainActivity extends Activity {
ListView list;
ArrayAdapter<String> adapter;
ArrayList<String>listitem=new ArrayList<String>();
String val;
String[] list_number={"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list=(ListView)findViewById(R.id.listView1);
adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list_number);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position,
long id) {
String selectedFromList =(list.getItemAtPosition(position).toString());
if(listitem.isEmpty())
{
listitem.add(selectedFromList);
Set<String> primesWithoutDuplicates = new LinkedHashSet<String>(listitem);
listitem.clear();
listitem.addAll(primesWithoutDuplicates);
v.setBackgroundColor(Color.GRAY);
}
else
{
if( listitem.contains(selectedFromList))
{
listitem.remove(selectedFromList);
v.setBackgroundColor(Color.WHITE);
}
else
{
listitem.add(selectedFromList);
Set<String> primesWithoutDuplicates = new LinkedHashSet<String>(listitem);
listitem.clear();
listitem.addAll(primesWithoutDuplicates);
v.setBackgroundColor(Color.GRAY);
}
}
}
});
}
}

How to implement onItemClickListener on each item in the ListView to go to another activity?

How to implement onItemClickListener on each item in the ListView to go to another activity / to a new class?
public class MainActivity extends Activity{
ListView list;
String[] itemname ={
"Resturants",
"Coffee Shops",
"Hotels",
"Gas Stations",
"Hospitals",
"Airports",
"ATM",
"Cinemmas",
"Phamacies"
};
Integer[] imgid={
R.drawable.restaurantz,
R.drawable.coffeeshop,
R.drawable.hotel,
R.drawable.gaspump,
R.drawable.hospitalblue,
R.drawable.airporticon,
R.drawable.atm,
R.drawable.cinemma,
R.drawable.hospitalblue,
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
CustomListAdapter adapter = new CustomListAdapter(this, itemname, imgid);
list = (ListView) findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
}
}
inside onItemClick:
startActivity(new Intent(MainActivity.this, NewActivity.class));
if you need to differenciate the clicked item, you can use the position parameter, for example:
if (position == 0)
// do something
else
// do something else

android on click list item i want to select 2 item(tick mark)

I a newbie,i would like to ask on click list view i want to select (tick) mark on particular item,i know how to get position on click item.Here is my code below
public class MainActivity extends Activity {
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get ListView object from xml
listView = (ListView) findViewById(R.id.list);
// Defined Array values to show in ListView
String[] values = new String[]{"FirstName",
"FullName",
"Simple List View In Android"};
ArrayAdapter < String > adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
listView.setClickable(true);
// Assign adapter to ListView
listView.setAdapter(adapter);
// ListView Item Click Listener
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item index
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) listView.getItemAtPosition(position);
// Show Alert
Toast.makeText(getApplicationContext(),
"Position :" + itemPosition + " ListItem : " + itemValue, Toast.LENGTH_LONG)
.show();
listView.getAdapter().getView(position, null, null).performClick();
}
});
}
}
Thank you..Please
Check this sample project. It may help.
Use this if you need multiple selections
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
If you need a brief description visit
http://theopentutorials.com/tutorials/android/listview/android-multiple-selection-listview/

listView item click event not firing

I am having an issue with using a listview that when I click it nothing is happening. I would like for it when I click the listview item to make a toast so I know it is being clicked. I have been trying/researching for a while and nothing. Would anybody mind taking a look to see if I am missing something I just am overlooking? Many thanks in advance!
Here is my class:
public class MyCar extends Activity {
/**
* Called when the activity is first created.
*/
public ListView mylistView;
String carInfo;
private ArrayAdapter<String> mylistAdapter;
ArrayList<String> arrayListCar = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mycar);
mylistView = (ListView) findViewById(R.id.listView);
arrayListCar = new ArrayList<String>();
//Just had to remove setting this adapter 2 times. Took out line below to fix.
mylistView.setAdapter(mylistAdapter);
mylistView.setOnItemClickListener(new 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();
}
});
}
#Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
carInfo = trySomethin();
fillList();
}
public void fillList() {
String make = "";
String model = "";
String[] pieces = carInfo.split("\"");
make = pieces[3];
model = pieces[7];
ArrayList<String> carList = new ArrayList<String>();
carList.add(make + " " + model);
// Create ArrayAdapter using the car list.
mylistAdapter = new ArrayAdapter<String>(MyCar.this, android.R.layout.simple_list_item_single_choice, carList);
mylistView.setAdapter(mylistAdapter);
mylistAdapter.notifyDataSetChanged();
}
}
if you have any elements on listview item change this for them
android:focusable="false"
and if you are changing any elements visibility on runtime you have to handle focus programatically each time you change its visibility.
Try this
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item index
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) listView.getItemAtPosition(position);
// Show Alert
Toast.makeText(getApplicationContext(),
"Position :"+itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG)
.show();
}
});

how to add action to arraylist item in listview and how to get its position?

Hi I am working with android.I had created a listview using array list. Now how can I add Action to each list item and also how to get the list position of that item. Please help me, thanks
here is my code
public class MainActivity extends Activity {
private ListView lv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv=(ListView)findViewById(R.id.listView1);
ArrayList<String> list=new ArrayList<String>();
list.add("aaaa");
list.add("bbbb");
list.add("cccc");
list.add("dddd");
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
list);
lv.setAdapter(arrayAdapter);
}
}
You need to implement ListView setOnItemClickListener
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item index
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) listView.getItemAtPosition(position);
// Show Alert
Toast.makeText(getApplicationContext(),
"Position :"+itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG)
.show();
}
});
For more information go to Documentation.
you can also get the selected item of ListView in setOnItemClickListener using following code:
listView1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
int itemPosition = position;
String itemname = ((TextView)view).getText().toString();
Toast.makeText(getBaseContext(), "Item: " + itemname + " is at position: " + itemPosition, Toast.LENGTH_LONG).show();
}
});
As the listview cells position and the index of arraylist are same. So we can easily get the item from arraylist by the position of cell of listview.
listView1.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id)
{
int position=position;
String item=marraylist.get(position).toString();
}
}

Categories

Resources