Модель классаКак показать SQLite данные в ListView с помощью фрагментировать
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(){
}
}
Это мой dababase вспомогательный класс:
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;
}
}
Это мой фрагмент класс:
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;
}
Это моя ада PTER класс
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. Я не знаю, где я делаю ошибку.
1) Вы должны не называть 'getAllClientList()' для каждого потенциального зрения в адаптер. 2) Как мы знаем, что есть данные в базе данных? –
Почему вы не вызываете 'getAllClientList' в свой класс фрагментов? –