问题描述
- android怎么在listview中显示联系人 我的代码错在哪里??
-
package com.example.maysisteen;import java.util.ArrayList;
import java.util.List;import android.os.Bundle;
import android.R.anim;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;public class MainActivity extends Activity {
private ListView listView; private Button Submit; private List<String> list; //1.调用系统的“打开或创建数据库”方法来获得一个SQLiteDataBase对象 SQLiteDatabase db= openOrCreateDatabase("stu.db", MODE_PRIVATE, null); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //2.执行命令去创建表 db.execSQL("create table if not exists stutb (id integer primary key autoincrement," + "name text not null,sex text not null)"); //3.填充数据 ContentValues values=new ContentValues(); values.put("name", "张三"); values.put("sex", "男"); db.insert("stutb", null, values); values.clear(); //清空values后,可继续使用values对象 //5.将stutb表中, // 满足条件 id>?,其中条件值?为2, // id大于2的所有sex列更新为女。 values.put("sex", "女"); db.update("stutb", values, "id>?", new String[]{"2"}); values.clear(); listView=(ListView) findViewById(R.id.listView1); Submit=(Button) findViewById(R.id.submit); Submit.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub dd(); } }); } public void dd() { ArrayAdapter<String> adapter=new ArrayAdapter<String>(this ,android.R.layout.simple_list_item_1, ddv()); listView.setAdapter(adapter); }
public final List ddv()
{Cursor cursor= db.query("stutb", null, "id>?", new String[]{"0"}, null, null, "name"); if (cursor!=null) { while (cursor.moveToNext()) { int id=cursor.getInt(cursor.getColumnIndex("id")); String name=cursor.getString(cursor.getColumnIndex("name")); String sex=cursor.getString(cursor.getColumnIndex("sex")); String value=id+""+name+""+sex+""; list.add(value); } } return list;
}
}
解决方案
Android 添加新的联系人代码
android 联系人 中英文排序 --代码仓库
时间: 2024-12-23 04:16:23