我在使用从{sqlite数据库中填充的listView
的日期范围时遇到问题。该代码完美无缺,但我遇到的问题是listView
没有问题'当我选择另一个日期范围时(当我再次触摸“搜索”按钮时)更新(刷新),尽管数据适配器确实从数据库获取数据(通过使用{{1确保) }})但不会在logcat
中打印出来。我无法使用函数listView
,它无法正常工作,每当我在notifyDataSetChanged()
应用程序崩溃后使用它时。
以下是代码: -
add()
这是布局代码: -
public class ProductionCommentsActivity extends Activity implements View.OnClickListener {
private DBHandler dbHandler;
private ListView listView;
private Context context;
private ArrayList<String> results = new ArrayList<String>();
private ArrayAdapter adapter;
private static String newline = System.getProperty("line.separator");//a variable for line break
private EditText editTextFrom, editTextTo;
private DatePickerDialog datePickerDialogFrom, datePickerDialogTo;
private SimpleDateFormat simpleDateFormat;
private String fromDate,toDate ; // variables to store the chosen dates
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_production_comments);
dbHandler = new DBHandler(this, dbHandler.DATABASE_NAME_PRODUCTION, null, 1);
try {
dbHandler.copyDataBase();
Log.d("copydb", dbHandler.getDatabaseName());
} catch (IOException e) {
e.printStackTrace();
Log.d("copydb",e.getMessage());
}
//defining list view
listView = (ListView) findViewById(R.id.listView);
//defining edit texts properties
editTextFrom = (EditText) findViewById(R.id.editTextFrom);
editTextFrom.setInputType(InputType.TYPE_NULL);
editTextFrom.requestFocus();
editTextTo = (EditText) findViewById(R.id.editTextTo);
editTextTo.setInputType(InputType.TYPE_NULL);
//setting up the date format
simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
setDateTimeField();
context = this;
}
//method to handle the date pickers properties
private void setDateTimeField() {
editTextFrom.setOnClickListener((View.OnClickListener) ProductionCommentsActivity.this);
editTextTo.setOnClickListener((View.OnClickListener) ProductionCommentsActivity.this);
//creating a new instance of the calendar
Calendar newCalendar = Calendar.getInstance();
//creating a pop up date picker
datePickerDialogFrom = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
//getting the chosen date and setting its format
//and writing the chosen date in the edit text
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar newDate = Calendar.getInstance();
newDate.set(year, monthOfYear, dayOfMonth);
editTextFrom.setText(simpleDateFormat.format(newDate.getTime()));
fromDate = editTextFrom.getText().toString();
}
},newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH),
newCalendar.get(Calendar.DAY_OF_MONTH));
datePickerDialogTo = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar newDate = Calendar.getInstance();
newDate.set(year, monthOfYear, dayOfMonth);
editTextTo.setText(simpleDateFormat.format(newDate.getTime()));
toDate = editTextTo.getText().toString();
}
},newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH),
newCalendar.get(Calendar.DAY_OF_MONTH));
}
// on click method to handle which edit text was touched
// and show the appropriate pop up calendar
@Override
public void onClick(View view) {
if(view == editTextFrom) {
datePickerDialogFrom.show();
} else if(view == editTextTo) {
datePickerDialogTo.show();
}
}
//method to set format of date and attach the array adapter to the list view
public void searchDates(View view){
if(!editTextFrom.getText().toString().matches("From:")
&& !editTextTo.getText().toString().matches("To:")) {
setDateTimeField();
getProductionComments(fromDate, toDate);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results);
listView.setAdapter(adapter);
Log.d("fire",adapter.toString());
}else{
Toast.makeText(this,"Please enter valid dates", Toast.LENGTH_LONG).show();
}
}
//get production comments data
public void getProductionComments(String from, String to) {
try {
SQLiteDatabase db = dbHandler.getReadableDatabase();
String query = "SELECT Date,Item,Comments FROM ProductionCommentData WHERE Date " +
"BETWEEN ? AND ? ORDER BY Date DESC";
Cursor cursor = db.rawQuery(query,new String[]{from,to});
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
String date = cursor.getString(cursor.getColumnIndex("Date"));
String item = cursor.getString(cursor.getColumnIndex("Item"));
String comments = cursor.getString(cursor.getColumnIndex("Comments"));
results.add("Date: " + date.substring(0, 10) + newline + newline +
"Item: " + item + newline + newline + comments);
} while (cursor.moveToNext());
}else{
Toast.makeText(this,"No Data was found for the chosen dates",Toast.LENGTH_LONG).show();
}
}
} catch (SQLiteException se){
Log.e(getClass().getSimpleName(), se.getMessage());
}
}
提前致谢
答案 0 :(得分:0)
adapter.notifyDataSetChanged();
适用于这种情况。
[如果没有尝试重新加载列表]
答案 1 :(得分:0)
每次搜索时都不要重新创建适配器。
类字段:
mAdapter // instantiate once
mListView // instantiate once
mResults // refresh with new data when available
这应该在onCreate()
mListView = (ListView)findViewByid(//etc
mResults = new ArrayList<>();
//Init mAdapter here only
mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mResults);
mListView.setAdapter(mAdapter);
然后,在searchDates(...)
中,执行
getProductionComments(fromDate, toDate);//Set mResults with new data
//Don't set adapter here
mAdapter.notifyDataSetChanged(); //refresh adapter