One ListView Receiving Data from Multiple Class - android

I am looking for something like this:
several <ItemTemplate> in one ListView.
But it was in .ASP and above my level.
What I need
Class Vitals: vTime, BP, Heart Rate, Respirations per Minute, etc.
Class Medications: mTime, RxName, RxRoute, RxDose, RxDoseUnit, etc.
Class Procedures: pTime, Intubation, IV insertion, Defibrillation, etc.
Classes Vitals, Medications and Procedures to be based on user input that inject in to a ListView (sorted chronologically). A "Many-to-One" if I may.
I've went through hours of "CustomAdapter & ListView" tutorials, code samples, walkthroughs.
Here is my current code (trashed and scattered) to show that I am actively working towards a solution:
/*
* Created by SwaLayS on 2/19/2015.
*/
public class VitalAdapter extends BaseAdapter {
private ArrayList<VitalItem> vitalData;
private LayoutInflater layoutInflater;
public VitalAdapter(Context acontext, ArrayList<VitalItem> vitalData){
this.vitalData=vitalData;
layoutInflater=LayoutInflater.from(acontext);
}
#Override
public int getCount() {
return vitalData.size();
}
#Override
public Object getItem(int position) {
return vitalData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder holder;
if (convertView == null){
convertView = layoutInflater.inflate(R.layout.vital_view_children,null);
holder = new ViewHolder();
}
}
public class VitalView extends RelativeLayout {
private TextView vTimeTV;
// private TextView vPTATV;
private TextView vRateTV;
private TextView vOxySatTV;
private TextView vSysBPTV;
private TextView vDiaBPTV;
private TextView vRespRateTV;
private TextView vRespEffortTV;
//private TextView vMethodBPTV;
public static VitalView inflate(ViewGroup parent){
VitalView vitalView = (VitalView) LayoutInflater.from(parent.getContext())
.inflate(R.layout.vital_view,parent,false);
return vitalView;
}
public VitalView(Context c){
this(c,null);
}
public VitalView(Context context, AttributeSet attrs){
this(context, attrs,0);
}
public VitalView(Context context, AttributeSet attrs, int defStyle){
super(context,attrs,defStyle);
LayoutInflater.from(context).inflate(R.layout.vital_view_children, this,true);
setupChildren();
}
private void setupChildren(){
vTimeTV = (TextView)findViewById(R.id.vTime);
// vPTATV = (TextView)findViewById(R.id.vPTA);
vRateTV = (TextView) findViewById(R.id.vBPM);
vOxySatTV = (TextView) findViewById(R.id.vOsat);
vSysBPTV = (TextView) findViewById(R.id.vSystolic);
vDiaBPTV = (TextView)findViewById(R.id.vDiastolic);
vRespRateTV = (TextView) findViewById(R.id.vRespRate);
vRespEffortTV = (TextView)findViewById(R.id.vRespEffort);
// vMethodBPTV = (TextView)findViewById(R.id.vMethodBP
}
public void setVital(VitalItem vital){
//vTimeTV.setText(vital.get);
}
}
public class VitalItem {
private String vTime;
// private String vPTA;
private String vRate;
private String vOxySat;
private String vSysBP;
private String vDiaBP;
private String vRespRate;
private String vRespEffort;
// private String vMethodBP;
public VitalItem(String Time, String Rate, String OxySat, String SysBP, String DiaBp, RespRate, String RespEffort){
super();
vTime=Time;
// vPTA=PTA;
vRate=Rate;
vOxySat = OxySat;
vSysBP = SysBP;
vDiaBP = DiaBP;
vRespRate = RespRate;
vRespEffort=RespEffort;
//vMethodBP=MethodBP;
}
public String getvTime() {
return vTime;
}
public void setvTime(String vTime) {
this.vTime = vTime;
}
// public String getvPTA() {
// return vPTA;
// }
// public void setvPTA(String vPTA) {
// this.vPTA = vPTA;
// }
public String getvRate() {
return vRate;
}
public void setvRate(String vRate) {
this.vRate = vRate;
}
public String getvOxySat() {
return vOxySat;
}
public void setvOxySat(String vOxySat) {
this.vOxySat = vOxySat;
}
public String getvSysBP() {
return vSysBP;
}
public void setvSysBP(String vSysBP) {
this.vSysBP = vSysBP;
}
public String getvDiaBP() {
return vDiaBP;
}
public void setvDiaBP(String vDiaBP) {
this.vDiaBP = vDiaBP;
}
public String getvRespRate() {
return vRespRate;
}
public void setvRespRate(String vRespRate) {
this.vRespRate = vRespRate;
}
public String getvRespEffort() {
return vRespEffort;
}
public void setvRespEffort(String vRespEffort) {
this.vRespEffort = vRespEffort;
}
// public String getvMethodBP() {
// return vMethodBP;
//}
// public void setvMethodBP(String vMethodBP) {
// this.vMethodBP = vMethodBP;
//
}
}
I'd appreciate any and everything;
I'm working on a NEMSIS . org project;
I may even be searching with the wrong search terms for what I need.
All help is appreciated

