خواندن اطلاعات از دیتابیس و نمایش در ExpandableListView

ساخت وبلاگ
سلام به همه :-)
دوستان من دو تا جدول دارم (روز هفته - فعالیت روزانه)
حالا با توجه به هر کار (که یک یا چند روز رو برای خودش تعریف کرده) باید توی لیست دیتیل (زیر فهرست روز هفته) فعالیت های همون روز نشون داده بشه ولی تمام فعالیت ها نشون داده میشه !

این کد Adapter :

package ir.rahgoshafan.mybody.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import java.util.List;

import ir.rahgoshafan.mybody.R;
import ir.rahgoshafan.mybody.model.Week;
import ir.rahgoshafan.mybody.model.Workout;

public class Expandable extends BaseExpandableListAdapter {
private Context context;
private List week;
private List workout;

public Expandable(Context context, List week, List workout) {
this.context = context;
this.week = week;
this.workout = workout;
}

@Override
public int getGroupCount() {
return week.size();
}

@Override
public int getChildrenCount(int groupPosition) {
return workout.size();
}

@Override
public Object getGroup(int groupPosition) {
return week.get(groupPosition);
}

@Override
public Object getChild(int groupPosition, int childPosition) {
return workout.get(childPosition);
}

@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}

@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}

@Override
public boolean hasStableIds() {
return true;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
Week weekName = (Week) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLA TER_SERVICE);
convertView = layoutInflater.inflate(R.layout.expandable_list_gr oup, null);
}
TextView weekDay = (TextView) convertView.findViewById(R.id.listTitle);
weekDay.setText(weekName.getDayName());
return convertView;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
Workout workName = (Workout) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLA TER_SERVICE);
convertView = layoutInflater.inflate(R.layout.expandable_list_it em, null);
}
TextView workoutName = (TextView) convertView.findViewById(R.id.expanded_ListItem_ex ercise);
workoutName.setText(workName.getWorkout());
TextView workoutNo = (TextView) convertView.findViewById(R.id.expanded_ListItem_no );
workoutNo.setText(String.valueOf(workName.getWorko utSet()));
return convertView;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}

این هم کدهای مربوط به اکتیویتی :

ExpandableListView expandableListView;
Expandable expandableListAdapter;
List expandableListTitle;
List expandableListDetail;expandableListDetail = DB_HELPER.WORKOUT_LIST(1);
expandableListTitle = DB_HELPER.WEEK_LIST(USER_ID);
expandableListAdapter = new Expandable(this, expandableListTitle, expandableListDetail);
expandableListView.setAdapter(expandableListAdapte r);

و این هم کوئری :


public List WORKOUT_LIST(int id) { try {
Workout WORKOUT_TABLE = null;
List WORKOUT = new ArrayList<>();
openDatabase();
db.beginTransaction();
Cursor WORKOUT_CURSOR = db.rawQuery("select * from tblWorkout where userDay =" + id, null);
db.setTransactionSuccessful();
WORKOUT_CURSOR.moveToFirst();
while (!WORKOUT_CURSOR.isAfterLast()) {
WORKOUT_TABLE = new Workout(WORKOUT_CURSOR.getInt(0), WORKOUT_CURSOR.getInt(1), WORKOUT_CURSOR.getString(2), WORKOUT_CURSOR.getInt(3),
WORKOUT_CURSOR.getInt(4), WORKOUT_CURSOR.getString(5));
WORKOUT.add(WORKOUT_TABLE);
WORKOUT_CURSOR.moveToNext();
}
WORKOUT_CURSOR.close();
return WORKOUT;
} catch (Exception e) {
Log.i(LOG_TAG, "VIEW_WORKOUT_LIST Error :" + e.toString());
return null;
} finally {
db.endTransaction();
closeDatabase();
}
}

توی عکس ضمیمه مشخصه ... یک فعالیت تعریف شده که فقط باید در روز شنبه نمایش داده بشه ولی توی دوشنبه هم هست !

برنامه نویس...
ما را در سایت برنامه نویس دنبال می کنید

برچسب : نویسنده : محمد رضا جوادیان programers بازدید : 198 تاريخ : جمعه 29 ارديبهشت 1396 ساعت: 17:48