halllo,
我试图从arraylist填充gridview。我找到了这个link,我跟着它。
它工作但数据被覆盖。这意味着这个位置并不好。 我是Android的初学者,不知道如何解决它。
我的代码: 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>
和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>
和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();
}
当然还有我的适配器类:
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;
}