have you try the getViewTypeCount() method in adapter,
it can define different itemView for your different data types.
for your case you need to define three layout items .
search some demos,it may help you .

Related

Android: ListView Contains All Values in One Line

I have problem with displaying values using ListView.
I created adapter extends Base Adapter and Setter & Getter for listview, however it stores
all values in one line.
This is displayed screen
I would like to display like this
This is my adapter class
public class DataShow_List_Adapter extends BaseAdapter {
private LayoutInflater layoutInflater;
private ArrayList<TimeTable_ListItems> timeTable_listItems;
public DataShow_List_Adapter(Context context){
this.layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void SetTimeTable_listItems_adapter(ArrayList<TimeTable_ListItems> timeTable_listItems){
this.timeTable_listItems = timeTable_listItems;
}
#Override
public int getCount() {
return timeTable_listItems.size();
}
#Override
public Object getItem(int position) {
return timeTable_listItems.get(position);
}
#Override
public long getItemId(int position) {
return timeTable_listItems.get(position).getId();
}
#SuppressLint("ViewHolder")
#Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = layoutInflater.inflate(R.layout.timetable_datashow_listview_row,parent,false);
((TextView)convertView.findViewById(R.id.Title_List)).setText(timeTable_listItems.get(position).getTitle());
((TextView)convertView.findViewById(R.id.SubTitle_List)).setText(timeTable_listItems.get(position).getSubTitle());
((TextView)convertView.findViewById(R.id.Start_Time_List)).setText(timeTable_listItems.get(position).getStart_Time());
((TextView)convertView.findViewById(R.id.End_Time_List)).setText(timeTable_listItems.get(position).getEnd_time());
return convertView;
}
}
This is Setter & Getter
public class TimeTable_ListItems {
private long id;
private String title;
private String subTitle;
private String start_Time;
private String end_time;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getSubTitle() {
return subTitle;
}
public void setSubTitle(String subTitle) {
this.subTitle = subTitle;
}
public String getStart_Time() {
return start_Time;
}
public void setStart_Time(String start_Time) {
this.start_Time = start_Time;
}
public String getEnd_time() {
return end_time;
}
public void setEnd_time(String end_time) {
this.end_time = end_time;
}
}
This is method that returns String from Database
public String getTitle_database_mon(){
sqLiteDatabase = this.getReadableDatabase();
String[] columns = new String[]{COLUMN_TITLE, COLUMN_MON,};
#SuppressLint("Recycle")
Cursor cursor =
sqLiteDatabase.query(TABLE_TIMETABLE,columns,COLUMN_MON + "=" + 1 ,null,null,null,null);
int iTitle = cursor.getColumnIndex(COLUMN_TITLE);
StringBuilder result = new StringBuilder();
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){
result.append(cursor.getString(iTitle)).
append("\n\n");
}
return result.toString();
}
This is My fragment
public class Monday_DataShow_Fragment extends Fragment {
private View root;
private ListView listView_mon;
private ArrayList<TimeTable_ListItems> timeTable_listItems_array;
private TimeTable_ListItems timeTable_listItems;
private DatabaseTimetable databaseTimetable;
private DataShow_List_Adapter dataShow_list_adapter;
public Monday_DataShow_Fragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
root = inflater.inflate(R.layout.fragment_monday__data_show_, container, false);
findViews();
databaseTimetable = new DatabaseTimetable(getActivity());
timeTable_listItems = new TimeTable_ListItems();
dataShow_list_adapter = new DataShow_List_Adapter(requireActivity());
setListItems();
timeTable_listItems_array.add(timeTable_listItems);
dataShow_list_adapter.SetTimeTable_listItems_adapter(timeTable_listItems_array);
listView_mon.setAdapter(dataShow_list_adapter);
// Inflate the layout for this fragment
return root;
}
private void setListItems(){
timeTable_listItems_array = new ArrayList<>();
timeTable_listItems.setTitle(databaseTimetable.getTitle_database_mon());
dataShow_list_adapter.notifyDataSetChanged();
}
private void findViews(){
listView_mon = root.findViewById(R.id.listview_monday);
}
}
If you have any suggestions I'd like to hear it.
The issue is you have only one title as title String. getTitle_database_mon returns result.toString(). If you want to have multiple rows you need to have multiple objects in your list and unique titles for each. So instead of one TimeTable_ListItems. You need to be using ArrayList<TimeTable_ListItems> instead and have each entry contain a title so it is going to create multiple rows in the list instead of one.

