dimanche 10 mai 2015

ListFragment with Adapter

I have Listfragment with Adapter: main_fragment.xml:

`<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout  xmlns:android="http://ift.tt/nIICcg"
    xmlns:tools="http://ift.tt/LrGmb4"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffffff">

    <ListView
        android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:listSelector="@color/Grey" />

    <ImageView
        android:src="@drawable/add"
        android:layout_width="52dp"
        android:layout_height="52dp"
        android:id="@+id/buttonAddMode"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="24dp"
        android:layout_marginBottom="24dp" />

</RelativeLayout>`


list_item.xml:

    `<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout  xmlns:android="http://ift.tt/nIICcg"
        xmlns:tools="http://ift.tt/LrGmb4"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="72dp"
        android:background="#ffffffff"
        android:id="@+id/LLLIST"
        android:baselineAligned="false">
        <ImageView
            android:id="@+id/icon"
            android:layout_width="72dp"
            android:layout_height="72dp"
            android:layout_gravity="center_vertical"
            android:paddingLeft="16dp"
            android:paddingRight="16dp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_weight="1"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:paddingTop="16dp"
            android:paddingBottom="16dp">

            <TextView
                android:id="@+id/label"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#000000"
                android:fontFamily="sans-serif-thin"
                android:typeface="monospace"
                android:textSize="16dp" />

            <TextView
                android:id="@+id/date"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="14dp"
                android:textStyle="normal"
                android:textColor="#ff9b9b9b" />
        </LinearLayout>
        <ImageView android:id="@+id/imRight"
            android:layout_width="72dp"
            android:layout_height="72dp"
            android:layout_weight="0"
            android:layout_gravity="center_vertical"
            android:src="@drawable/ic_chevron_right"
            android:paddingLeft="24dp"
            android:paddingRight="24dp" />
    </LinearLayout>`

Mainfragment.xml :

  public class FragmentMain extends ListFragment implements View.OnClickListener {



    ModeVariantBase dbHelper;


    ArrayList<String> modeNameList;
    Boolean toggleVar;
    MySimpleArrayAdapter adapter;

    ArrayList<Mode> modes=new ArrayList<Mode>();

    ImageView addMode;

    ListView list;



        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);

        }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub      
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);

        list = (ListView) rootView.findViewById(R.id.list);

        dbHelper = new ModeVariantBase(getActivity());
        modeNameList = new ArrayList<String>(); // empty in start



        reedBase();

        rootView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT ));       
        return rootView;        
    }




     @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        String selection = adapter.getStringList(position);


        switch (v.getId()){
            case R.id.imRight:
              //some function
               break;
            default:
                //someEventListener.someEvent(WhatsUpInMain.Edit,selection);
                break;
        }

    }


    public void reedBase() {
        modeNameList.clear();

        SQLiteDatabase db = dbHelper.getWritableDatabase();
        Cursor c = db.query("data", null,null,null,null,null,null,null);
        boolean[] days= {false,false,false,false,false,false,false};


        if (c.moveToFirst()) {
            int nameColIndex = c.getColumnIndex("MODENAME");

            do {

               modeNameList.add(c.getString(nameColIndex));

            } while (c.moveToNext());
        } 

        adapter = new MySimpleArrayAdapter(getActivity(), modeNameList);
        setListAdapter(adapter);
        c.close();
    }}

My adapter:

 public class MySimpleArrayAdapter extends ArrayAdapter<String>  {
    private final Context context;
    private final ArrayList <String> values;




    public MySimpleArrayAdapter(Context context, ArrayList<String> values) {
        super(context, R.layout.fragment_main,  values);
        this.context = context;
        this.values = values;
        dbHelper = new ModeVariantBase(context);
    }


    public long getItemId(int position) {


        return position;
    }


    public String getStringList(int position) {
        return values.get(position);
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.list_item, parent, false);


        TextView textView = (TextView) rowView.findViewById(R.id.label);
        TextView textView2 = (TextView) rowView.findViewById(R.id.date);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
        ImageView imRight = (ImageView) rowView.findViewById(R.id.imRight);

        textView.setText(values.get(position));
        textView2.setText("Some text");

        imageView.setImageResource(R.drawable.ic_work);}
        imRight.setImageResource(R.drawable.ic_favorites);


        ///////////////////////////////
        return rowView;
    }


}

I want: After the user clicked on imRight to sent notification, and if the click on other elements of list to do some function. But after clicked, in onListItemClick, View v - LLLIST (LinearLayout in list_item).How do I do this?

Aucun commentaire:

Enregistrer un commentaire