button does not click inside listview adatper - android

Here is a picture of my layout. I want to be able to click on the play button on the left hand side. I am current only able to click on the button on the last row. I tried to click on the other rows but only the last row would respond to the click. Following is my source codes:
custom_list_item_layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
android:descendantFocusability="blocksDescendants">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp">
<TextView
android:id="#+id/time_view_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="25dp"
android:layout_toRightOf="#+id/play_pause_id"
android:text="TextView"
android:textColor="#color/white"
android:textSize="30sp" />
<TextView
android:id="#+id/task_view_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_alignParentRight="true"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:text="TextView"
android:textColor="#color/white"
android:textSize="18sp"
/>
<ImageButton
android:id="#+id/play_pause_id"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_centerInParent="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="15dp"
android:background="#null"
android:scaleType="fitXY"
android:src="#drawable/play_btn"
/>
</RelativeLayout>
</RelativeLayout>
Custom list adapter class:
package com.example.study.studytimer;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import java.util.List;
public class CustomListAdapter extends ArrayAdapter<Task>{
Context mCtx;
int resource;
List<Task> taskList;
ImageButton playSelect;
private boolean isPressed = false;
public CustomListAdapter(Context mCtx, int resource, List<Task> taskList) {
super(mCtx, resource, taskList);
this.mCtx = mCtx;
this.resource = resource;
this.taskList = taskList;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(mCtx);
View view = inflater.inflate(resource, null);
TextView tvTask = view.findViewById(R.id.task_view_id);
TextView tvHour = view.findViewById(R.id.time_view_id);
playSelect = view.findViewById(R.id.play_pause_id);
playSelect.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
isPressed = !isPressed;
if (isPressed) {
playSelect.setImageResource(R.drawable.pause_btn);
} else {
playSelect.setImageResource(R.drawable.play_btn);
}
}
});
Task task = taskList.get(position);
Log.d("getViewCustomList", "getView: " + task.getTask_name());
tvTask.setText(task.getTask_name());
tvHour.setText(task.getNum_hours());
return view;
}
}

try to define playSelect inside the getView method for each item.
ImageButton playSelect = view.findViewById(R.id.play_pause_id);
each item must has its listener with a defferent objecct.

Use 'v' reference instead of 'playSelect' reference to change the drawable.
if (isPressed) {
v.setImageResource(R.drawable.pause_btn);
} else {
v.setImageResource(R.drawable.play_btn);
}
You also need to store value of isPressed with each item entry. You can set tag on button or you can create arraylist to store the value.

I tried your code it works fine. all rows are responding to the click the but only problem i found that you have decalred isPressed as global variable you should have used final one element array as local variable with each item for it to work properly. if this is not what you are looking please write in comments.you can check following example:
final boolean[] isPressed = {false};
playSelect.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
isPressed[0] = !isPressed[0];
if (isPressed[0]) {
Toast.makeText(mCtx, "Pressed", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(mCtx, "Not Pressed", Toast.LENGTH_SHORT).show();
}
}
});
Replace Toast with your action.

Related

How to fix OnItemClickListener on ListView

