将Cursor内容放在listview中

时间:2016-05-27 12:25:53

标签: java android android-studio

嗨我想把一个游标内容放在列表视图中有没有具体的方法呢? 我在网上没有具体研究

在排除

时出现此错误

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);




}

}

,布局是

<?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"
    />

请帮助我花一天时间托盘来搞清楚

2 个答案:

答案 0 :(得分:1)

此处您的适配器需要像这样更改

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

这是ArrayAdapter的基本构造函数,您可以在下面使用它。

(1)如果您对ListItem中的单个值使用默认布局而不是

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

其中items是您要在ListView

中显示的对象数组

2)如果您希望在ListItem的自定义布局中显示单个项目而不是像那样

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

其中R.id.label是您要在其中显示值的资源

(3)制作您自己的适配器,以便在ListItem

中显示批量数据

此参考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;
}

} 

并像这样使用

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

答案 1 :(得分:1)

  

Mainactivity.xml

<LinearLayout 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:orientation="vertical"
   tools:context=".ListActivity" >

   <ListView
      android:id="@+id/mobile_list"
      android:layout_width="match_parent"
      android:layout_height="wrap_content" >
   </ListView>

</LinearLayout>
  

listview_detail.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/label"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:padding="10dip"
   android:textSize="16dip"
   android:textStyle="bold" >
</TextView>
  

MainActivity.Java

//getting all user data from database
UserMst sql_userMst = new UserMst(context);
sql_usermst.open();
Cursor cursor = sql_userMst.getAllUserData();

//binding cursor to listview
ListView listView = (ListView) findViewById(R.id.mobile_list);
listView.setAdapter(new TodoCursorAdapter(context,cursor));

public class TodoCursorAdapter extends CursorAdapter {
  public TodoCursorAdapter(Context context, Cursor cursor) {
      super(context, cursor);
  }

  // The newView method is used to inflate a new view and return it, 
  // you don't bind any data to the view at this point. 
  @Override
  public View newView(Context context, Cursor cursor, ViewGroup parent) {
      return LayoutInflater.from(context).inflate(R.layout.listview_detail, parent, false);
  }

  // The bindView method is used to bind all data to a given view
  // such as setting the text on a TextView. 
  @Override
  public void bindView(View view, Context context, Cursor cursor) {
      // Find fields to populate in inflated template
      TextView label = (TextView) view.findViewById(R.id.label);

      // Extract properties from cursor
      String username= cursor.getString(cursor.getColumnIndexOrThrow("UserName"));
      // Populate fields with extracted properties
      label .setText(username);
  }
}

使用此网址作为参考listview with cursor