dimanche 23 juillet 2017

Android Listview Multiple Columns with a checkbox

For now my code use R. layout. simple_list_item_multiple_choice and it's working perfectly fine. Since I need a second TextView, I've tried to create my own xml having 2 TextView and 1 CheckBox.

I need 2 action on my listview: OnClickListener will open a new activity and OnLongClickLisener will check/uncheck.

Here is the problem, if I add a CheckBox, I lose the ability to click on my ListView and I can only click on the CheckBox.

Here's a simple version of my code: (for now I haven't changed the Checkmark code since I wasn't able to make it work with my new 3 column version)

Main2Acitvity :

if (POSITION == 1){

        setContentView(R.layout.activity_main2);
        Log.d(TAG, "onCreate: Started.");
        mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mActionBarToolbar);
        getSupportActionBar().setTitle("Stigmate");

        ImageView = (ImageView)findViewById(R.id.imageView2);
        ImageView.setImageResource(R.drawable.r_stigmate);
        listView = (ListView)findViewById(R.id.listView);

        Person john = new Person("Première cicatrice","V.0");
        Person john1 = new Person("Première cicatrice","V.0");
        Person john2= new Person("Première cicatrice","V.0");
        Person john3 = new Person("Première cicatrice","V.0");

        ArrayList<Person> peopleList = new ArrayList<>();
        peopleList.add(john);
        peopleList.add(john1);
        peopleList.add(john2);
        peopleList.add(john3);

        PersonListAdapter adapter = new PersonListAdapter(this, R.layout.adapter_view_layout, peopleList);


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            }
        });
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int i, long id) {
                if (Var[i] == 0){
                    listView.setItemChecked(i,false);
                }
                else if (Var[i] == 1){
                    listView.setItemChecked(i,true);
                }
                Intent intent = new Intent(Main2Activity.this, Main3Activity.class);
                intent.putExtra("CountryName",listView.getItemAtPosition(i).toString());
                startActivity(intent);

                int n = 0;
                for (n= 0; n < 20 ; n++) {
                    if(i==n){
                        Main3Activity.POSITION3 = n;
                    }}
            }

        });
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override

            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                                           int pos, long id) {
                if (Var[pos] == 1){
                    listView.setItemChecked(pos,false);
                    Var[pos] = 0;
                }
                else if (Var[pos] == 0){
                    listView.setItemChecked(pos,true);
                    Var[pos] = 1;
                }
                else {
                    listView.setItemChecked(pos,true);
                Var[pos] = 0;
                }
                Var0 = Var[0];
                Var1 = Var[1];
                Var2 = Var[2];
                Var3 = Var[3];
                Var4 = Var[4];

                SharedPreferences saveinfo = getSharedPreferences(SAVE, 0);
                SharedPreferences.Editor editor = saveinfo.edit();
                editor.putInt("SavedStigmate1",Var0).putInt("SavedStigmate2",Var1).putInt("SavedStigmate3",Var2).putInt("SavedStigmate4",Var3).putInt("SavedStigmate5",Var4);
                editor.apply();
                return true;
            }
        });




        // here you should set checked items

        SharedPreferences loadinfo = getSharedPreferences(SAVE,0);
        Var0 = loadinfo.getInt("SavedStigmate1",0);
        Var1 = loadinfo.getInt("SavedStigmate2",0);
        Var2 = loadinfo.getInt("SavedStigmate3",0);
        Var3 = loadinfo.getInt("SavedStigmate4",0);
        Var4 = loadinfo.getInt("SavedStigmate5",0);
        Var[0] = Var0;
        Var[1] = Var1;
        Var[2] = Var2;
        Var[3] = Var3;
        Var[4] = Var4;

        int iii;
        for  (iii=0; iii<10; iii++){
            if (Var[iii]== 1)
                listView.setItemChecked(iii,true);
            else if (Var[iii]== 0)
                listView.setItemChecked(iii,false);
            else
                listView.setItemChecked(iii,false);

        }

    }

Person code :

    package com.carte.alex.test3;


/**
 * Created by User on 3/14/2017.
 */

public class Person {
    private String name;
    private String birthday;


    public Person(String name, String birthday) {
        this.birthday = birthday;
        this.name = name;

    }

    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

PersonListAdapter :

 package com.carte.alex.test3;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by User on 3/14/2017.
 */

public class PersonListAdapter extends ArrayAdapter<Person> {

    private static final String TAG = "PersonListAdapter";

    private Context mContext;
    private int mResource;
    private int lastPosition = -1;

    /**
     * Holds variables in a View
     */
    private static class ViewHolder {
        TextView name;
        TextView birthday;

    }

    /**
     * Default constructor for the PersonListAdapter
     * @param context
     * @param resource
     * @param objects
     */
    public PersonListAdapter(Context context, int resource, ArrayList<Person> objects) {
        super(context, resource, objects);
        mContext = context;
        mResource = resource;
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //get the persons information
        String name = getItem(position).getName();
        String birthday = getItem(position).getBirthday();


        //Create the person object with the information
        Person person = new Person(name,birthday);

        //create the view result for showing the animation
        final View result;

        //ViewHolder object
        ViewHolder holder;


        if(convertView == null){
            LayoutInflater inflater = LayoutInflater.from(mContext);
            convertView = inflater.inflate(mResource, parent, false);
            holder= new ViewHolder();
            holder.name = (TextView) convertView.findViewById(R.id.textView1);
            holder.birthday = (TextView) convertView.findViewById(R.id.textView2);


            result = convertView;

            convertView.setTag(holder);
        }
        else{
            holder = (ViewHolder) convertView.getTag();
            result = convertView;
        }




        holder.name.setText(person.getName());
        holder.birthday.setText(person.getBirthday());


        return convertView;



    }
}

Adapter_view_layout.xml

   <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="100">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="90"
        android:orientation="vertical">

        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:gravity="center_vertical|left"
            android:paddingBottom="8dp"
            android:paddingLeft="15dp"
            android:paddingTop="8dp"
            android:text="TextView1"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1"
            android:textSize="16sp" />


    </LinearLayout>


    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="12.36">

        <TextView

            android:id="@+id/textView2"
            android:layout_width="60dp"
            android:layout_height="50dp"
            android:gravity="center_vertical|left"
            android:paddingBottom="8dp"
            android:paddingLeft="12dp"
            android:paddingTop="8dp"
            android:text="TextView2" />


    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <CheckBox
            android:id="@+id/checkBox1"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:gravity="center"
            android:paddingLeft="20dp"
            android:paddingTop="8dp"
            android:paddingBottom="8dp"
            android:theme="@style/WhiteCheck"/>


    </LinearLayout>

</LinearLayout>

activity_main2.xml

    <?xml version="1.0" encoding="utf-8"?>



<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
    xmlns:app="http://ift.tt/GEGVYd"
    xmlns:tools="http://ift.tt/LrGmb4"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.carte.alex.test3.MainActivity">

    <android.support.v7.widget.Toolbar
        xmlns:android="http://ift.tt/nIICcg"
        xmlns:app="http://ift.tt/GEGVYd"
        android:id="@+id/toolbar"

        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:fitsSystemWindows="true"
        android:minHeight="?attr/actionBarSize"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        android:background="?attr/colorPrimary">

    </android.support.v7.widget.Toolbar>

    <android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

    </android.support.v4.view.ViewPager>

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="232dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/toolbar"
        android:scaleType="fitXY"
        />

    <ListView
        android:id="@+id/listView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/imageView2"
        android:longClickable="true"
        android:checkMark="@color/red"
    />


</RelativeLayout>

Here is the result on an emulator




Aucun commentaire:

Enregistrer un commentaire