This is java class MainActivity.java
package com.example.mhn.intercoapp;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
import com.example.mhn.intercoapp.Adapters.EmployeeDirAdapter;
import com.example.mhn.intercoapp.static_class.EmployeeDir;
import java.util.ArrayList;
public class EmployeeDirectoryActivity extends AppCompatActivity {
ListView listView;
ImageView back_button;
EmployeeDir obj;
ArrayList <EmployeeDir> empDir ;
EmployeeDirAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_employee_directory);
listView = (ListView) findViewById(R.id.listView_emp_directory_xml);
back_button = (ImageView) findViewById(R.id.back_button_header_emp_directory_activity_xml);
obj = new EmployeeDir();
empDir = new ArrayList<>();
adapter = new EmployeeDirAdapter(getApplicationContext(),empDir);
back_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
startActivity(intent);
finish();
}
});
obj.setEmp_name("Hafiz Sadique Umar");
obj.setEmp_email("hsu#gmail.com");
obj.setEmp_contact_num("+923045607057");
empDir.add(obj);
empDir.add(obj);
empDir.add(obj);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> adapter, View v, int position,
long arg3)
{
String value = (String)adapter.getItemAtPosition(position);
Intent intent= new Intent(getApplicationContext(),DoneActivity.class);
startActivity(intent);
// assuming string and if you want to get the value on click of list item
// do what you intend to do on click of listview row
}
});
}
}
This is activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".EmployeeDirectoryActivity">
<RelativeLayout
android:id="#+id/relatice_layout_header_emp_directory_activity_xml"
android:background="#color/colorAccent"
android:layout_width="match_parent"
android:layout_alignParentTop="true"
android:layout_height="56dp">
<ImageView
android:id="#+id/back_button_header_emp_directory_activity_xml"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:src="#drawable/back_arrow_header"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Employee Directory"
android:textColor="#fff"
android:textSize="18dp"
android:textStyle="bold"
android:layout_centerInParent="true"
/>
</RelativeLayout>
<ListView
android:clickable="true"
android:id="#+id/listView_emp_directory_xml"
android:layout_below="#id/relatice_layout_header_emp_directory_activity_xml"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</RelativeLayout>
This is Adapter
package com.example.mhn.intercoapp.Adapters;
import android.content.Context;
import android.content.Intent;
import android.text.util.Linkify;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.mhn.intercoapp.MainActivity;
import com.example.mhn.intercoapp.R;
import com.example.mhn.intercoapp.static_class.EmployeeDir;
import java.util.ArrayList;
public class EmployeeDirAdapter extends BaseAdapter {
Context context;
private LayoutInflater inflater;
private ArrayList<EmployeeDir> menuData=new ArrayList<>();
public EmployeeDirAdapter(Context context, ArrayList<EmployeeDir>EmpDir)
{
this.context = context;
this.menuData=EmpDir;
inflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return menuData.size();
}
#Override
public Object getItem(int i) {
return menuData.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolderEmpDir holder;
if (convertView == null) {
holder = new ViewHolderEmpDir();
convertView = inflater.inflate(R.layout.emp_directory_list_row_layout, parent, false);
holder.name = (TextView) convertView.findViewById(R.id.name_textView_emp_directory_list_row_layout);
holder.email = (TextView) convertView.findViewById(R.id.email_textView_emp_directory_list_row_layout);
holder.phone = (TextView) convertView.findViewById(R.id.phone);
holder.pic = (ImageView) convertView.findViewById(R.id.emp_pic_emp_directory_list_row_layout);
holder.viewButton = (ImageView) convertView.findViewById(R.id.view_button_emp_dir_list_row_layout);
convertView.setTag(holder);
}
else
{
holder = (ViewHolderEmpDir) convertView.getTag();
}
holder.name.setText(menuData.get(position).getEmp_name());
holder.email.setText(menuData.get(position).getEmp_email());
holder.phone.setText(menuData.get(position).getEmp_contact_num());
//Linkify.addLinks(holder.email,Linkify.EMAIL_ADDRESSES);
//Linkify.addLinks(holder.phone,Linkify.PHONE_NUMBERS);
holder.pic.setImageResource(R.drawable.user_sign_up);
holder.viewButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context,MainActivity.class);
}
});
//downloadImage(position,holder);
return convertView;
}
// private void downloadImage(int position,ViewHolderShowAllBooking holder)
// {
// Picasso.with(context)
// .load("http://www.efefoundation.net/inklink/uploads/artist/"+menuData.get(position).getArtistId()+".jpg")
// .fit() // will explain later
// .into(holder.profile);
// }
static class ViewHolderEmpDir
{
TextView name;
TextView email;
TextView phone;
ImageView pic;
ImageView viewButton;
}
}
This is list_row_layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:weightSum="4"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_marginTop="5dp"
android:layout_centerHorizontal="true"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="70dp"
android:background="#color/colorAccent">
<ImageView
android:id="#+id/emp_pic_emp_directory_list_row_layout"
android:layout_marginTop="5dp"
android:layout_width="80dp"
android:layout_height="70dp"
android:layout_marginBottom="5dp"
android:src="#drawable/user_sign_up"/>
</RelativeLayout>
<RelativeLayout
android:background="#color/colorAccent"
android:layout_marginTop="5dp"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="3">
<TextView
android:id="#+id/name_textView_emp_directory_list_row_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:textColor="#fff"
android:textSize="18dp"
android:layout_alignParentLeft="true"
android:text="Husnain"/>
<TextView
android:id="#+id/phone"
android:layout_toRightOf="#id/name_textView_emp_directory_list_row_layout"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:autoLink="phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="***********"
android:textColor="#fff"/>
<TextView
android:id="#+id/email_textView_emp_directory_list_row_layout"
android:layout_alignParentLeft="true"
android:layout_below="#id/name_textView_emp_directory_list_row_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="mhn786#gmail.com"
android:autoLink="email"
android:layout_marginLeft="10dp"
android:textSize="18dp"
android:textColor="#fff"
/>
<ImageView
android:id="#+id/view_button_emp_dir_list_row_layout"
android:src="#drawable/view"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"/>
</RelativeLayout>
</LinearLayout>
When I apply OnItemClickListener it does not do any action like intent inside the func not called. I tried toast too but nothing shown on clicking the listView Items.
I also tried OnItemSlectedListener , it also not worked for me.
What is the problem here with this code. ?
Thanks Every One. Issue was with autolink on email and phone, I make their focusable=false but nothing happened.Then I remove it and guess what? Clicklistener also starts working.
The attribute android:clickable="true" in your ListView could be overriding all of click events of its child views, you should probably remove it:
<ListView
android:clickable="true" <!-- Remove this attribute -->
android:id="#+id/listView_emp_directory_xml"
android:layout_below="#id/relatice_layout_header_emp_directory_activity_xml"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

