2

I need a ListView which items can be selected by checkbox near them and check boxes not showing in default mode but they must be shown in selection mode (when hoding touch on one of the items) and a MarkAll button in action bar exactly like default contacts app (see picture below).

enter image description here I've found somethings, I found contacts app surce code but it's too complicated I just need two functionalities of contacts app, in other example that I've found either show checkbox in listview always always I mean not only in selection mode or just setting choice mode of list view on CHOICE_MODE_MULTIPLE_MODAL and no check box at all.

I also tried myself I make my list view CHOICE_MODE_MULTIPLE_MODAL and write appropriate events and I also put a CheckedTextView in my ListView item xml file.

here is my ListView Item xml layout

<?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" >

<TextView
    android:id="@+id/tvTitle"
    android:layout_width="200dp"
    android:layout_height="30dp"
    android:layout_alignParentRight="true"
    android:text="Study cursors"
     android:ellipsize="end"
    android:singleLine="true"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/tvTags_Album"
    android:layout_width="200dp"
    android:layout_height="25dp"
    android:layout_alignParentRight="true"
    android:layout_below="@id/tvTitle"
    android:layout_marginBottom="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="10dp"
    android:text=""
    android:ellipsize="end"
    android:singleLine="true"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<ImageView
    android:id="@+id/imgPic_Grid"
    android:layout_width="120dp"
    android:layout_height="90dp" />

<TextView
    android:id="@+id/tvModifiedDate"
    android:layout_width="200dp"
    android:layout_height="25dp"
    android:layout_alignBottom="@+id/imgPic_Grid"
    android:layout_alignParentRight="true"
    android:text="تاریخ ایجاد : "
    android:textAppearance="?android:attr/textAppearanceMedium" />
<CheckedTextView 
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
 />
 </RelativeLayout>

here are my adaptor codes

public class MediaListAdaptor extends CursorAdapter {
   public ImageLoader imageLoader;
  public MediaListAdaptor(Context context, Cursor cursor) {
      super(context, cursor, 0);
  }

  // The newView method is used to inflate a new view and return it, 
  // you don't bind any data to the view at this point. 
  @Override
  public View newView(Context context, Cursor cursor, ViewGroup parent) {
      return LayoutInflater.from(context).inflate(R.layout.activity_medialist_item, parent, false);
  }
  @Override
  public void bindView(View view, Context context, Cursor cursor) {
      // Find fields to populate in inflated template
      if (cursor != null)
      {

          TextView tvTitle = (TextView) view.findViewById(R.id.tvTitle);
          TextView tvtags = (TextView) view.findViewById(R.id.tvTags_Album);
          TextView tvModifiedDate = (TextView) view.findViewById(R.id.tvModifiedDate);
          ImageView imgPic = (ImageView) view.findViewById(R.id.imgPic_Grid);

          // Extract properties from cursor
          String FileName = cursor.getString(cursor.getColumnIndexOrThrow("FileName"));
          String Title = cursor.getString(cursor.getColumnIndexOrThrow("Title"));
          String Tags = cursor.getString(cursor.getColumnIndexOrThrow("Tags")).replace("\n", "").replace("\r", "");
          String ModifiedDate = cursor.getString(cursor.getColumnIndexOrThrow("LastModifiedDate"));
        /*  final String path = Environment.getExternalStorageDirectory().getPath()
                +"/"+  Environment.DIRECTORY_DCIM+"/Camera/2.jpg";*/
          final String path = cursor.getString(cursor.getColumnIndex("filepath"));
          if(path != "")
          {
              File imgFile = new  File(path);

                if(imgFile.exists()){
                     imageLoader=new ImageLoader(context);
                     imageLoader.DisplayImage(path, imgPic);

                }
          }

          tvTitle.setText(String.valueOf(Title));
          tvtags.setText(String.valueOf(Tags));
          tvModifiedDate.setText("تاریخ ایجاد : "+ModifiedDate);
      }


  }
  }

here is part of my main activity code that is responsible to bind ListView and set it to multiple choice and related events

 Cursor cr = mRepository.GetMediaForAlbum(AlbumId);
      BindListViewAndGrid(cr);

         lvItems.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
         lvItems.setMultiChoiceModeListener(new MultiChoiceModeListener() {

            @Override
            public boolean onPrepareActionMode(ActionMode arg0, Menu arg1) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public void onDestroyActionMode(ActionMode arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public boolean onCreateActionMode(ActionMode arg0, Menu arg1) {
                MenuInflater inflater =arg0.getMenuInflater(); 
                inflater.inflate(R.menu.multiselect_remove, arg1);
                return true;
            }

            @Override
            public boolean onActionItemClicked(ActionMode arg0, MenuItem arg1) {

                return false;
            }

            @Override
            public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
                IDList.add(id);
                Toast.makeText(MainActivity.this,Long.toString(id)+"< >"+Integer.toString(position) , Toast.LENGTH_LONG).show();
                .0
            }
        });

as you see above I used a CheckedTextView inside my List View item but I have two major problems with it, first CheckedTextView is shown always, not only in selection mode (I want CheckedTextView appear only in selection mode) and second CheckedTextView can't be checked neither in default mode nor in selection mode.

do you know any sample app that has functionalities mentioned above or any solution that fixes my own codes???

Thanks in advance

Code_Worm
  • 4,069
  • 2
  • 30
  • 35

0 Answers0