如何使用片段在列表视图中显示sqlite数据

How to show sqlite data in listview using fragment

型号Class

public class ClientModel {

public int id;
public String case_no;
public String case_title;
public String client_name;
public String client_email;
public String client_address;
public String client_opt_lawyer;
public String client_court;
public String client_fee;
public String client_date;

public ClientModel(int id, String case_no, String case_title,String client_email,
                   String client_name,String client_address,String client_opt_lawyer,String client_court,
                   String client_date) {
    // TODO Auto-generated constructor stub
    this.id = id;
    this.case_no = case_no;
    this.case_title = case_title;
    this.client_email = client_email;
    this.case_title = case_title;
    this.client_name = client_name;
    this.client_address = client_address;
    this.client_opt_lawyer = client_opt_lawyer;
    this.client_court = client_court;
    this.client_date = client_date;
}


public ClientModel(){

}

}

这是我的数据库助手 class:

public class DatabaseHelper extends SQLiteOpenHelper {

public static String DATABASE_NAME = "vakil";

// Current version of database
private static final int DATABASE_VERSION = 1;

// Name of table
private static final String TABLE_CASES = "cases";

// All Keys used in table
private static final String KEY_ID = "id";
private static final String KEY_CASENO = "case_no";
private static final String KEY_TITLE = "case_title";
private static final String KEY_NAME = "client_name";
private static final String KEY_EMAIl = "client_email";
private static final String KEY_ADDRESS = "client_address";
private static final String KEY_OPTLAWYER = "client_opt_lawyer";
private static final String KEY_COURT = "client_court";
private static final String KEY_FEE = "client_fee";
private static final String KEY_DATE = "client_date";


public static String TAG = "tag";



private static final String CREATE_TABLE_CASES = "CREATE TABLE "
        + TABLE_CASES + "("
        + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
        + KEY_NAME + " TEXT,"
        + KEY_CASENO + " TEXT,"
        + KEY_TITLE + " TEXT,"
        + KEY_EMAIl + " TEXT,"
        + KEY_ADDRESS + " TEXT,"
        + KEY_OPTLAWYER + " TEXT,"
        + KEY_FEE + " TEXT,"
        + KEY_DATE + " TEXT,"
        + KEY_COURT + " TEXT);";

public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}



@Override
public void onCreate(SQLiteDatabase db) {

    db.execSQL(CREATE_TABLE_CASES); // create students table

}


@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_CASES ); // drop table if exists

    onCreate(db);
}



public long addClientDetail(ClientModel client) {

    SQLiteDatabase db = this.getWritableDatabase();

    // Creating content values
    ContentValues values = new ContentValues();
    values.put(KEY_CASENO, client.case_no);
    values.put(KEY_TITLE, client.case_title);
    values.put(KEY_NAME, client.client_name);
    values.put(KEY_EMAIl, client.client_email);
    values.put(KEY_ADDRESS, client.client_address);
    values.put(KEY_OPTLAWYER, client.client_opt_lawyer);
    values.put(KEY_COURT, client.client_court);
    values.put(KEY_FEE, client.client_fee);
    values.put(KEY_DATE, client.client_date);


    long insert = db.insert(TABLE_CASES, null, values);

    return insert;
}

public List<ClientModel> getAllClientList() {
    List<ClientModel> clientArrayList = new ArrayList<ClientModel>();

    clientArrayList.clear();
    String selectQuery = "SELECT  * FROM " + TABLE_CASES ;
    Log.d(TAG, selectQuery);

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor c = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (c.moveToFirst()) {
        do {

            ClientModel client = new ClientModel();
            client.id = c.getInt(c.getColumnIndex(KEY_ID));
            client.case_no = c.getString(c.getColumnIndex(KEY_CASENO));
            client.case_title = c.getString(c.getColumnIndex(KEY_TITLE));
            client.client_name = c.getString(c.getColumnIndex(KEY_NAME));
            client.client_email = c.getString(c.getColumnIndex(KEY_EMAIl));
            client.client_address = c.getString(c.getColumnIndex(KEY_ADDRESS));
            client.client_opt_lawyer = c.getString(c.getColumnIndex(KEY_OPTLAWYER));
            client.client_court = c.getString(c.getColumnIndex(KEY_COURT));
            client.client_fee = c.getString(c.getColumnIndex(KEY_COURT));
            client.client_date = c.getString(c.getColumnIndex(KEY_DATE));
            // adding to Students list
            clientArrayList.add(client);
        } while (c.moveToNext());
    }

    return clientArrayList;
}
}