Issue in setting Textview's text in Custom Adapter for Android

I am not able to set textview's setText property inside getView() method of Custom Adapter.
I already tried below solutions but it does not work for me :
Solution One
Solution Two
Solution Three
Listview.xml file :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/imgLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:divider="#null"
android:dividerHeight="5dp"
android:scrollbars="none" />
</RelativeLayout>
Below is my keep_resing xml file :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/customRowLayout"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:layout_marginTop="20dp"
android:orientation="horizontal" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:orientation="vertical" >
<ImageButton
android:id="#+id/Btn"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:background="#drawable/thumb_down_disabled" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="#string/resing"
android:textColor="#357EC7"
android:textSize="10sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:orientation="vertical" >
<TextView
android:id="#+id/lyricsTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
<RelativeLayout
android:id="#+id/parentLayout"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:background="#drawable/playing_view"
android:orientation="horizontal" >
<TextView
android:id="#+id/helloTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="5dp"
android:singleLine="true"
android:text="Hello"
android:textColor="#357EC7"
android:textSize="14sp" />
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:background="#drawable/volume" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:orientation="vertical" >
<ImageButton
android:id="#+id/keepBtn"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginRight="10dp"
android:background="#drawable/thumb_up_disabled" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="#string/keep"
android:textColor="#357EC7"
android:textSize="10sp" />
</LinearLayout>
</RelativeLayout>
Below is my adapter code :
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.simpleframework.xml.stream.Position;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.content.res.Resources;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Handler;
import android.os.PowerManager;
import android.sax.StartElementListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
public class KeepReSingAdapter extends BaseAdapter implements OnClickListener {
/*********** Declare Used Variables *********/
private Activity activity;
private ArrayList data;
private static LayoutInflater inflater = null;
public Resources res;
SongCue tempValues = null;
int i = 0;
private ArrayList<SongCue> songCue;
private int cueIndex = 0;
private String selectedSongId;
private MediaPlayer mMediaPlayer;
ViewHolder holder;
View vi;
boolean right_button_flag, left_button_flag;
SessionManager session;
// int left_button_flag;
/************* CustomAdapter Constructor *****************/
public KeepReSingAdapter(Activity a, ArrayList d, Resources resLocal) {
/********** Take passed values **********/
activity = a;
data = d;
res = resLocal;
/*********** Layout inflator to call external xml layout () ***********/
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
selectedSongId = SessionManager.getInstance(
activity.getApplicationContext()).getString(
AppConstants.SONG_ID);
right_button_flag = false;
left_button_flag = false;
// Session class instance
session = new SessionManager(activity.getApplicationContext());
}
/******** What is the size of Passed Arraylist Size ************/
public int getCount() {
if (data.size() <= 0)
return 1;
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
/********* Create a holder Class to contain inflated xml file elements *********/
public static class ViewHolder {
public TextView lyricsTxt, helloTxt;
// public TextView text1;
public ImageButton leftBtn, rightBtn;
public RelativeLayout parentLayout;
public LinearLayout rowLayout;
// public TextView textWide;
// ImageView image;
}
/****** Depends upon data size called for each row , Create each ListView row *****/
public View getView(final int position, View convertView, ViewGroup parent) {
vi = convertView;
if (convertView == null) {
vi = inflater.inflate(R.layout.keep_resing, null);
holder = new ViewHolder();
holder.lyricsTxt = (TextView) vi.findViewById(R.id.lyricsTxt);
holder.helloTxt = (TextView) vi.findViewById(R.id.helloTxt);
holder.leftBtn = (ImageButton) vi.findViewById(R.id.reSingBtn);
holder.rightBtn = (ImageButton) vi.findViewById(R.id.keepBtn);
holder.parentLayout = (RelativeLayout) vi
.findViewById(R.id.parentLayout);
holder.rowLayout = (LinearLayout) vi
.findViewById(R.id.customRowLayout);
vi.setTag(holder);
} else {
holder = (ViewHolder) vi.getTag();
}
if (data.size() <= 0) {
} else {
/***** Get each Model object from Arraylist ********/
tempValues = null;
tempValues = (SongCue) data.get(position);
holder.lyricsTxt.setText(tempValues.getLyric());
List<Song> AllSong = SplashScreen_Activity.songs;
if (selectedSongId != null) {
for (Song tempsong : AllSong) {
if (tempsong.songId.equals(selectedSongId)) {
songCue = (ArrayList<SongCue>) tempsong.songCues.songCue;
}
}
} else {
}
holder.parentLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(activity.getApplicationContext(),
" " + position, Toast.LENGTH_SHORT).show();
holder.helloTxt.setText("Test");
Toast.makeText(activity.getApplicationContext(),
holder.helloTxt.getText().toString(),
Toast.LENGTH_LONG).show();
}
});
}
vi.setOnClickListener(new OnItemClickListener(position));
return vi;
}
#Override
public void onClick(View v) {
Log.v("CustomAdapter", "=====Row button clicked=====");
}
/********* Called when Item click in ListView ************/
private class OnItemClickListener implements OnClickListener {
private int mPosition;
OnItemClickListener(int position) {
mPosition = position;
}
#Override
public void onClick(View arg0) {
}
}
}
Problem is, I am not able to change the text view text to "Test".
Note : If I see the value in Toast by doing "holder.helloTxt.getText().toString()" it shows me "Test".
I don't know why this problem is arising.Any help will be highly appreciated.
Thanks
Finally I got the solution to my question and guess what it was a minor updation in the code.
I updated my code to :
final ViewHolder holder;
inside getView() of CustomAdapter instead of declaring it globally.
Once again, thanking you all for your answers.
It helped me to look into my code in detail.
Try this hope this helps
vi = convertView;
if (vi == null) {
vi = inflater.inflate(R.layout.xmlfile, null);
holder = new ViewHolder();
holder.helloTxt= (TextView) vi.findViewById(R.id.hearTxt);
holder.parentLayout= (RelativeLayout) vi
.findViewById(R.id.parentLayout);
vi.setTag(holder);
}else
holder=(ViewHolder)vi.getTag();
holder.parentLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(activity.getApplicationContext(),
" " + position, Toast.LENGTH_SHORT).show();
holder.helloTxt.setText("Test");// not required
// just change the value of text in your list(which you are passing to the adapter) at corresponding position and then notify the list
notifyDataSetChanged();
Toast.makeText(activity.getApplicationContext(),
holder.helloTxt.getText().toString(),
Toast.LENGTH_LONG).show();
}
});
return vi;
It will display the text "test" but when you scroll, it will again changed to its original value so you have to change the value in the list also which you are sending to the adapter
Also in your case the problem is, when you update the text of your TextView, it gets updated for the time being that's why you are getting the TextView text in your Toast. But in order to show the updated text in ListView, you have to notify the List. But when you do so, your TextView resets to original value. So all you have to do is to take a global String variable and intialise that to your initial value whatever you want like this
private String yourText="initial text"
and in getView() set your TextView text to this variable like this
holder.helloTxt.setText(yourText);
and when you click your parentLayout,just assign, this string the value you want and notify the list
yourText="test";
notifyDataSetChanged();
and this will do the trick. But remember this works only when you have single row, if you have multiple rows you have to take a String array.
Do you perform a holder = vi.getTag(); in case the convertView not is null?
This is necessary when a view is already created to reinitialise it in the state it was.
Problem is, I am not able to change the text view text to "Test".
you are doing it wrong. Let's say your adapter is made of String. What I would have expected, is
if (convertView == null) {
// inflate stuff
// create older
// set the older in the converView's tag
} else {
// retriewholder
}
// here you set the String in the holder at position:
String tmpString = getItem(position);
holder.helloTxt.setText(tmpString);
holder.parentLayout.setTag(position);
Inside the onClick, you can retrieve the position trough the tag object, and update the String in the dataset and call notifyDataSetChanged
Your problem is you didnot touch the parent layout. Make sure that your finger can touch the parentLayout, you can add padding for it. Or you can try
holder.helloTxt.setOnClickListener(...)
instead of parentLayout.
I've seen a lot of ways but for me the simple way is pass component view to adapter. Then you can change you need. See an example below:
Link

