我有一个以String格式存储日期的数据库。我正在访问数据库并在列表视图中显示它。现在,我想要的是我想比较相邻的日期来计算它们之间的天数,并将它们显示为同一屏幕中的列表。 这是我的代码:
MainActivity:
public class LogActivity extends Activity {
SqlHandler sqlHandler;
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_log);
lv = (ListView) findViewById(R.id.lv_custom_list);
sqlHandler = new SqlHandler(this);
showList();
}
private void showList() {
ArrayList<PredictedDateItems> predicteddateList = new ArrayList<PredictedDateItems>();
predicteddateList.clear();
String query = "SELECT * FROM predicted_date_record_table ORDER BY _id DESC ";
Cursor c1 = sqlHandler.selectQuery(query);
//if(c1==null)
// {
// TextviewMsz = (TextView)findViewById(R.id.logview);
// }
if (c1 != null) {
if (c1.moveToFirst()) {
do {
PredictedDateItems predictedDateItems = new PredictedDateItems();
predictedDateItems.setSlno(c1.getString(c1
.getColumnIndex("_id")));
predictedDateItems.setDate(c1.getString(c1
.getColumnIndex("predicted_date")));
predicteddateList.add(predictedDateItems);
} while (c1.moveToNext());
}
}
c1.close();
PredictedDateAdapter predicteddateListAdapter = new PredictedDateAdapter(
LogActivity.this, predicteddateList);
lv.setAdapter(predicteddateListAdapter);
} }
适配器类: 公共类PredictedDateAdapter扩展了BaseAdapter {
Context context;
ArrayList<PredictedDateItems> predicteddateList;
public PredictedDateAdapter(Context context, ArrayList<PredictedDateItems> list) {
this.context = context;
predicteddateList = list;
}
@Override
public int getCount() {
return predicteddateList.size();
}
@Override
public Object getItem(int position) {
return predicteddateList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup arg2) {
PredictedDateItems predictedDateItems = predicteddateList.get(position);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.date_list_row, null);
}
// TextView tvSlNo = (TextView) convertView.findViewById(R.id.tv_slno);
//tvSlNo.setText(contactListItems.getSlno());
TextView tvDate = (TextView) `.findViewById(R.id.tv_predicted_date);
tvDate.setText(predictedDateItems.getDate());
return convertView;
}
}