这是我的片段class:

   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view= inflater.inflate(layout.caselist, container, false);
    lv = (ListView) view.findViewById(id.list_client);
    lv.setAdapter(adapter);
    return view;
}

这是我的适配器class

public ClientListAdapter(Context context, List<ClientModel> clientlist) {
    this.context = context;
    this.clientlist = clientlist;
    this.db = new DatabaseHelper(context.getApplicationContext());
    clientlist = db.getAllClientList();
}

 @Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view=convertView;
    if(convertView==null){

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.list_item,parent,false);

    }



    txt_client_case_no = (TextView)view.findViewById(R.id.txt_client_case_no);
    txt_client_name1 = (TextView)view.findViewById(R.id.txt_client_name1);
    txt_client_title = (TextView)view.findViewById(R.id.txt_client_title);
    txt_client_email = (TextView)view.findViewById(R.id.txt_client_email);
    txt_client_address = (TextView)view.findViewById(R.id.txt_client_address);

    txt_client_case_no.setText((CharSequence) clientlist.get(position).case_no);
    txt_client_name1.setText(clientlist.get(position).client_name);
    txt_client_email.setText(clientlist.get(position).client_email);

    return view;
}

当我 运行 上面的代码我无法获得任何视图,即使它没有在 logerror 中显示任何错误。我不知道我哪里弄错了。

我认为你应该重新构建源代码。
clientlist = db.getAllClientList(); 移动到适配器构造函数中。

你确定没有错误吗?

String selectQuery = "SELECT  * FROM " + CREATE_TABLE_CASES;

这是不正确的。您不能从 CREATE 语句中 select。你select来自table。

与 DROP 语句类似

db.execSQL("DROP TABLE IF EXISTS " + CREATE_TABLE_CASES);

此外,最好将 clientlist = db.getAllClientList(); 移动到构造函数中,但这只是出于性能原因。

在片段中初始化适配器 class

ClientListAdapter adapter;
ArrayList<ClientModel> search = new ArrayList<ClientModel>();

adapter = new ClientListAdapter(getActivity(), search, lv);
search = db.getAllClientList(); // call your method here
lv.setAdapter(adapter);

尝试将您的适配器class更新为此

 private static ArrayList<ClientModel> search;
 private LayoutInflater mInflater;
 ListView listview;

 public ClientListAdapter(Context context, ArrayList<ClientModel> searchList, ListView listview) {
        search=searchList;
        this.listview=listview;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder =null;
        if(convertView==null){
         convertView = mInflater.inflate(R.layout.list_item,parent,null);
         holder = new Holder();
         txt_client_case_no = (TextView)convertView.findViewById(R.id.txt_client_case_no);
         txt_client_name1 = (TextView)convertView.findViewById(R.id.txt_client_name1);
         txt_client_title = (TextView)convertView.findViewById(R.id.txt_client_title);
         txt_client_email = (TextView)convertView.findViewById(R.id.txt_client_email);
         txt_client_address = (TextView)convertView.findViewById(R.id.txt_client_address);
     convertView.setTag(holder);
     }
    else {
          holder = (ViewHolder) convertView.getTag();
          }
        holder.txt_client_case_no.setText((CharSequence) clientlist.get(position).case_no);
        holder.txt_client_name1.setText(clientlist.get(position).client_name);
        holder.txt_client_email.setText(clientlist.get(position).client_email);
        }
       return convertView;
    }

     static class ViewHolder {
            TextView txt_client_case_no;
            // also other textView        
        }
    }