Android: can't apply onclick listener to button inside custom arrayadapter<String>

I'm trying to dynamically add click listeners to a custom array adapter for a list view. The idea is that I have list objects with text on them (eg "Group 1", "Group 2" etc.), and then when I click on the object a drop down appears ("expandable_section") with two buttons on it ("Members" and "Settings"). When I click on the members button I want to be able to access the specific text on the original group object. For example, clicking "Members" under the "Group 1" tab would give me access to the text "Group 1."
Here is my XML layout for the custom adapter object:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- The list element for GROUP SETTINGS tab sub-section -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:paddingBottom="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp" >
<ImageView
android:id="#+id/activeIcon"
android:layout_width="34dp"
android:layout_height="34dp"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="5dp"
android:layout_marginLeft="-7dp"
android:layout_marginTop="5dp"
android:gravity="center_vertical" />
<ImageView
android:id="#+id/modeIcon"
android:layout_width="34dp"
android:layout_height="34dp"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="5dp"
android:layout_marginRight="15dp"
android:layout_marginTop="5dp"
android:gravity="center_vertical" />
<TextView
android:id="#+id/groupName"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:gravity="center_vertical"
android:textColor="#000000"
android:textSize="22dp" />
</LinearLayout>
<!-- SPECIAL HIDDEN TOOLBAR FOR DEMO LIBRARY LIST MODES TODO: Change to whitelist/blacklist GROUP TAB! -->
<!-- EXPANDABLEFOR GROUP-SETTINGS FRAGMENT -->
<LinearLayout
android:id="#+id/expandableSubsection"
android:layout_width="fill_parent"
android:layout_height="90dp"
android:layout_marginBottom="-100dp"
android:background="#dddddd"
android:gravity="center"
android:visibility="gone" >
<Button
android:id="#+id/groupMembersButton"
android:layout_width="150dp"
android:layout_height="40dp"
android:layout_marginRight="20dp"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="Members" />
<Button
android:id="#+id/groupSettingsButton"
android:layout_width="150dp"
android:layout_height="40dp"
android:layout_marginRight="20dp"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="Settings" />
</LinearLayout>
</LinearLayout>
Here is the custom adapter code:
package com.sapphire.view;
import java.util.List;
import com.sapphire.model.Mode;
import com.sapphire.view.ModeAdapter.ModeHolder;
import android.R;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class GroupAdapter extends ArrayAdapter<String>{
private Context context;
private int resource;
public GroupAdapter(Context context, int resource, int textViewResourceId,
List<String> objects) {
super(context, resource, textViewResourceId, objects);
this.context = context;
this.resource = resource;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi =(LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(this.resource, null);
}
Button but= (Button) v.findViewById(R.id.button1);
if( but!=null){
but.setId(position);
but.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("customgroup clicked !");
}
});
}
return v;
}
}
and here is where the custom adapter is actually used to display the list of groups:
public void displayGroupsOptions(){
final ListView lView = (ListView) thisView.findViewById(R.id.GroupsView);
final ArrayList<String> groupNameList = new ArrayList<String>();
lView.setAdapter(null); //empty the list view
//Get all groups from database table
System.out.println("customgroup about to get all groups from database");
List<Group> allGroups = db.getAllGroups();
if(!allGroups.isEmpty()){
for (Group g: allGroups){
groupNameList.add(g.getName());
}
//Convert group arraylist to group array for use in adapter for list view
String[] groupNameArray = (String[]) groupNameList.toArray(new String[0]);
GroupAdapter adapter = new GroupAdapter(getActivity().getApplicationContext(), R.layout.group_list_row, R.id.groupName, groupNameList);
lView.setAdapter(adapter);
// Creating an item click listener, to open/close the mode options toolbar for each item
lView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
View groupOptions = view.findViewById(R.id.expandableSubsection);
// Creating the expand animation for the item
ExpandAnimation ea = new ExpandAnimation(groupOptions, ANIMATION_DELAY);
// Start the animation on the toolbar
groupOptions.startAnimation(ea);
}
});
}
}
Any help would be appreciated! I just can't figure out for the life of me how to get the proper text to show up on the tab (right now it's blank) and how to make the onclick listener actually attach and work. Thanks!
You can set tag for your Button in getView method to pass the position argument, and get the tag in th ClickListener.
btn.setTag(position);
#Override
public void onClick(View v) {
int position = (Integer)v.getTag();
}
Even you can set other view in the same adapter item as a tag and pass it to the listener.
use this piece of code where you used getView in adapter class
public class ViewHolder {
Button b;
}
and also this
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.xyz,
parent, false);
holder.b = (Button) convertView.findViewById(R.id.button1);
convertView.setTag(holder);
}
else{
holder = (ViewHolder) convertView.getTag();
}
Item it = item.get(position);
holder.b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("customgroup clicked !");
}
});
return convertView;
}

