I was wondering if I can check items (checkbox) from ListView and have something with the selected items. But I dont want to use 'ListView.OnItemClickListener' because I have a separate functionality for clicking the list item. Something like 'CheckBox.OnClickListener' where I can grab all the list items that are checked. Any help is highly appreciated. Below is the code I am currently having, not functional though:
public class MainActivity extends AppCompatActivity {
ListView list;
GoodAdapter imageAdapter;
CheckBox cb;
#Override
protected void onCreate(Bundle savedInstanceState) {
//populates the details as in imageBeanArray
imageAdapter = new GoodAdapter(this, imageBeanArray);
list.setAdapter(imageAdapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v,
final int position, long id) {
//does something
}
//throws NPE
cb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (cb.isSelected()) {
checkedArray.add((ImageBean) list.getItemAtPosition(list.getSelectedItemPosition()));
}
}
});
}
GoodAdapter.java:
All other variables initialized
public class GoodAdapter extends BaseAdapter{
boolean[] itemChecked;
CheckBox[] checkBoxArray;
public GoodAdapter(Context context, ArrayList<ImageBean> imageBean) {
mInflater = LayoutInflater.from(context);
mImageBeans = imageBean;
itemChecked=new boolean[imageBean.size()];
checkBoxArray=new CheckBox[itemChecked.length];
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view;
if(convertView == null) {
view = mInflater.inflate(R.layout.good_adapter, parent, false);
holder = new ViewHolder();
holder.imageView = (ImageView)view.findViewById(R.id.image);
holder.name = (TextView)view.findViewById(R.id.docName);
holder.imageMore = (TextView)view.findViewById(R.id.imageMore);
holder.imageMore.setVisibility(View.GONE);
holder.checkbox=(CheckBox)view.findViewById(R.id.checkBox);
view.setTag(holder);
} else {
view = convertView;
holder = (ViewHolder) view.getTag();
holder.checkbox.setChecked(false);
if (itemChecked[position]){
holder.checkbox.setChecked(true);
mImageBeans.get(position).setSelected(true);
}
else
holder.checkbox.setChecked(false);
}
ImageBean imageBean = mImageBeans.get(position);
Bitmap bm = BitmapFactory.decodeByteArray(imageBean.getImage(), 0, imageBean.getImage().length);
holder.imageView.setImageBitmap(bm);
holder.name.setText(imageBean.getDocName());
if (imageBean.isMoreThanOne()!=0)
holder.imageMore.setVisibility(View.VISIBLE);
holder.checkbox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (holder.checkbox.isChecked())
itemChecked[position] = true;
else
itemChecked[position] = false;
}
});
checkBoxArray[position].setChecked(itemChecked[position]);
checkBoxArray[position].setOnCheckedChangeListener(mListener);
return view;
}
CompoundButton.OnCheckedChangeListener mListener = new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
itemChecked[(Integer)buttonView.getTag()] = isChecked; // get the tag so we know the row and store the status
}
};
private class ViewHolder {
public ImageView imageView;
public TextView name,imageMore;
public CheckBox checkbox;
}
Good Adapter.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="64dip"
android:layout_marginTop="5dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:weightSum="1"
android:id="#+id/imageList">
<CheckBox
android:id="#+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:focusable="false"
android:focusableInTouchMode="false"
/>
<ImageView
android:id="#+id/image"
android:scaleType="centerCrop"
android:layout_marginLeft="2dp"
android:layout_gravity="center_vertical"
android:layout_width="90dip"
android:layout_height="match_parent"
android:background="#drawable/image_border"
android:padding="4dp"
android:cropToPadding="true"
/>
<TextView
android:id="#+id/docName"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:textColor="#color/black"
android:textSize="20dp"
android:gravity="center_vertical"/>
<TextView
android:id="#+id/imageMore"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginRight="5dp"
android:background="#drawable/more"/>
</LinearLayout>
Please help me.I didnt find any useful tutorials/solutions.
From my experience you can't have a listener on component of the row and a listener on the row itself if you do that one will take advantage on the other that will never be called.
Related
I know here we have a lot of questions like mine, but I don't know why none works for me.
My objective: I have an AlertDialog with a ListView with a check box in each row, I can select some of the items, and I wish to make an ArrayList with the elements selected.
For that reason, I'm calling the SetOnclickListener, I put a Log inside the method, but does nothing.
I tried with focusable and clickable almost everywhere, but my Log doesn't appear.
Here My alert Dialog
private void callAdditionalDialog() {
LayoutInflater layoutInflater = LayoutInflater.from(ConfigProductActivity.this);
final View additionalView = layoutInflater.inflate(R.layout.dialog_additional, null);
additionalView.setFocusable(true);
// set the custom dialog components - text, buttons, accountants
TextView titleDialog = (TextView) additionalView.findViewById(R.id.title_additional);
titleDialog.setTypeface(boldFont);
Button buttonAccept = (Button) additionalView.findViewById(R.id.button_accept);
buttonAccept.setTypeface(boldFont);
Button buttonCancel = (Button) additionalView.findViewById(R.id.button_cancel);
buttonCancel.setTypeface(boldFont);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ConfigProductActivity.this);
alertDialogBuilder.setView(additionalView);
final AlertDialog alertD = alertDialogBuilder.create();
alertD.setCanceledOnTouchOutside(false);
//Fill object of additional
final ListView additionalListView = (ListView) additionalView.findViewById(R.id.list_additional);
TextView additionalNotFound = (TextView) additionalView.findViewById(R.id.additional_not_found);
if (!withoutModifiers){
additionalAdapter = new AdditionalAdapter(ConfigProductActivity.this, additionalList);
additionalListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
additionalListView.setAdapter(additionalAdapter);
final ArrayList<ModifierEntity> modifierList = new ArrayList<ModifierEntity>();
additionalListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent,
View view, int position, long id) {
Object modifier = additionalListView.getAdapter().getItem(position).toString();
Log.d(TAG, "SOMETHIIIIING");
}
});
}
else{
additionalListView.setVisibility(View.GONE);
additionalNotFound.setVisibility(View.VISIBLE);
additionalNotFound.setTypeface(font);
buttonCancel.setVisibility(View.GONE);
}
//End of fill object of additional
buttonCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
additionalBox.setEnabled(true);
alertD.dismiss();
}
});
buttonAccept.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//additional.setText(additionalAdapter.getCount());
additionalBox.setEnabled(true);
alertD.dismiss();
}
});
alertD.show();
}
Here my adapter:
public class AdditionalAdapter extends ArrayAdapter {
private static String TAG = AdditionalAdapter.class.getName();
private List<ModifierEntity> originalData = null;
private ConfigProductActivity activity;
private Typeface font;
private Typeface boldFont;
private static ModifierEntity modifier;
public AdditionalAdapter (ConfigProductActivity activity, List<ModifierEntity> listArray){
super(activity, R.layout.additional_item);
this.activity = activity;
this.originalData = listArray ;
font = Typeface.createFromAsset(activity.getAssets(),"HelveticaNeueThn.ttf");
boldFont = Typeface.createFromAsset(activity.getAssets(), "avgardm.ttf");
}
public static class Row
{
public TextView labelName;
public TextView labelPrice;
public CheckBox check;
}
#Override
public int getCount() {
return originalData.size();
}
#Override
public ModifierEntity getItem(int position) {
return originalData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final int colorFont = activity.getResources().getColor(R.color.brown_tataki);
final Row holder;
View rowView = convertView;
// reuse views
if (convertView == null) {
holder = new Row();
LayoutInflater inflater = LayoutInflater.from(activity);
rowView = inflater.inflate(R.layout.additional_item, null);
rowView.setClickable(true);
//rowView.setFocusable(false);
// configure view holder
holder.labelName = (TextView) rowView.findViewById(R.id.additional_name);
holder.labelPrice = (TextView) rowView.findViewById(R.id.additional_price);
holder.check = (CheckBox) rowView.findViewById(R.id.additional_check);
rowView.setTag(holder);
}
else {
holder = (Row) convertView.getTag();
// rowView.setClickable(true);
}
final ModifierEntity itm = originalData.get(position);
holder.labelName.setText(itm.getModifier_name());
holder.labelName.setTypeface(font);
holder.labelName.setTextColor(colorFont);
holder.labelPrice.setText(GlobalParameters.CURRENCY.concat(String.valueOf(itm.getModifier_cost())));
holder.labelPrice.setTypeface(boldFont);
holder.labelPrice.setTextColor(colorFont);
return rowView;
}
}
Here My Dialog
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:background="#color/white">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/orange_tataki"
android:text="#string/additional_title"
android:textColor="#color/white"
android:textSize="20sp"
android:gravity="center"
android:id="#+id/title_additional"
android:padding="5dp"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/title_additional"
android:gravity="center"
android:background="#color/white"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/list_additional"
android:divider="#color/brown_tataki"
android:dividerHeight="0.5dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/list_additional"
android:gravity="center"
android:padding="10dp"
android:visibility="gone"
android:textColor="#color/brown_tataki"
android:id="#+id/additional_not_found"
android:text="#string/additional_not_found"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/buttons"
android:layout_marginBottom="10dp"
android:layout_below="#+id/additional_not_found"
android:gravity="center"
android:layout_marginTop="20dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:id="#+id/button_cancel"
android:background="#color/green_tataki"
android:text="#string/button_cancel"
android:textSize="15sp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button_accept"
android:padding="10dp"
android:background="#color/green_tataki"
android:text="#string/button_accept"
android:textSize="15sp"
android:layout_marginLeft="15dp"
/>
</LinearLayout>
</RelativeLayout>
And Here my item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants"
>
<TextView
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="QUESO"
android:singleLine="true"
android:padding="10dp"
android:textColor="#color/brown_tataki"
android:id="#+id/additional_name"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/additional_price"
android:padding="10dp"
android:text="Bs. 500"
android:textColor="#color/brown_tataki"
android:layout_toRightOf="#id/additional_name"
android:layout_marginLeft="10dp"
/>
<CheckBox
android:id="#+id/additional_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="8dp" />
Using interfaces I can solve my problem, this link provided me the solution, and bassically consist in create an interface and implement in my activity.
Like this:
1.- Interface
public interface MyListener {
void folderClicked();
}
2.- Implements the interface in the activity
public class ActivityA extends Activity implements MyListener
3.- You will have to auto override the method folderClicked it will look like this:
#Override
protected void folderClicked() {
// Do your stuff here.
}
4.- Send the activity listener to the adapter with constructor like this:
MyAdpater adapter = new MyAdpater(ActivityA.this);
5.- Your adapter class your code should be like this:
public class TimeLineAdapter extends BaseAdapter {
private MyListener mListener;
public TimeLineAdapter(MyListener listener) {
super();
mListener = listener;
}
holder.iconImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mListener.onFolderClicked()
//code to do stuff when the image is clicked
}
I currently have ListFragment with a ArrayAdapter class
The codes are below.
1)I want to implement a checkbox from listview main xml to check all the checkboxes that are present in custom list row xml which is inflated using Array adapter.
2)how to refresh view in this code by clearing all checkboxes when a button is clicked.
listview_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="10"
android:background="#fff2fff2" >
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="10"
android:choiceMode="multipleChoice" >
</ListView>
<TextView
android:id="#android:id/empty"
android:layout_width="wrap_content"
/>
<LinearLayout
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="#+id/btnin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="In" />
<Button
android:id="#+id/btndelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/checkAll"
android:checked="false" />
</LinearLayout>
</RelativeLayout>
custom_row.xml
<?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="wrap_content" >
<ImageView
android:id="#+id/icon"
android:layout_width="50dp"
android:layout_height="50dp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/paackage"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent">
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Adapter class
public class FileAdapter extends ArrayAdapter<String>{
private List<String> filelist = null;
private Context context;
CheckBox checkAll;
public FileAdapter(Context _context, int _resource,List<String> _filelist) {
super(_context,_resource,_filelist);
this.context = _context;
this.filelist = _filelist;
this.itemChecked = new boolean[_filelist.size()];
}
private class ViewHolder {
TextView Name;
TextView pName;
CheckBox ck1;
ImageView iconview;
}
#Override
public int getCount() {
return ((null != filelist) ? filelist.size() : 0);
}
#Override
public String getItem(int position) {
return ((null != filelist) ? filelist.get(position) : null);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
View view = convertView;
if (null == view) {
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.custom_row, null);
holder = new ViewHolder();
holder.Name = (TextView) view.findViewById(R.id.name);
holder.pName = (TextView) view
.findViewById(R.id.paackage);
holder.ck1 = (CheckBox) view.findViewById(R.id.checkBox1);
holder.iconview = (ImageView) view.findViewById(R.id.app_icon);
view.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.appName.setText();
holder.packageName.setText();
holder.iconview.setImageDrawable();
holder.ck1.setChecked(false);
if (itemChecked[position])
holder.ck1.setChecked(true);
else
holder.ck1.setChecked(false);
holder.ck1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (holder.ck1.isChecked()) {
itemChecked[position] = true;
} else {
itemChecked[position] = false;
}
}
});
return view;
}
}
ListFragment class
public class FragmentA extends ListFragment implements View.OnClickListener {
public ArrayList<String> filelist;
Button in, delete;
ListView lv;
CheckBox cb;
private FileAdapter listadaptor = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
filelist = new ArrayList<String>();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View inflatedView = inflater.inflate(R.layout.fragment_app_restore, container, false);
in = (Button) inflatedView.findViewById(R.id.btnin);
delete= (Button) inflatedView.findViewById(R.id.btndelete);
lv = (ListView) inflatedView.findViewById(android.R.id.list);
empty = inflatedView.findViewById(android.R.id.empty);
//calling asynctask to load files here
delete.setOnClickListener(this);
in.setOnClickListener(this);
return inflatedView;
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
cb = (CheckBox) v.findViewById(R.id.checkBox1);//this is the checkbox in row only
cb.performClick();
if (cb.isChecked()) {
} else {
}
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btndelete:
break;
case R.id.btnin:
break;
}
}
private class LoadFiles extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... params) {
filelist = GetFiles(root_path);
if (filelist != null) {
listadaptor = new AppRestoreFileAdapter(getActivity(),
R.layout.custom_row, filelist);
} else {
// lv.setEmptyView(empty);
}
return null;
}
#Override
protected void onPostExecute(Void result) {
lv.setAdapter(listadaptor);
progress.dismiss();
super.onPostExecute(result);
}
}
}
I've read some SO questions like this and this but still couldn't figure out whats wrong with my code.
I have a ListFragment, and each row has a TextView and a CheckBox.
Clicking the CheckBox is working, but clicking on the TextView does nothing, and OnListItemClick doesn't get called. I've also tried to dynamically add an OnClickListener, which make it work, but it's not the correct way to do it, and it also lacks the GUI feedback of the click (item being "highlighted" for a sec).
This is my textview_with_checkbox.XML I'm using for each item:
<?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="wrap_content"
android:clickable="true"
android:focusable="true"
android:gravity="left"
android:descendantFocusability="blocksDescendants"
android:orientation="horizontal" >
<TextView
android:id="#+id/textview_event_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="3dp"
android:paddingTop="5dp"
android:focusable="true"
android:singleLine="false" />
<CheckBox
android:id="#+id/checkbox_for_event_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.05"
android:focusable="false"
android:clickable="false" />
</LinearLayout>
now the code:
#Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
displayEventsLogFiles();
}
#Override
public void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
String chosenItem = (String) getListAdapter().getItem(position);
mCallBack.onEventItemSelected(chosenItem);
}
static class myCustomAdapterViewHolder
{
public TextView eventName;
public CheckBox eventCheckBox;
}
private class myCustomAdapter extends ArrayAdapter<String>
{
private ArrayList<String> sortedList;
private Context m_oContext;
List<String> m_oValues = null;
public myCustomAdapter(Context cont, int viewResId, List<String> objects)
{
super(cont, viewResId, objects);
m_oContext = cont;
m_oValues = objects;
sortedList = new ArrayList<String>();
for (String str : objects)
sortedList.add(str);
java.util.Collections.sort(sortedList);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View l_oRowView = convertView;
myCustomAdapterViewHolder l_oInitializedViewHolder = null;
final int l_nPosition = position;
// Use convertView if possible, otherwise inflate a view:
if (l_oRowView == null)
{
LayoutInflater inflater = (LayoutInflater) m_oContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
l_oRowView = inflater.inflate(R.layout.textview_with_checkbox, parent, false);
// PlaceHolder pattern:
final myCustomAdapterViewHolder l_oViewHolder = new myCustomAdapterViewHolder();
l_oViewHolder.eventName = (TextView) l_oRowView.findViewById(R.id.textview_event_name);
l_oViewHolder.eventCheckBox = (CheckBox) l_oRowView.findViewById(R.id.checkbox_for_event_name);
l_oViewHolder.eventCheckBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CheckBox cb = (CheckBox)v;
//cb.setChecked(!cb.isChecked());
String l_sFilename = l_oViewHolder.eventName.getText().toString();
if (cb.isChecked())
{
if (!m_lstSelectedFilenames.contains(l_sFilename))
m_lstSelectedFilenames.add(l_sFilename);
}
else
{
if (m_lstSelectedFilenames.contains(l_sFilename))
m_lstSelectedFilenames.remove(l_sFilename);
}
}
});
l_oViewHolder.eventCheckBox.setFocusable(false);
//l_oViewHolder.eventCheckBox.setClickable(false);
// "Add" the viewHolder as a tag:
l_oRowView.setTag(l_oViewHolder);
l_oInitializedViewHolder = l_oViewHolder;
}
else
l_oInitializedViewHolder = (myCustomAdapterViewHolder) l_oRowView.getTag();
// By now, the rowView is initialized, just get the viewHolder and then get the views from it, to update:
//myCustomAdapterViewHolder l_oViewHolder = (myCustomAdapterViewHolder) l_oRowView.getTag();
/*l_oInitializedViewHolder.eventName.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCallBack.onEventItemSelected((String)getListAdapter().getItem(l_nPosition));
}
});*/
l_oInitializedViewHolder.eventName.setText(m_oValues.get(position));
return l_oRowView;
//return super.getView();
}
public myCustomAdapter(Context cont, int viewResId, String[] strings)
{
this(cont, viewResId, Arrays.asList(strings));
}
#Override
public String getItem(int position)
{
return sortedList.get(position);
}
#Override
public int getPosition(String item)
{
return sortedList.indexOf(item);
}
}
What am I doing wrong here?
All I want is to be able to select "files" for deletion, using the CheckBoxes
Try only using only onclick for textview and checkbox, not onListItemClick -which you can remove- as well, so you should change some properties for the linear layout as well.
<?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="wrap_content"
android:clickable="false"
android:focusable="false"
android:gravity="left"
android:orientation="horizontal" >
<TextView
android:id="#+id/textview_event_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="3dp"
android:paddingTop="5dp"
android:focusable="true"
android:singleLine="false" />
<CheckBox
android:id="#+id/checkbox_for_event_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.05"
android:focusable="false"
android:clickable="false" />
</LinearLayout>
Implement in the adapter
// PlaceHolder pattern:
final myCustomAdapterViewHolder l_oViewHolder = new myCustomAdapterViewHolder();
l_oViewHolder.eventName = (TextView) l_oRowView.findViewById(R.id.textview_event_name);
l_oViewHolder.eventCheckBox = (CheckBox) l_oRowView.findViewById(R.id.checkbox_for_event_name);
l_oViewHolder.eventName.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// your code for textview click
}
}
l_oViewHolder.eventCheckBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CheckBox cb = (CheckBox)v;
//cb.setChecked(!cb.isChecked());
String l_sFilename = l_oViewHolder.eventName.getText().toString();
if (cb.isChecked())
{
if (!m_lstSelectedFilenames.contains(l_sFilename))
m_lstSelectedFilenames.add(l_sFilename);
}
else
{
if (m_lstSelectedFilenames.contains(l_sFilename))
m_lstSelectedFilenames.remove(l_sFilename);
}
}
});
l_oViewHolder.eventCheckBox.setFocusable(false);
add following attribute:
android:focusable="false"
android:clickable="false"
for example in my xml file:
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeightSmall"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:button="?android:attr/listChoiceIndicatorSingle"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:buttonTint="#color/gray"
android:focusable="false"
android:clickable="false"
/>
and in my code:
public class MyTestFragment extends ListFragment {
.....
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
}
}
I create a custom listview with a TextView and 2 ImageButton. But onitemclicklistner is not calling. Here is my code
police_station_list = (ListView) findViewById(R.id.police_station_listView_xml);
adapter = new ArrayAdapter<String>(this, R.layout.coustome_listview, R.id.district_listview_xml1, police_station_name);
PSAdapter = new Police_Station_Adapter(this);
police_station_list.setAdapter(PSAdapter);
police_station_list.setOnItemClickListener(PSAdapter);
Here is my custom ArrayAdapter class:
public class Police_Station_Adapter extends ArrayAdapter<String> implements OnItemClickListener{
Context context;
public Police_Station_Adapter(Context context) {
//super(context, R.layout.police_station_listview, R.id.police_station_listView_textView, police_station_name);
super(context, R.layout.police_station_listview, police_station_name);
this.context = context;
}
private class ViewHolder {
TextView tv;
ImageButton mobile, phone;
public ViewHolder(View v) {
tv = (TextView) v.findViewById(R.id.police_station_listView_textView);
mobile = (ImageButton) v.findViewById(R.id.police_station_listView_imageButton_mobile);
phone = (ImageButton) v.findViewById(R.id.police_station_listView_imageButton_phone);
}
}
ViewHolder holder;
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if(v == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.police_station_listview, parent, false);
holder = new ViewHolder(v);
v.setTag(holder);
}
else {
holder = (ViewHolder) v.getTag();
}
holder.tv.setText(police_station_name[position]);
holder.mobile.setTag(position);
holder.mobile.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new Message(context, police_station_name[(Integer) v.getTag()]+" button", 500);
}
});
return v;
}
#Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
new Message(context, "Item Clicked", 500); //Toast message
}
}
Here is the custom ListView xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<TextView
android:id="#+id/police_station_listView_textView"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="8"
android:textSize="16sp"
android:layout_marginRight="10dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageButton
android:id="#+id/police_station_listView_imageButton_mobile"
android:layout_gravity="center_vertical"
android:layout_width="27dp"
android:layout_height="30dp"
android:layout_weight="1"
android:layout_marginRight="15dp"
android:background="#drawable/mobile_icon" />
<ImageButton
android:id="#+id/police_station_listView_imageButton_phone"
android:layout_gravity="center_vertical"
android:layout_width="27dp"
android:layout_height="30dp"
android:layout_weight="1"
android:layout_marginRight="10dp"
android:background="#drawable/telephone_icon" />
</LinearLayout>
it's seems like i can add onClikcListner for textview but it doesn't show any pressed animation. It may not be the appropriate solution. I need the proper way to setOnItemClickListner.
You need to set OnItemClickListener in your Activity. then change this
police_station_list.setOnItemClickListener(PSAdapter); to
police_station_list.setOnItemClickListener(this);
Remove implementation of OnItemClickListener from adapter.
Check this tutorial.
add this attribute to imageButton:
android:focusable="false"
Hello I have create an Android application, in that I add some buttons and images in ListView using CustomAdapter.
My Files Structure is:
layout
[1] album ->List View
[2] album_list -> Button
So how I create Button Object ?
My Code is:
album.xml
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="1" >
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_column="0"
android:layout_gravity="left|top"
android:layout_row="0" >
</ListView>
</GridLayout>
album_list.xml
<RelativeLayout
android:id="#+id/RelativeLayout001"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="2"
android:orientation="horizontal" >
<Button
android:id="#+id/btn_eng"
style="?android:attr/buttonStyleSmall"
android:layout_width="100px"
android:layout_height="31px"
android:layout_marginLeft="11dp"
android:layout_marginRight="17dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="35dp"
android:layout_alignTop="#+id/btn_hindi"
android:layout_alignBottom="#+id/ImageView12"
android:layout_alignLeft="#+id/ImageView12"
android:background="#drawable/normal"
android:text="ENG"
android:textColor="#android:color/white" />
<TextView
android:id="#+id/tv_albumname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tv_go"
android:layout_alignTop="#+id/iv_album_image"
android:layout_marginLeft="2dp"
android:layout_marginTop="2dp"
android:text="Album Name"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#android:color/white" />
<Button
android:id="#+id/btn_hindi"
style="?android:attr/buttonStyleSmall"
android:layout_width="100px"
android:layout_height="31px"
android:layout_marginTop="7dp"
android:layout_marginRight="17dp"
android:layout_alignTop="#+id/ImageView12"
android:layout_alignLeft="#+id/btn_eng"
android:background="#drawable/normal"
android:text="HINDI"
android:textColor="#android:color/white" />
</RelativeLayout>
Album.java
Album_List_Custom_Adapter adapter =
new Album_List_Custom_Adapter(getApplicationContext(), albumList);
lv.setAdapter(adapter);
Album_List_Custom_Adapter.java
private Context context;
ArrayList<HashMap<String, String>> listAlbum;
ViewHolder vholder;
private OnClickListener listener;
public Album_List_Custom_Adapter(Context context, ArrayList<HashMap<String, String>> albumList)
{
this.context = context;
this.listAlbum=albumList;
}
#Override
public int getCount()
{
return listAlbum.size();
}
#Override
public Object getItem(int position)
{
return null;
}
#Override
public long getItemId(int position)
{
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View vi=convertView;
if (convertView == null)
{
vi = inflater.inflate(R.layout.home_list_model, null);
vholder = new ViewHolder();
vholder.hindi=(Button)vi.findViewById(R.id.btn_hindi);
vholder.eng=(Button)vi.findViewById(R.id.btn_eng);
vholder.tv_album_name = (TextView) vi.findViewById(R.id.tv_albumname);
vi.setTag(vholder);
}
else
{
vholder = (ViewHolder) (vi.getTag());
}
vholder.hindi.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
vholder.hindi.setBackgroundResource(R.drawable.selected);
vholder.eng.setBackgroundResource(R.drawable.normal);
}
});
vholder.hindi.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
v.setBackgroundResource(R.drawable.selected);
vholder.eng.setBackgroundResource(R.drawable.normal);
}
});
vholder.eng.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
v.setBackgroundResource(R.drawable.selected);
vholder.hindi.setBackgroundResource(R.drawable.normal);
}
});
vholder.tv_album_name.setText(listAlbum.get(position).get("album"));
return vi;
}
static class ViewHolder
{
TextView tv_album_name;
Button eng, hindi;
}
You have to create button object in adapter where u r inflating list item layout.
like
Button b = (Button)yourViewobject.findViewById(R.id.button1);
Now implement onClick event for that
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
EDIT:
Use this. Make one custom file in drawable folder named mybutton.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/selected" android:state_selected="true"></item>
<item android:drawable="#drawable/normal"></item>
</selector>
Now just in your custom layout file set background for your both buttons.
android:background="#drawable/mybutton" // for Hindi Button
and same as for english button.
And also now remove onClick event method from your adapter.
Need to change..
<Button
android:id="#+id/btn_eng"
style="?android:attr/buttonStyleSmall"
android:layout_width="100px"
android:layout_height="31px"
android:layout_marginLeft="11dp"
android:layout_marginRight="17dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="35dp"
android:clickable = "true"
android:focusable = "true"
android:layout_alignTop="#+id/btn_hindi"
android:layout_alignBottom="#+id/ImageView12"
android:layout_alignLeft="#+id/ImageView12"
android:background="#drawable/mybutton"
android:text="ENG"
android:textColor="#android:color/white" />
Do this in your custom adapter
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item, null);
viewHolder=new ViewHolderforGroup();
viewHolder.btn = (Button)convertView.findViewById(R.id.btn);
convertView.setTag(viewHolder)
}else{
viewHolder = (ViewHolder)convertView.getTag(viewHolder);
}
viewHolder.btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
static class ViewHolder{
Button btn;
}
public class CustomAdapter extends BaseAdapter
{
private Context mContext;
Cursor cursor;
public CustomAdapter(Context context,Cursor cur)
{
super();
mContext=context;
cursor=cur;
}
public int getCount()
{
// return the number of records in cursor
return cursor.getCount();
}
// getView method is called for each item of ListView
public View getView(int position, View view, ViewGroup parent)
{
// inflate the layout for each item of listView
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.listview_each_item, null);
// fetch the sender number and sms body from cursor
String Number="123456";
// get the reference of textViews
TextView Number=(TextView)view.findViewById(R.id.textViewSMSSender);
Button btn=(Button)view.findViewById(R.id.button);
// Set the Sender number and smsBody to respective TextViews
Number.setText(senderNumber);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
return view;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
}