0

halllo,

im trying to populate the gridview from arraylist. i found this link and i follow it.

it work but the data is overwrite. that means the position is not good. i am beginer in android, don't know how to fix it.

My code: gridview_layout.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" >

    <TextView
        android:id="@+id/textView_CodeDest"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView_CodeCl"
        android:layout_alignBottom="@+id/textView_CodeCl"
        android:layout_centerHorizontal="true"
        android:text="@string/txt_code_dest" />

    <TextView
        android:id="@+id/textView_DateTampon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="74dp"
        android:layout_toRightOf="@+id/textView_CodeDest"
        android:text="@string/txt_date_tampon" />

    <TextView
        android:id="@+id/textView_CodeCl"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView_DateTampon"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="38dp"
        android:text="@string/txt_code_client" />

</RelativeLayout>

and activity_liste_ordre_valide.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: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="com.example.soft8suivicolis.ListeOrdreValide" >

    <GridView
        android:id="@+id/gridView_Ordre"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="26dp"
        android:numColumns="3" >
    </GridView>

</RelativeLayout>

and listeordervalide.java

public class ListeOrdreValide extends ActionBarActivity {

    private GridView gridView_Ordre; 
    SQLController   dbcon;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_liste_ordre_valide);

        gridView_Ordre=(GridView) findViewById(R.id.gridView_Ordre); 

         ArrayList<Pers_Ordre> oOder = new ArrayList<Pers_Ordre>();
          dbcon = new SQLController(this);

         dbcon.open();
         oOder = dbcon.ReadData();
         GridViewListAdapter TheAdapt = new GridViewListAdapter(this, R.layout.gridview_layout, oOder);

         gridView_Ordre.setAdapter(TheAdapt);

         dbcon.close();
    }

and of course my adapter class:

public class GridViewListAdapter extends ArrayAdapter<Pers_Ordre>{

    ArrayList<Pers_Ordre> m_list; // your person arraylist
    Context m_context; // the activity context
    int m_resource; // this will be your xml file

    public GridViewListAdapter(Context context, int resource, ArrayList<Pers_Ordre> objects) {
        super(context, resource,  objects);
        // TODO Auto-generated constructor stub
        m_list = objects;
        m_context = context;
        m_resource = resource;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        if(m_list.size() == 0){
            return 0;
        }else{
            return m_list.size();
        }
    }

    @Override
    public Pers_Ordre getItem(int position) {
        // TODO Auto-generated method stub
        return m_list.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View child = convertView;
        RecordHolder holder;
        LayoutInflater inflater = ((ListeOrdreValide) m_context).getLayoutInflater(); // inflating your xml layout

        if (child == null) {            
            child = inflater.inflate(m_resource, parent, false);
            holder = new RecordHolder();
            holder.CodeCl = (TextView) child.findViewById(R.id.textView_CodeCl); // fname is the reference to a textview
            holder.CodeDest = (TextView) child.findViewById(R.id.textView_CodeDest); // in your xml layout file 
            holder.DateTampon =(TextView) child.findViewById(R.id.textView_DateTampon); // you are inflating.etc
            child.setTag(holder);
        }else{
            holder = (RecordHolder) child.getTag();
        }

        Pers_Ordre user = m_list.get(position); // you can remove the final modifieer.

        holder.CodeCl.setText(user.getLe_CodeClient());      
        holder.CodeDest.setText(user.getLe_CodeDest());
        holder.DateTampon.setText(user.getLe_Date());

        // the string as url and set it to your imageview..
        return child;
    }

    static class RecordHolder {
        TextView CodeCl,CodeDest,DateTampon;

    }
5
  • Please explain it work but the data is overwrite Commented Apr 9, 2015 at 12:58
  • as u can see i have 3 column, normaly i should have 5 row, but i only got 1 row...all data is overwrite in 1 only row Commented Apr 9, 2015 at 13:00
  • What is size of m_list ? Commented Apr 9, 2015 at 13:02
  • the size of m_list is dynamic Commented Apr 9, 2015 at 13:10
  • @Override public int getCount() { return m_list.size(); } Commented Apr 9, 2015 at 13:25

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.