Soft key not appear while tapping on EditText which is inside listView and listView is in AleartBox

In my application customize Alear Box is there. In this alert box there is a customize ListView in which one TextView and one EditText is there. Every thing work fine and Alert Box coming on the screen perfectly.
But when I tap on Edit text to fill the characters into the Edit Text, Android Softkey on appearing. Therefore I am not able to insert and data into Edit Text.
My Code:-
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add Population"
android:onClick="getPopulation" />
</RelativeLayout>
alert_box.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/lView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
tools:listitem="#layout/list_view_resourse" >
</ListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/add"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Add" />
<Button
android:id="#+id/cancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel" />
</LinearLayout>
</LinearLayout>
list_view_resourse.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="#+id/district"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:textColor="#000000"
android:textStyle="bold" />
<EditText
android:id="#+id/population"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
</LinearLayout>
MainActivity
package com.exmp;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class MainActivity extends Activity implements OnClickListener{
private ArrayAdapter<Disrict> adapter;
private ArrayList<Disrict> disricts;
private ListView lView;
private Button add, cancel;
private AlertDialog dialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void getPopulation() {
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.alert_box, null);
lView = (ListView) view.findViewById(R.id.lView);
add = (Button) view.findViewById(R.id.add);
cancel = (Button) view.findViewById(R.id.cancel);
disricts = new ArrayList<Disrict>();
disricts.add(new Disrict(1, "Bangalore", 45.22));
disricts.add(new Disrict(1, "Gulbarga", 22.22));
adapter = new DistrictAdapter(MainActivity.this, R.layout.list_view_resourse, disricts);
lView.setAdapter(adapter);
cancel.setOnClickListener(this);
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Population");
alert.setView(view);
alert.setCancelable(false);
dialog = alert.create();
dialog.show();
}
public void getPopulation(View v){
getPopulation();
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.add:
break;
case R.id.cancel:
dialog.dismiss();
break;
}
}
}
Disrict
package com.exmp;
public class Disrict {
private int id;
private String strDistrict;
private double population;
public Disrict(int id, String strDistrict, double population) {
super();
this.id = id;
this.strDistrict = strDistrict;
this.population = population;
}
public int getId() {
return id;
}
public String getStrDistrict() {
return strDistrict;
}
public double getPopulation() {
return population;
}
#Override
public String toString() {
return strDistrict;
}
}
DistrictAdapter
package com.exmp;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
public class DistrictAdapter extends ArrayAdapter<Disrict> {
private final Context context;
private final int rowResourceId;
private final ArrayList<Disrict> disricts;
public DistrictAdapter(Context context, int resource,
ArrayList<Disrict> disricts) {
super(context, resource, disricts);
this.context = context;
this.rowResourceId = resource;
this.disricts = disricts;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Spinner spinnerPaymentType = null;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(rowResourceId, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.district);
textView.setText(disricts.get(position).getStrDistrict());
return rowView;
}
}
try this code in your listview
<ListView
android:id="#android:id/list"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:descendantFocusability="beforeDescendants"
/>
There are some problems which help me.
It better to use dynamical views rather than ListView when you have e Focasable view inside of Cell
related links:
EditText inside ListView lost focus,
EditText inside ListView will not stay focused,
Android :EditText loses content on scroll in ListView?
ettext.requestFocus();
ettext.postDelayed(new Runnable() {
#Override
public void run() {
InputMethodManager keyboard = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(ettext, 0);
}
},200);

