我想使用可扩展列表视图,其内容从数据库中填充。
我写代码但我没有得到正确的输出。它给出了所有父母的所有记录,但我想显示一个父母只显示相关的孩子和其他孩子根据他们各自的父母显示。 它给了我输出像:
日期1
DATE2
//下面是活动类
public class FavoriteActivity extends Activity {
/** Final static constant. */
private static final String TAG = "FAVORITE_ACTIVITY";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_favorite);
Cursor mCursor = null;
Cursor mDataCursor = null;
FavoriteNews mFavoriteNews = null;
List<String> mList;
List<FavoriteNews> mFavoriteList = new ArrayList<FavoriteNews>();
mCursor = DBHandler.getData("select DISTINCT " + DBHelper.NEWS_DATE + " from " + DBHelper.FAVOURITE_TABLE + "",
null);
if (mCursor.getCount() != 0) {
mList = new ArrayList<String>();
while (mCursor.moveToNext()) {
mList = new ArrayList<String>();
mDataCursor = DBHandler.getData("select " + DBHelper.NEWS_LABEL + " from " + DBHelper.FAVOURITE_TABLE
+ " where " + DBHelper.NEWS_DATE + "='" + mCursor.getString(0) + "'", null);
if (mDataCursor.getCount() != 0) {
while (mDataCursor.moveToNext()) {
mFavoriteNews = new FavoriteNews();
String news = mDataCursor.getString(0);
mList.add(news);
}
mDataCursor.close();
String date = mCursor.getString(0);
mFavoriteNews.setDate(date);
mFavoriteNews.setFavNews(mList);
}
mFavoriteList.add(mFavoriteNews);
}
try {
ExpandableListView mExList = (ExpandableListView) findViewById(R.id.expandable_list);
mExList.setAdapter(new CustomExpandableAdapter(this, mFavoriteList));
mCursor.close();
} catch (Exception e) {
Log.e(TAG, "Data not found", e);
} finally {
if (mDataCursor != null) {
mDataCursor.close();
}
if (mCursor != null) {
mCursor.close();
}
}
} else {
Toast.makeText(this, "No Favorite Items are Available", Toast.LENGTH_SHORT).show();
}
}
}
//这是我的另一个班级
public class FavoriteNews {
String date;
List<String> favNews;
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public List<String> getFavNews() {
return favNews;
}
public void setFavNews(List<String> favNews) {
this.favNews = favNews;
}
}
//这是适配器的代码
public class CustomExToDoAdapter extends BaseExpandableListAdapter{
LayoutInflater inflater;
ArrayList<ToDoTaskList> list;
public CustomExToDoAdapter(Context context,ArrayList<ToDoTaskList> list){
inflater=LayoutInflater.from(context);
this.list=list;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return list.get(groupPosition).getTaskList().get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent) {
if(convertView==null){
convertView=inflater.inflate(R.layout.child_row, parent,false);
}
TextView childTxt=(TextView) convertView.findViewById(R.id.childItem);
childTxt.setText(list.get(groupPosition).getTaskList().get(childPosition));
childTxt.setPadding(40, 5, 5, 5);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return list.get(groupPosition).getTaskList().size();
}
@Override
public Object getGroup(int groupPosition) {
return list.get(groupPosition).getDate();
}
@Override
public int getGroupCount() {
return list.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) {
if(convertView==null){
convertView=inflater.inflate(R.layout.header_row, parent,false);
}
TextView txtView=(TextView) convertView.findViewById(R.id.header);
txtView.setText(getGroup(groupPosition).toString());
txtView.setPadding(50, 10, 10, 10);
return convertView;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public void registerDataSetObserver(DataSetObserver observer)
{
/* used to make the notifyDataSetChanged() method work */
super.registerDataSetObserver(observer);
//Log.i("TAG","Inside Cons 10");
}
}
答案 0 :(得分:0)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_favorite);
dbHelper=new DBHelper(this, "smartdiary.db", null, 2);
SQLiteDatabase db=dbHelper.getWritableDatabase();
dbHandler=new DBHandler(this,db);
favoriteList=new ArrayList<FavoriteNews>();
Cursor cursor=dbHandler.getData("select DISTINCT date from favorite", null);
mExList=(ExpandableListView) findViewById(R.id.expandable_list);
if(cursor.getCount()!=0)
{
while(cursor.moveToNext())
{
//create a list each time data found so that it contains only relevant data with //corresponding group
list=new ArrayList<String>();
Cursor dataCursor=dbHandler.getData("select newsLabel from favorite where date='"+cursor.getString(0)+"'",null);
if(dataCursor.getCount()!=0)
{
System.out.println(" Value of Cursor "+cursor.getCount());
System.out.println("Value of dataCursor "+dataCursor.getCount());
while(dataCursor.moveToNext()){
favoriteNews=new FavoriteNews();
String news=dataCursor.getString(0);
list.add(news);
}
String date=cursor.getString(0);
favoriteNews.setDate(date);
favoriteNews.setFavNews(list);
}
favoriteList.add(favoriteNews);
}
try{
mExList.setAdapter(new CustomExpandableAdapter(this, favoriteList));
}
catch(Exception e)
{
e.printStackTrace();
}
}
else{
Log.i("FAV_CURSOR", "Data not Found");
Toast.makeText(this, "No Favorite Items are Availble", Toast.LENGTH_SHORT).show();
}
}