amir barksdale amir barksdale - 6 months ago 19
Java Question

put Cursor content in listview

hi i want to put a cursor content in a list view is there any specific way to do it ?
i researched online nothing specific

i get this error when excuting

enter image description here

public class AfficherToousLesMembre extends AppCompatActivity {
String nom,prenom,email;
int numero;
ListView listView;


@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_afficher_toous_les_membre);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
BaseDeDonee bdd = new BaseDeDonee(this);//database
Cursor c =bdd.GETallMem();//function returns all the data from table person

ArrayAdapter<String> mp = new ArrayAdapter<String>(this,R.layout.display_member_row);

c.moveToFirst();
listView=(ListView)findViewById(R.id.listView);



while(c.moveToNext()){
nom= c.getString(c.getColumnIndex("nom"));
prenom=c.getString(c.getColumnIndex("prenom"));
email=c.getString(c.getColumnIndex("profile"));
numero=c.getInt(c.getColumnIndex("numero_tel"));
String f=Integer.toBinaryString(numero);

mp.add(nom);
mp.add(prenom);
mp.add(email);
mp.add(f);



}
listView.setAdapter(mp);




}

}


and the layout is

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#76ba7e"
android:layout_height="75dp">
<TextView
android:layout_width="70dp"
android:layout_height="match_parent"
android:id="@+id/nom_id"
android:layout_alignParentLeft="true"
android:text="nom"
android:gravity="center"
android:textAppearance="?android:textAppearanceLarge"
/>
<TextView
android:layout_width="80dp"
android:layout_height="match_parent"
android:id="@+id/prenom_id"
android:layout_toRightOf="@+id/nom_id"
android:text="Prenom"
android:gravity="center"
android:textAppearance="?android:textAppearanceLarge"
/>
<TextView
android:layout_width="70dp"
android:layout_height="match_parent"
android:id="@+id/email_id"
android:layout_toRightOf="@+id/prenom_id"
android:text="Email"
android:gravity="center"
android:textAppearance="?android:textAppearanceLarge"
/>
<TextView
android:layout_width="200dp"
android:layout_height="match_parent"
android:id="@+id/numero_tele_id"
android:layout_toRightOf="@+id/email_id"
android:text="Numero telephone"
android:gravity="center"
android:textAppearance="?android:textAppearanceLarge"
/>




please help i spend the hole day trayin to figure it out

Answer

Here your adapter need to change like this

ArrayAdapter(Context context, int resource, T[] objects). 

this is the basic constructor for ArrayAdapter you can use this below way.

(1) If you use Default layout for single value in ListItem than make it like

ArrayAdapter<String> itemsAdapter = 
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);

where items is a array of object which you want to display in ListView

(2) If you want to display single item in your custom layout for ListItem than make it like

 ArrayAdapter<String> itemsAdapter = new ArrayAdapter<String>(this,
    R.layout.rowlayout, R.id.label, values);

where R.id.label is a resource in which you want to display values

(3) Make your own Adapter to display bulk data in your ListItem

for this refer http://www.vogella.com/tutorials/AndroidListView/article.html

public class CustomListAdapter extends BaseAdapter{   
 String [] result;
 Context context;
 int [] imageId;
 private static LayoutInflater inflater=null;

  public CustomListAdapter (Context mContext, String[] NameList, int[] Images) {
    // TODO Auto-generated constructor stub
    result=NameList;
    context=mContext;
    imageId=Images;
    inflater = ( LayoutInflater )context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return result.length;
}

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

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

public class Holder
{
    TextView tv;
    ImageView img;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    Holder holder=new Holder();
    View rowView;       
         rowView = inflater.inflate(R.layout.row_list_item, null);
         holder.tv=(TextView) rowView.findViewById(R.id.textView1);
         holder.img=(ImageView) rowView.findViewById(R.id.imageView1);       
         holder.tv.setText(result[position]);
         holder.img.setImageResource(imageId[position]);         
         rowView.setOnClickListener(new OnClickListener() {            
          @Override
          public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(context, "You Clicked "+result[position], Toast.LENGTH_LONG).show();
        }
    });   
    return rowView;
}

} 

and used like this

ListView yourListView = (ListView) findViewById(R.id.itemListView);
CustomListAdapter customAdapter = new CustomListAdapter(this,String Array of NameList, int Array of Images);
yourListView.setAdapter(customAdapter);
Comments