How do I get a list of checked items from a custom ListView in Android?

I am experimenting with custom ListView's in Android. I have subclassed ArrayAdpater to hold an ArrayList of objects. The objects that get put in the ArrayAdapter come from a SQLite database.
In my ArrayAdapter class, I have overwritten the getView() method and inflate a custom view that puts an ImageView, 3 TextViews, and a Checkbox view on each row of the ListView.
I want to be able to get all the checked rows in my list do something with them, ie. remove them from the list and underlying database or edit the underlying data object represented by that row.
My problem is that I cannot seem to find a way to get my OnItemClickListener to work. The ListView displays fine but nothing happens when I click the row or if I check the checkbox and try to remove the checked items with my removeUnit() method. I've ran out of ideas.
What am I doing wrong here? Thanks for any help!
Here is my Activity
package com.mack.mylogger;
import android.os.Environment;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.util.*;
import android.database.Cursor;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.CheckBox;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import android.util.SparseBooleanArray;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import java.util.StringTokenizer;
import java.io.*;
import java.util.*;
public class ReportActivity extends Activity {
private Context context = null;
DBAdapter db = new DBAdapter(this); //my database adapter
ListView reportLV;
ArrayList<Integer> checkedPositions = new ArrayList<Integer>();
String TAG = "ReportActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_report);
// set up the list of finished units
reportLV = (ListView) findViewById(R.id.listview);
reportLV.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
ArrayList<RUnit> units = new ArrayList<RUnit>(); //init adapter with empty list
reportLV.setAdapter(new ReportLVAdapter(this, units));
reportLV.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.d(TAG, "Item is clicked at position: " + position);
CheckBox cb = (CheckBox) view.findViewById(R.id.checkbox);
cb.toggle();
if (cb.isChecked()) {
checkedPositions.add(position);
Log.d(TAG, "Item is checked at position: " + position);
} else if (!cb.isChecked()) {
checkedPositions.remove(position);
Log.d(TAG, "Item is unchecked at position: " + position);
}
}
});
// context
context = this.getBaseContext();
}
#Override
public void onResume() {
super.onResume();
updateListView();
}
private void updateListView() {
// get list of units
db.read();
RUnit[] allUnits = db.getAllUnits();
ArrayList<RUnit> finishedUnits = new ArrayList<RUnit>();
System.out.println("Units in database: " + allUnits.length);
RUnit unit;
for (int i=0; i<allUnits.length; i++) {
unit = allUnits[i];
// check status
if (unit.getStatus() == "Finished") {
finishedUnits.add(unit);
}
}
// update adapter with updated list of finished units
reportLV.setAdapter(new ReportLVAdapter(this, finishedUnits));
}
public void addUnit(View view) {
// method to run when "Add" button is clicked
Intent intent = new Intent(this, AddActivity.class);
startActivity(intent);
}
public void removeUnit(View view) {
// method to run when remove button is clicked
// loop thru the items in the ListView and remove checked items from the db
for (int i=0; i<checkedPositions.size(); i++) {
String unitStr = reportLV.getItemAtPosition(i).toString();
Log.d(TAG, "Removing unit: " + unitStr);
// remove unit from the database
db.deleteUnit(unitStr);
}
//update the LV
updateListView();
}
public void submit(View view) {
// do something here
}
}
Here is my ArrayAdapter
package com.mack.mylogger;
import android.app.Activity;
import android.util.*;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.CheckBox;
import android.view.View.OnClickListener;
import java.util.*;
public class ReportLVAdapter extends ArrayAdapter<RUnit> {
private final Activity context;
private final ArrayList<RUnit> units;
String TAG = new String("ReportLVAdapter");
static class ViewHolder {
public TextView serial;
public TextView status;
public TextView date;
public ImageView model;
public ImageView flag;
public CheckBox checkbox;
}
public ReportLVAdapter(Activity context, ArrayList<RUnit> units) {
super(context, R.layout.adapterlv_report, units);
this.context = context;
this.units = units;
Log.d(TAG, "Size of adapter array: " + units.size());
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
ViewHolder viewHolder = new ViewHolder();
if (rowView == null) {
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.adapterlv_report, null);
viewHolder.serial = (TextView) rowView.findViewById(R.id.serial);
viewHolder.model = (ImageView) rowView.findViewById(R.id.model);
viewHolder.status = (TextView) rowView.findViewById(R.id.status);
viewHolder.date = (TextView) rowView.findViewById(R.id.date);
viewHolder.flag = (ImageView) rowView.findViewById(R.id.flag);
viewHolder.checkbox = (CheckBox) rowView.findViewById(R.id.checkbox);
rowView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) rowView.getTag();
}
// Need to handle empty database situation
// get info from RUnit obj
RUnit unit = units.get(position);
String sn = unit.getSerialNumAsString();
String mdl = unit.getModel();
String stat = unit.getStatus();
// date
String d;
if (stat == "Finished") { d = unit.getFinishDate(); }
else if (stat == "Submitted") { d = unit.getSubmitDate(); }
else if (stat == "Shipped") { d = unit.getShipDate(); }
else { d = "NA"; }
// flags
String[] flags = unit.getFlags();
///// Set view values
// text values
viewHolder.serial.setText(sn);
viewHolder.status.setText(stat);
viewHolder.date.setText(d);
// set model image
if (mdl == "TK") {
viewHolder.model.setImageResource(R.drawable.tk_icon);
} else {
// must be carrier
viewHolder.model.setImageResource(R.drawable.carrier_icon);
}
// set flag image - only look for hold
for (int i=0; i<flags.length; i++) {
if (flags[i] == "hold") {
// set hold image
viewHolder.flag.setImageResource(R.drawable.hold_flag);
} else {
// no image
}
}
return rowView;
}
My activity XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:text="#string/main_title"
android:textAlignment="gravity"
android:textAllCaps="true"
android:textSize="36sp"
android:textStyle="bold" />
<TextView
android:id="#+id/subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/title"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:text="#string/report_subtitle"
android:textAlignment="gravity"
android:textAllCaps="true"
android:textSize="24sp"
android:textStyle="bold" />
<Button
android:id="#+id/button_submit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="center_horizontal"
android:text="#string/button_submit"
android:onClick="submit"
android:textSize="26sp" />
<LinearLayout
android:id="#+id/button_group"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#id/button_submit"
android:layout_gravity="center_vertical"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1.0"
android:onClick="addUnit"
android:text="#string/button_add_fg" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1.0"
android:text="#string/button_modify_fg" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1.0"
android:onClick="removeUnit"
android:text="#string/button_remove_fg" />
</LinearLayout>
<ListView
android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#id/button_group"
android:layout_below="#id/subtitle"
android:footerDividersEnabled="true"
android:headerDividersEnabled="true" />
</RelativeLayout>
My custom View for the rows XML
<?xml version="1.0" encoding="utf-8"?>
<GridLayout
xmlns:tools="http://schemas.android.com/tools"
android:columnCount="5"
android:rowCount="2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:id="#+id/model"
android:layout_row="0"
android:layout_column="0"
android:layout_margin="5dip"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/serial"
android:layout_row="0"
android:layout_column="1"
android:layout_columnSpan="2"
android:textSize="35dp"
android:textStyle="bold"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/status"
android:layout_row="1"
android:layout_column="0"
android:layout_columnSpan="2"
android:textSize="20dp"
android:textStyle="italic"
android:layout_marginLeft="10dip"
android:layout_marginRight="5dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/date"
android:layout_row="1"
android:layout_column="2"
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/flag"
android:layout_row="0"
android:layout_column="3"
android:layout_rowSpan="2"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<CheckBox
android:id="#+id/checkbox"
android:layout_row="0"
android:layout_column="4"
android:layout_rowSpan="2"
android:layout_gravity="right"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</GridLayout>
You can call getCheckedItemPositions() on the list view. This will return a SparseBooleanArray containing ALL items in the list and whether or not they are checked. Once you have that, you can loop through the array and use valueAt(i) to determine if an item is selected or not.
Here's a good article on this topic.
You can try this
SparseBooleanArray checkedPositions = lv.getCheckedItemPositions ();int size = checkedPositions.size ();for (int i=0 ; i<size ; i++) {
// We get the key stored at the index 'i'
int key = checkedPositions.keyAt (i);
// We get the boolean value with the key
Log.i (Tag, "checkedPositions(" + key + ")=" + checkedPositions.get (key));}

Categories

Resources