Showing more details in RecyclerView + FastAdapter, when the item has been clicked

In an app using FastAdapter and ConstraintLayout I display a list of completed games (pardon the non-english characters in the screenshot):
When the app user clicks an item in the list (anywhere in the item), I would like to display an image view with a square game board and also change the plus image view to minus:
For that I have already prepared a method:
private void toggleDetails() {
if (mBoard.getVisibility() == View.GONE) {
mBoard.setVisibility(View.VISIBLE);
mDetails.setImageResource(R.drawable.minus_circle_gray);
} else {
mBoard.setVisibility(View.GONE);
mDetails.setImageResource(R.drawable.plus_circle_gray);
}
}
However my problem is that I am not sure, where to call the method.
Should I add a click listener to the fast adapter object or should I add it to my view holder?
Also, when looking at ExpandableSampleActivity example, I notice that there is some animation and wonder how to animate my disclosure of the mBoard image view...
Below is my current source code:
public class FinishedGameItem extends AbstractItem<FinishedGameItem, FinishedGameItem.ViewHolder> {
private final static String WON = "won";
private final static String LOST = "lost";
public long stamp;
public int gid;
public int score1;
public int score2;
public int elo1;
public int elo2;
public String state1;
public String finished;
public String given1;
public String given2;
public String photo1;
public String photo2;
#Override
public int getType() {
return R.id.finished_game_item_id;
}
#Override
public int getLayoutRes() {
return R.layout.item_finished_game;
}
#NonNull
#Override
public ViewHolder getViewHolder(#NonNull View v) {
return new ViewHolder(v);
}
protected static class ViewHolder extends FastAdapter.ViewHolder<FinishedGameItem> {
private ImageView mDetails;
private TextView mGid;
private TextView mFinished;
private TextView mScore1;
private TextView mScore2;
private TextView mGiven1;
private TextView mGiven2;
private TextView mElo1;
private TextView mElo2;
private ImageView mPhoto1;
private ImageView mPhoto2;
private ImageView mBoard;
public ViewHolder(View view) {
super(view);
mDetails = view.findViewById(R.id.details);
mGid = view.findViewById(R.id.gid);
mFinished = view.findViewById(R.id.finished);
mScore1 = view.findViewById(R.id.score1);
mScore2 = view.findViewById(R.id.score2);
mGiven1 = view.findViewById(R.id.given1);
mGiven2 = view.findViewById(R.id.given2);
mElo1 = view.findViewById(R.id.elo1);
mElo2 = view.findViewById(R.id.elo2);
mPhoto1 = view.findViewById(R.id.photo1);
mPhoto2 = view.findViewById(R.id.photo2);
mBoard = view.findViewById(R.id.board);
}
#Override
public void bindView(#NonNull FinishedGameItem item, #NonNull List<Object> payloads) {
Context ctx = mDetails.getContext();
Resources res = mDetails.getResources();
String result = (WON.equals(item.state1) ? "Victory" : (LOST.equals(item.state1) ? "Loss" : "Draw"));
mDetails.setImageResource(R.drawable.plus_circle_gray);
mBoard.setVisibility(View.GONE);
mGid.setText(r.getString(R.string.str_game, item.gid));
mFinished.setText(result + " / " + item.finished);
mScore1.setText(res.getString(R.string.str_score, item.score1));
mScore2.setText(res.getString(R.string.str_score, item.score2));
mGiven1.setText(item.given1);
mGiven2.setText(item.given2);
mElo1.setText(String.valueOf(item.elo1));
mElo2.setText(String.valueOf(item.elo2));
if (URLUtil.isHttpsUrl(item.photo1)) {
Picasso.with(ctx))
.load(item.photo1)
.placeholder(R.drawable.account_gray)
.into(mPhoto1);
}
if (URLUtil.isHttpsUrl(item.photo2)) {
Picasso.with(ctx)
.load(item.photo2)
.placeholder(R.drawable.account_gray)
.into(mPhoto2);
}
Picasso.with(ctx)
.load("http://slova.de/words/board2.php?gid=" + item.gid)
.placeholder(R.drawable.checkerboard_gray)
.into(mBoard);
}
#Override
public void unbindView(#NonNull FinishedGameItem item) {
Context ctx = mDetails.getContext();
Picasso.with(ctx).cancelRequest(mPhoto1);
Picasso.with(ctx).cancelRequest(mPhoto2);
Picasso.with(ctx).cancelRequest(mBoard);
mDetails.setImageDrawable(null);
mGid.setText(null);
mFinished.setText(null);
mScore1.setText(null);
mScore2.setText(null);
mGiven1.setText(null);
mGiven2.setText(null);
mElo1.setText(null);
mElo2.setText(null);
mPhoto1.setImageDrawable(null);
mPhoto2.setImageDrawable(null);
mBoard.setImageDrawable(null);
}
// WHERE TO CALL THIS METHOD AND HOW TO ANIMATE?
private void toggleDetails() {
if (mBoard.getVisibility() == View.GONE) {
mBoard.setVisibility(View.VISIBLE);
mDetails.setImageResource(R.drawable.minus_circle_gray);
} else {
mBoard.setVisibility(View.GONE);
mDetails.setImageResource(R.drawable.plus_circle_gray);
}
}
}
}
I have also asked my question in the Github issue 713
UPDATE:
I have tried adding a boolean details to my model and use it in the bindView method (true means display the mBoard), but the app behaves erratically now, wrong details are shown and that not immediately after clicking -
Here the new Fragment source code:
mFastAdapter.withSelectable(true);
mFastAdapter.withOnClickListener(new OnClickListener<FinishedGameItem>() {
#Override
public boolean onClick(View v, #NonNull IAdapter<FinishedGameItem> adapter, #NonNull FinishedGameItem item, int position) {
item.details = !item.details;
// THIS METHOD IS CALLED IN DEBUGGER, BUT VIEWS ARE NOT UPDATED
return true;
}
});
And here the changed FinishedGameItem model:
public class FinishedGameItem extends AbstractItem<FinishedGameItem, FinishedGameItem.ViewHolder> {
private final static String WON = "won";
private final static String LOST = "lost";
public boolean details; // TRIED TO ADD THIS BOOLEAN
public long stamp;
public int gid;
public int score1;
public int score2;
public int elo1;
public int elo2;
public String state1;
public String finished;
public String given1;
public String given2;
public String photo1;
public String photo2;
#Override
public int getType() {
return R.id.finished_game_item_id;
}
#Override
public int getLayoutRes() {
return R.layout.item_finished_game;
}
#NonNull
#Override
public ViewHolder getViewHolder(#NonNull View v) {
return new ViewHolder(v);
}
protected static class ViewHolder extends FastAdapter.ViewHolder<FinishedGameItem> {
private ImageView mDetails;
private TextView mGid;
private TextView mFinished;
private TextView mScore1;
private TextView mScore2;
private TextView mGiven1;
private TextView mGiven2;
private TextView mElo1;
private TextView mElo2;
private ImageView mPhoto1;
private ImageView mPhoto2;
private ImageView mBoard;
public ViewHolder(View view) {
super(view);
mDetails = view.findViewById(R.id.details);
mGid = view.findViewById(R.id.gid);
mFinished = view.findViewById(R.id.finished);
mScore1 = view.findViewById(R.id.score1);
mScore2 = view.findViewById(R.id.score2);
mGiven1 = view.findViewById(R.id.given1);
mGiven2 = view.findViewById(R.id.given2);
mElo1 = view.findViewById(R.id.elo1);
mElo2 = view.findViewById(R.id.elo2);
mPhoto1 = view.findViewById(R.id.photo1);
mPhoto2 = view.findViewById(R.id.photo2);
mBoard = view.findViewById(R.id.board);
}
#Override
public void bindView(#NonNull FinishedGameItem item, #NonNull List<Object> payloads) {
Context ctx = mDetails.getContext();
Resources res = mDetails.getResources();
String result = (WON.equals(item.state1) ? "Victory" : (LOST.equals(item.state1) ? "Loss" : "Draw"));
mDetails.setImageResource(item.details ? R.drawable.minus_circle_gray : R.drawable.plus_circle_gray);
mBoard.setVisibility(item.details ? View.VISIBLE : View.GONE);
mGid.setText(res.getString(R.string.str_game, item.gid));
mFinished.setText(result + " / " + item.finished);
mScore1.setText(res.getString(R.string.str_score, item.score1));
mScore2.setText(res.getString(R.string.str_score, item.score2));
mGiven1.setText(item.given1);
mGiven2.setText(item.given2);
mElo1.setText(String.valueOf(item.elo1));
mElo2.setText(String.valueOf(item.elo2));
if (URLUtil.isHttpsUrl(item.photo1)) {
Picasso.with(ctx)
.load(item.photo1)
.placeholder(R.drawable.account_gray)
.into(mPhoto1);
}
if (URLUtil.isHttpsUrl(item.photo2)) {
Picasso.with(ctx)
.load(item.photo2)
.placeholder(R.drawable.account_gray)
.into(mPhoto2);
}
if (item.details) {
Picasso.with(ctx)
.load("http://slova.de/words/board2.php?gid=" + item.gid)
.placeholder(R.drawable.checkerboard_gray)
.into(mBoard);
}
}
#Override
public void unbindView(#NonNull FinishedGameItem item) {
Context ctx = mDetails.getContext();
Picasso.with(ctx).cancelRequest(mPhoto1);
Picasso.with(ctx).cancelRequest(mPhoto2);
Picasso.with(ctx).cancelRequest(mBoard);
mDetails.setImageDrawable(null);
mGid.setText(null);
mFinished.setText(null);
mScore1.setText(null);
mScore2.setText(null);
mGiven1.setText(null);
mGiven2.setText(null);
mElo1.setText(null);
mElo2.setText(null);
mPhoto1.setImageDrawable(null);
mPhoto2.setImageDrawable(null);
mBoard.setImageDrawable(null);
}
}
}
The app kind of works, but erratically:
Judging by your code, it seems that you may have forgot to actually tell the adapter that your backing model (FinishedGameItem) has changed. Calling notifyItemChanged(position) might help.
mFastAdapter.withSelectable(true);
mFastAdapter.withOnClickListener(new OnClickListener<FinishedGameItem>() {
#Override
public boolean onClick(View v, #NonNull IAdapter<FinishedGameItem> adapter, #NonNull FinishedGameItem item, int position) {
item.details = !item.details;
mFastAdapter.notifyItemChanged(position);
return true;
}
});

