这段代码向我展示了一些来自数据库的意外数据,但并不是一一准确地显示出来,有时会显示10次相同的数据,那么该问题的解决方案是什么?
listItem = new ArrayList<>();
customAdapter customAdapter = new customAdapter();
userlist = v.findViewById(R.id.lisview);
userlist.setDivider(null);
userlist.setAdapter(customAdapter);
我的自定义适配器
class customAdapter extends BaseAdapter {
@Override
public int getCount() {
int cursor = databaseHelper.getAllServiceAll().getCount();
return cursor;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = getLayoutInflater().inflate(R.layout.customlayout, null);
TextView showprice = view.findViewById(R.id.showprice);
TextView showliters = view.findViewById(R.id.showLiters);
TextView showdata = view.findViewById(R.id.showdata);
TextView showvalute = view.findViewById(R.id.showvalute);
Cursor cursor = databaseHelper.getAllServiceAll();
cursor.moveToFirst();
cursor.moveToNext();
String formula4S = new DecimalFormat("#0.00").format(Float.parseFloat(cursor.getString(2)));
showprice.setText(formula4S); // Price
showdata.setText(cursor.getString(3) ); // data
showvalute.setText("" + databaseHelper.getValute ()); // Valute
showliters.setText(cursor.getString(1) ); // Liters
return view;
}
}
DatabaseHelper.java:
public Cursor getAllServiceAll (){
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery("SELECT * FROM servis ORDER BY id DESC LIMIT 10", null);
return res;
}
答案 0 :(得分:0)
通常,您会在“活动”中获取项的ArrayList并将其传递给适配器。这是活动中代码的一个示例:
private DataBaseHelper myDbHelper;
private ListView mLocationList;
private TextView mEmptyList;
static ArrayList<LocationAddress> locationList = new ArrayList<LocationAddress>();
private LocationsListAdapter adapter;
....
thisContext = this;
myDbHelper = new DataBaseHelper(thisContext);
mLocationList = (ListView) findViewById(R.id.list_browse);
mLocationList.setOnItemClickListener(new LocationClickListener());
// at this point locationList is empty
adapter = new LocationsListAdapter(thisContext, locationList);
mLocationList.setAdapter(adapter);
// now, get the locationList by calling a background Async Task
new GetLocations().execute("");
private class GetLocations extends AsyncTask<String, Void, String> {
ProgressDialog dialog;
@Override
protected void onPreExecute() {
dialog = new ProgressDialog(thisContext);
dialog.setMessage("Loading, please wait...");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
}
protected String doInBackground(String... urls) {
String results = "";
locationList = new ArrayList<LocationAddress>();
try {
myDbHelper.openDataBase();
}catch(SQLException sqle){
results = "Error Opening Database";
throw sqle;
}
Cursor c = null;
try{
c = myDbHelper.getLocations();
if (c != null) {
if (c.moveToFirst())
{
do {
LocationAddress location = new LocationAddress();
location.setID(c.getInt(0));
location.setName(c.getString(1));
location.setAddress(c.getString(2));
location.setLatitude(c.getDouble(3));
location.setLongitude(c.getDouble(4));
location.setType(c.getString(5));
location.setSortOrder(c.getInt(6));
locationList.add(location);
} while (c.moveToNext());
}
}
if (c != null) {
if (!c.isClosed()) {
c.close();
}
}
} catch (Throwable t){
if (c != null) {
if (!c.isClosed()) {
c.close();
}
}
}
try {
myDbHelper.close();
}catch(SQLException sqle){
results = "SQL Error";
throw sqle;
}
return results;
}
protected void onPostExecute(String results) {
dialog.dismiss();
if (results != null) {
if (results.length() > 0) {
alertMessage("Get Locations", results);
} else {
// This refreshes the adapter with a full list of items
adapter = new LocationsListAdapter(thisContext, locationList);
mLocationList.setAdapter(adapter);
}
}
}
} // private Class
Adapter类如下:
public class LocationsListAdapter extends BaseAdapter {
private Context context;
private ArrayList<LocationAddress> locationItems;
public LocationsListAdapter(Context context, ArrayList<LocationAddress> locationItems){
this.context = context;
this.locationItems = locationItems;
}
@Override
public int getCount() {
return locationItems.size();
}
@Override
public Object getItem(int position) {
return locationItems.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@SuppressLint("InflateParams")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.location_list_item, null);
}
LinearLayout topLayout = (LinearLayout) convertView.findViewById(R.id.location_top_layout);
TextView txtSectionTitle = (TextView) convertView.findViewById(R.id.location_section_title);
TextView txtTitle = (TextView) convertView.findViewById(R.id.location_title);
TextView txtAddress = (TextView) convertView.findViewById(R.id.location_address);
String previous_section_title = "";
String section_title = "";
if (position > 0) {
previous_section_title = locationItems.get(position - 1).getType();
}
section_title = locationItems.get(position).getType();
String location_title = locationItems.get(position).getName();
String location_address = locationItems.get(position).getAddress();
txtSectionTitle.setText(section_title);
if (section_title.contentEquals(previous_section_title)) {
topLayout.setVisibility(View.GONE);
} else {
topLayout.setVisibility(View.VISIBLE);
}
txtTitle.setText(location_title);
txtAddress.setText(location_address);
return convertView;
}
}