How to fetch data from another classes via Adapter in Android

Here I'm trying to make a quiz application without using databases (requirement). Each question has 4 options.
I had made a class for Questions. Now, in the activity in which I want to show my data, I'm unable to get method to fetch the data from the QuestionModelClass.
I had made 2D Array but it gets more complicated to get it. Is there any way to bind 3 of the classes (QuestionModelClass, Adapter class, and Activity class)?
public class QuestionsModelClass {
private String sQuestion;
private String sRightAnswer;
private List<String> sOptions;
QuestionsModelClass(){
sQuestion = null;
sRightAnswer = null;
sOptions = null;
}
public QuestionsModelClass(String sQuestion, String sRightAnswer, List<String> sOptions) {
this.sQuestion = sQuestion;
this.sRightAnswer = sRightAnswer;
this.sOptions = sOptions;
}
public String getsQuestion() {
return sQuestion;
}
public void setsQuestion(String sQuestion) {
this.sQuestion = sQuestion;
}
public String getsRightAnswer() {
return sRightAnswer;
}
public void setsRightAnswer(String sRightAnswer) {
this.sRightAnswer = sRightAnswer;
}
public List<String> getsOptions() {
return sOptions;
}
public void setsOptions(List<String> sOptions) {
this.sOptions = sOptions;
}
}
And my Adapter Class
public class QuizAdapter extends BaseAdapter {
private Context context;
private List<QuestionsModelClass> questionClassList;
private String[][] options;
private LayoutInflater inflater;
private QuizAdapter(Context c, List<QuestionsModelClass> l){
this.context= c;
this.questionClassList = l;
inflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return questionClassList.size();
}
#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) {
convertView = inflater.inflate(R.layout.questionpattern, parent,false);
QuestionsModelClass questions = questionClassList.get(position);
TextView quesText= convertView.findViewById(R.id.questionTextView);
RadioButton radioButtonA = convertView.findViewById(R.id.optionA);
RadioButton radioButtonB = convertView.findViewById(R.id.optionB);
RadioButton radioButtonC = convertView.findViewById(R.id.optionC);
RadioButton radioButtonD = convertView.findViewById(R.id.optionD);
return convertView;
}
And this is the Activity class in which I am trying to implement all the functions
public class QuizActivity extends Activity {
final Context context= this;
private List<QuestionsModelClass> classObject;
Button okayButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
String[] question= new String[]{"Q1. ABDE", "Q2. ADDASD"};
String[][] op;
String[] right = new String[]{"abc","def"};
classObject = new ArrayList<>();
op= new String[][]{
{"a1", "2", "3", "4"},
{"b1","b2","b3","b4"}};
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.customdialoguebox);
dialog.show();
okayButton = (Button) dialog.findViewById(R.id.okayButton);
okayButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(QuizActivity.this,"Good Luck!", Toast.LENGTH_SHORT).show();
dialog.cancel();
}
});
}

Data does not display properly in Adapter when Removing/Adding

I am running into an issue where I use 2 separate adapters, and 2 seperate GridViews (1 Adapter, for 1 GridView), and am basically removing items from either adapter, and adding it to the other if the item is pressed, and vice versa.
When I press and item in one GridView, the item will be removed from its respective adapter and put into the other GridView and its respective adapter. The issue seems to be with the way the data is being stored (potentially) because when I add the item to the adapter, the icons are totally different from what they should be.
Note: I also noticed that this only happens for the items pressed after the first one is pressed. So the first items data will be added/removed properly. However, I have found that when the item that takes on the first position of the associated adapter, will now have the icon of the previously removed item.
What can I do so ensure that the data stays consistent with the item that is pressed?.
Adapter Class
public class MiscChargeOptionsAdapter extends ArrayAdapter<CarClassSettingDetails> {
private LayoutInflater inflater;
public MiscChargeOptionsAdapter(Context context, List<CarClassSettingDetails> settingList) {
super(context, R.layout.misc_charge_option_layout, R.id.option_grid_option_text, settingList);
inflater = LayoutInflater.from(context);
}
#SuppressLint("InflateParams")
#Override
public View getView(int position, View convertView, ViewGroup parent) {
CarClassSettingDetails settingsDetails = this.getItem(position);
ViewHolder vh;
if (convertView == null) {
vh = new ViewHolder();
convertView = inflater.inflate(R.layout.misc_charge_option_layout, null);
vh.optionText = (TextView) convertView.findViewById(R.id.option_grid_option_text);
vh.settingView = (SettingView) convertView.findViewById(R.id.option_grid_option_icon);
convertView.setTag(vh);
} else {
vh = (ViewHolder) convertView.getTag();
}
vh.optionText.setText(settingsDetails.getName());
vh.settingView.setIcon(settingsDetails.getIcon());
vh.settingView.setIconSelected(settingsDetails.getIconSelected());
vh.settingView.setViewBackgroundColor(settingsDetails.getViewBackgroundColor());
vh.settingView.setBackgroundColorSelected(settingsDetails.getBackgroundColorSelected());
vh.settingView.setAmountBackgroundColor(settingsDetails.getAmountBackgroundColor());
vh.settingView.setAmountTextColor(settingsDetails.getAmountTextColor());
vh.settingView.setValue(settingsDetails.getValue());
vh.settingView.setIgnoreSetState(true);
vh.settingView.setTag(position);
return convertView;
}
class ViewHolder {
private TextView optionText;
private SettingView settingView;
/* public ViewHolder(TextView optionText, SettingView settingView) {
this.optionText = optionText;
this.settingView = settingView;
}*/
public TextView getOptionText() {
return optionText;
}
public SettingView getSettingView() {
return settingView;
}
}
}
How the adapters are set
selectedAdapter = new CarClassOptionsAdapter(getActivity(), selectedSettingsList);
unselectedAdapter = new CarClassOptionsAdapter(getActivity(), unselectedSettingsList);
gridSelected.setAdapter(selectedAdapter);
gridSelected.setOnItemClickListener(this);
gridUnselected.setAdapter(unselectedAdapter);
gridUnselected.setOnItemClickListener(this);
How I am adding/removing items from the adapters
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (parent.getAdapter() == selectedAdapter) {
//TODO: ADD DIALOGS
unselectedAdapter.add(selectedAdapter.getItem(position));
unselectedAdapter.notifyDataSetChanged();
selectedAdapter.remove(selectedAdapter.getItem(position));
selectedAdapter.notifyDataSetChanged();
} else if (parent.getAdapter() == unselectedAdapter) {
//TODO: ADD DIALOGS
selectedAdapter.add(unselectedAdapter.getItem(position));
selectedAdapter.notifyDataSetChanged();
unselectedAdapter.remove(unselectedAdapter.getItem(position));
unselectedAdapter.notifyDataSetChanged();
}
}
This is how the CarClassSettingDetails class is being populated
private void addMiscChargeSelected(Ws_MiscChargeSelected miscChargeSelected, boolean isSelected) {
try {
CarClassSettingDetails settingDetails = new CarClassSettingDetails(getActivity());
if (miscChargeSelected.getCode() != null && !miscChargeSelected.getCode().equals("")) {
settingDetails.setCode(miscChargeSelected.getCode());
}
if (miscChargeSelected.getName() != null && !miscChargeSelected.getName().equals("")) {
settingDetails.setName(miscChargeSelected.getName());
}
if (miscChargeSelected.getIcon() != null && !miscChargeSelected.getIcon().equals("")) {
Bitmap settingIcon = IconUtils.loadIcon(getActivity(), miscChargeSelected.getIcon());
Bitmap settingIconSelected = IconUtils.loadIcon(getActivity(), miscChargeSelected.getIcon());
settingDetails.setIcon(settingIcon);
settingDetails.setIconSelected(settingIconSelected);
}
if (miscChargeSelected.getValue() != null && !miscChargeSelected.getValue().equals("")) {
settingDetails.setValue(Ws_Value.fromInt(Integer.parseInt(miscChargeSelected.getValue())));
settingDetails.setAmountBackgroundColor(Color.parseColor("#00ffffff"));
settingDetails.setAmountTextColor(Color.parseColor("#ff00428e"));
} else {
settingDetails.setValue(null);
}
settingDetails.setViewBackgroundColor(Color.parseColor("#ffd4d4d4"));
settingDetails.setBackgroundColorSelected(Color.parseColor("#ff616161"));
if (isSelected) {
//TODO: ADD TO TOP ADAPTER
selectedSettingsList.add(settingDetails);
} else {
//TODO: ADD TO BOTTOM ADAPTER
unselectedSettingsList.add(settingDetails);
}
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
}
Misc Charge Details
public class MiscChargeSettingDetails {
private Context context;
private String code;
private String name;
private String description;
private Bitmap icon = null;
private Bitmap iconSelected = null;
private Ws_Value value = null;
private int amountBackgroundColor, amountTextColor, viewBackgroundColor, backgroundColorSelected;
public MiscChargeSettingDetails(Context context) {
this.context = context;
}
protected Context getContext() {
return context;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Bitmap getIcon() {
return icon;
}
public void setIcon(Bitmap icon) {
this.icon = icon;
}
public Bitmap getIconSelected() {
return iconSelected;
}
public void setIconSelected(Bitmap iconSelected) {
this.iconSelected = iconSelected;
}
public Ws_Value getValue() {
return value;
}
public void setValue(Ws_Value value) {
this.value = value;
}
public int getAmountBackgroundColor() {
return amountBackgroundColor;
}
public void setAmountBackgroundColor(int amountBackgroundColor) {
this.amountBackgroundColor = amountBackgroundColor;
}
public int getAmountTextColor() {
return amountTextColor;
}
public void setAmountTextColor(int amountTextColor) {
this.amountTextColor = amountTextColor;
}
public int getViewBackgroundColor() {
return viewBackgroundColor;
}
public void setViewBackgroundColor(int viewBackgroundColor) {
this.viewBackgroundColor = viewBackgroundColor;
}
public int getBackgroundColorSelected() {
return backgroundColorSelected;
}
public void setBackgroundColorSelected(int backgroundColorSelected) {
this.backgroundColorSelected = backgroundColorSelected;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
}
Can you show the place where you're populating the list of SettingViews? Is your data model (SettingView) extends View? It is not the best idea to use the view object as model..

Endless pagination listview Android with database

I need to implement an endless pagination listview in my code, I've searched online and I saw a lot of examples, but none of them is using database, my app does the following:
it connects to an API and retrieves the data with json and show it in a listview, ok that works fine, but I want that listview to have an infinite scroll, like the facebook one.
I don't want anyone to write the code for me, I'm asking to someone guide me on wich is the better way to achieve that, I'll share some of my code, so you can understand:
try {
//Repositorio is my database
Repositorio mRepositorio = new Repositorio(getActivity());
List listaDeClientes = mRepositorio.getClientes();
System.out.println(listaDeClientes);
TextView total = (TextView) rootView.findViewById(R.id.totalClientes);
total.setText(getTotalClientes(mRepositorio.getTotalRegistros("clientes")));
final ArrayAdapter ad = new ClienteViewAdapter(this.getActivity(), R.layout.fragment_cliente_item, listaDeClientes);
ListView lv = (ListView) rootView.findViewById(R.id.listaClientes);
lv.setVerticalFadingEdgeEnabled(true);
lv.setVerticalScrollBarEnabled(true);
lv.setAdapter(ad);
} catch (Exception e) {
e.printStackTrace();
}
return rootView;
}
ClienteViewAdapter:
public class ClienteViewAdapter extends ArrayAdapter<ClienteModel> {
private final LayoutInflater inflater;
private final int resourceId;
public ClienteViewAdapter(Context context, int resource, List<ClienteModel> objects) {
super(context, resource, objects);
this.inflater = LayoutInflater.from(context);
this.resourceId = resource;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
ClienteModel mClienteModel = getItem(position);
view = inflater.inflate(resourceId, parent, false);
TextView tvId = (TextView) view.findViewById(R.id.clienteId);
TextView tvNome = (TextView) view.findViewById(R.id.clienteNome);
TextView tvTipo = (TextView) view.findViewById(R.id.clienteTipo);
tvId.setText(String.valueOf(mClienteModel.getClientes_id()));
tvNome.setText(mClienteModel.getNome());
tvTipo.setText(mClienteModel.getTipo());
return view;
}
}
Model:
public class ClienteModel implements Serializable {
private static final long serialVersionUID = 1L;
private Integer clientes_id;
private Integer id_rm;
private Integer credencial_id;
private String nome;
private String tipo;
private String informacao_adicional;
private String _criado;
private String _modificado;
private String _status;
public Integer getClientes_id() {
return clientes_id;
}
public void setClientes_id(Integer clientes_id) {
this.clientes_id = clientes_id;
}
public Integer getId_rm() {
return id_rm;
}
public void setId_rm(Integer id_rm) {
this.id_rm = id_rm;
}
public Integer getCredencial_id() {
return credencial_id;
}
public void setCredencial_id(Integer credencial_id) {
this.credencial_id = credencial_id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getTipo() {
return tipo;
}
public void setTipo(String tipo) {
this.tipo = tipo;
}
public String getInformacao_adicional() {
return informacao_adicional;
}
public void setInformacao_adicional(String informacao_adicional) {
this.informacao_adicional = informacao_adicional;
}
public String get_criado() {
return _criado;
}
public void set_criado(String _criado) {
this._criado = _criado;
}
public String get_modificado() {
return _modificado;
}
public void set_modificado(String _modificado) {
this._modificado = _modificado;
}
public String get_status() {
return _status;
}
public void set_status(String _status) {
this._status = _status;
}
public static String[] getColunas() {
return Colunas;
}
public static void setColunas(String[] colunas) {
Colunas = colunas;
}
public static String[] Colunas = new String[]{
Coluna.CLIENTES_ID,
Coluna.ID_RM,
Coluna.CREDENCIAL_ID,
Coluna.NOME,
Coluna.TIPO,
Coluna.INFORMACAO_ADICIONAL,
Coluna._CRIADO,
Coluna._MODIFICADO,
Coluna._STATUS
};

Categories

Resources