ArrayAdapter<List<Object>> update on TimerTaskUpdate updating an object - android

I am new to Android and I am trying to do this. I have an ArrayAdapter, a Fragment using this adapter and a class that runs a timer task every 15 mins to hit an api and update my cached superobject. This superobject contains a field list. Also, I am using a calendar to filter from this list and my fragment displays the filtered list. So when i click on a date on calendar, SampleObjects with that creation date will be shown in the fragment. I want to force update this list whenever my timer task updates the superobject. Is it possible and if so, how?
public class ScheduleAdapter extends ArrayAdapter<SampleObject> {
private List<SampleObject> objects;
public void setObjects(List<SampleObject> objects) {
this.objects = objects;
}
public ScheduleAdapter(Context context, List<SampleObject> objects) {
super(context, R.layout.schedule_list_item, R.id.schedule_list_layout, objects);
this.objects = objects;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = LayoutInflater.from(getContext()).inflate(R.layout.schedule_list_item, parent, false);
ViewHolder holder = new ViewHolder(row);
row.setTag(holder);
final SampleObject sam = objects.get(position);
if (row == null) {
LayoutInflater inflater = ((Activity) getContext()).getLayoutInflater();
row = inflater.inflate(R.layout.schedule_list_item, parent, false);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
holder.customerView.setText(sampleObject.getStatus());
holder.addressView.setText(cleaningJob.getShortenedAddress());
holder.timingView.setText(sampleObject.getFormattedStartTime() + "-" + cleaningJob.getFormattedEndTime());
holder.iconView.setImageResource(R.drawable.ic_clear);
return row;
}
public static class ViewHolder {
public final ImageView iconView;
public final TextView customerView;
public final TextView addressView;
public final TextView timingView;
public ViewHolder(View view) {
iconView = (ImageView) view.findViewById(R.id.list_item_icon);
customerView = (TextView) view.findViewById(R.id.list_item_customer_textview);
addressView = (TextView) view.findViewById(R.id.list_item_address_textview);
timingView = (TextView) view.findViewById(R.id.list_item_timing_textview);
}
}
}
}
public class ScheduleFragment extends Fragment {
private static final String LOG_TAG = "ScheduleFragment";
private ScheduleAdapter mScheduleAdapter;
public static final String ARGS_CLEANING_JOB_ID = "id";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Bundle args = getArguments();
Date requestedDate = null;
if (args != null) {
int day = args.getInt("date");
int month = args.getInt("month");
int year = args.getInt("year");
requestedDate = BasicUtility.constructDate(day, month, year);
}
List<SampleObject> sampleObjs = Collections.EMPTY_LIST;
if (requestedDate != null) {
List<SampleObject> sampleObjsFromController = ScheduleController.getSampleObjByDate(requestedDate);
if (CollectionUtils.isNotEmpty(sampleObjsFromController)) {
sampleObjs = sampleObjsFromController;
}
}
mScheduleAdapter =
new ScheduleAdapter(getActivity(), sampleObjs);
View scheduleListView = inflater.inflate(R.layout.schedule_list_fragment, container, false);
ListView listView = (ListView) scheduleListView.findViewById(R.id.schedule_list);
listView.setAdapter(mScheduleAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
SampleObject job = mScheduleAdapter.getItem(position);
Bundle bundle = new Bundle();
bundle.putString(ARGS_CLEANING_JOB_ID, job.getUniqueIdentifier());
Intent intent = new Intent(getActivity(), DetailActivity.class).putExtras(bundle);
startActivity(intent);
}
});
return scheduleListView;
}
}
public class CalendarFragment extends Fragment {
private static final String LOG_TAG = "CalendarFragment";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.calendar_view, container, false);
CalendarView calendarView = (CalendarView) rootView.findViewById(R.id.calendarView);
calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
#Override
public void onSelectedDayChange(CalendarView calendarView, int i, int i1, int i2) {
Log.d(LOG_TAG, "Showing item for date " + i + "::" + i1+ "::" + i2);
Bundle args = new Bundle();
Fragment frag = new ScheduleFragment();
frag.setArguments(args);
args.putInt("date", i2);
args.putInt("month", i1 + 1);
args.putInt("year", i);
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.schedule_list_container,frag, MainActivity.SCHEDULEFRAGMENT_TAG)
.commit();
}
});
return rootView;
}
}
public class ScheduleController {
private static Map<String, List<String>> sampleObjsByDate = new HashMap<>();
private static final String LOG_TAG = "ScheduleController";
static {
TimerTask timerTask = new TimerTask() {
#Override
public void run() {
Log.i(LOG_TAG, "updating schedule...");
populateSchedule();
}
};
Timer timer = new Timer(true);
timer.scheduleAtFixedRate(timerTask, 0, 15*60*1000);
}
public static void populateSchedule() {
//populate SuperObject here
}
public static List<SampleObject> getCleaningJobByDate(Date date) {
//filter list here
}

I should not have used a timerTask in the first place. I am using the IntentService now with AlarmManager and ResultReceiver and it worked like a charm. If someone else is looking for the same funcitonality, follow the tutorial https://guides.codepath.com/android/Starting-Background-Services

Related

Drawing a listview through ListItem.add returns always a position = 0

I have an array of 3 elements which I try to draw in a listview. The issue is that it only draws the first entry because getView always returns a position = 0.
Why is that? What do I do wrong?
my main java (fragment):
public class PSGlobalFragment extends Fragment {
List<PSGitem> listPSGitem;
ListView list;
PSGadaptater psgAdapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.psglobal, container, false);
}
#Override
public void onActivityCreated(final Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String ip;
listPSGitem = new ArrayList<>();
psgAdapter = new PSGadaptater(getActivity(), listPSGitem);
listPSGitem.clear();
StoreDevDiscovery store = new StoreDevDiscovery();
// this is where I store the data
int count = store.getMax();
for(int i=0;i<count;i++){
ip = store.getIPDiscovery(i);
PSGitem item = new PSGitem();
item.setIp(ip);
listPSGitem.add(item);
list.setAdapter(psgAdapter);
}
}
and my adapter:
public class PSGadaptater extends BaseAdapter {
private int size = 0;
private List<PSGitem> listIp;
private LayoutInflater layoutInflater;
Context context;
public PSGadaptater(Context c, List<PSGitem> objects) {
context = c;
listIp = objects;
layoutInflater = LayoutInflater.from(context);
}
#Override
public void notifyDataSetChanged() {
size = listIp.size();
super.notifyDataSetChanged();
}
#Override
public int getCount() {
return listIp.size();
}
public Object getItem(int position) {
return listIp.get(position);
}
public long getItemId(int position) {
return position;
}
private class ViewIPHolder {
TextView ip_psg;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewIPHolder viewHolder;
if(convertView == null) {
viewHolder = new ViewIPHolder();
convertView = layoutInflater.inflate(R.layout.listview_item_psg, null);
viewHolder.ip_psg = (TextView) convertView.findViewById(R.id.ipaddr_psg);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewIPHolder) convertView.getTag();
}
viewHolder.ip_psg.setText(listIp.get(position).getIpaddr());
// position always = 0 this is my issue
return convertView;
}
}
and the PSCitem.java:
public class PSGitem {
private String ip1;
public String getIp(){
return ip1;
}
public void setIp(String ip){
ip1 = ip;
}
}
The problem is that you are creating your Adapter from an empty set of items:
listPSGitem = new ArrayList<>();
psgAdapter = new PSGadaptater(getActivity(), listPSGitem);
If you wish to add items to the adapter later, you should add the items to the adapter listIp list variable, and then let the adapter know about this change with notifyDataSetChanged() method.
Change your onActivityCreated method like below.
#Override
public void onActivityCreated(final Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String ip;
listPSGitem = new ArrayList<>();
listPSGitem.clear();
StoreDevDiscovery store = new StoreDevDiscovery();
// this is where I store the data
int count = store.getMax();
for(int i=0;i<count;i++){
ip = store.getIPDiscovery(i);
PSGitem item = new PSGitem();
item.setIp(ip);
listPSGitem.add(item);
}
psgAdapter = new PSGadaptater(getActivity(), listPSGitem);
list.setAdapter(psgAdapter);
}

List view showing on another fragment

I'm using a view pager with a sliding panel inside, so when my panel is expanded it creates a request of users and instantiates viewholders to show them in the list view, the problem is that they get instantiated on wherever they want, how I can tell in what fragment it should be instantiated.
Here is my code:
#Override
public void onPanelAnchored(View panel) {
final View cView = panel;
EndpointInterface Service = ServiceAuthGenerator.createService(EndpointInterface.class);
currentID = sharedpreferences.getInt("CURRENTID", 0);
Call<List<Ride>> call = Service.getPassengers(currentRide);
call.enqueue(new Callback<List<Ride>>() {
#Override
public void onResponse(Response<List<Ride>> response, Retrofit retrofit) {
if (response.isSuccess() && !response.body().isEmpty()) {
dialogx.dismiss();
ArrayList<String> myUsersName = new ArrayList<>();
ArrayList<String> myUsersLastName = new ArrayList<>();
ArrayList<String> myUsersMapDirection = new ArrayList<>();
ArrayList<Integer> myUsersID = new ArrayList<>();
ArrayList<Boolean> myUsersRole = new ArrayList<>();
for (int i = 0; i < response.body().size(); i++) {
myUsersRole.add(response.body().get(i).getRole());
myUsersName.add(response.body().get(i).getUser().getFirst_name());
myUsersLastName.add(response.body().get(i).getUser().getLast_name());
myUsersMapDirection.add(getAdress(new LatLng(response.body().get(i).getOrigin_lat(), response.body().get(i).getOrigin_lng())));
myUsersID.add(response.body().get(i).getId());
currentName = myUsersName.get(i) + " " + myUsersLastName.get(i);
mMap.addMarker(new MarkerOptions().snippet(getAdress(new LatLng(response.body().get(Integer.valueOf(i)).getOrigin_lat(), response.body().get(Integer.valueOf(i)).getOrigin_lng()))).position(new LatLng(response.body().get(Integer.valueOf(i)).getOrigin_lat(), response.body().get(Integer.valueOf(i)).getOrigin_lng())).title(response.body().get(Integer.valueOf(i)).getUser().getFirst_name()).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)));
}
ListAdapter userAdapter = new CustomAdapterRequest(MainMenu.this, myUsersName, myUsersLastName, myUsersMapDirection, myUsersID, myUsersRole, currentRide);
ListView userListView = (ListView) cView.findViewById(R.id.listViewUserRequest);
userListView.setAdapter(userAdapter);
}
}
#Override
public void onFailure(Throwable t) {
Toast.makeText(getApplicationContext(), "no", Toast.LENGTH_SHORT).show();
}
});
}
Also, here is my adapter code:
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
myViewHolder holder = null;
if (row == null) {
LayoutInflater customInflater = (LayoutInflater) contexto.getSystemService(contexto.LAYOUT_INFLATER_SERVICE);
row = customInflater.inflate(R.layout.custom_row_request, parent, false);
holder = new myViewHolder(row);
row.setTag(holder);
} else {
holder = (myViewHolder) row.getTag();
}
String singleNameItem = itemName.get(position);
String singleLastNameItem = itemLastName.get(position);
String singleDir = itemDirection.get(position);
Integer singleID = itemIDs.get(position);
Boolean singleRole = itemRoles.get(position);
holder.tv_name.setText(singleNameItem + " " + singleLastNameItem);
holder.tv_Direction.setText(singleDir);
holder.im_profilepic.setImageResource(R.mipmap.profile_photo3);
return row;
}
And my holder class.
class myViewHolder {
TextView tv_name;
TextView tv_Direction;
ImageView im_profilepic;
myViewHolder(View v) {
tv_name = (TextView) v.findViewById(R.id.nameText);
tv_Direction = (TextView) v.findViewById(R.id.originText);
im_profilepic = (ImageView) v.findViewById(R.id.ivImage);
}
}
This is the Fragment class
public class fragment1 extends Fragment {
public fragment1() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container == null) {
return null;
}
return (CardView) inflater.inflate(R.layout.layout1, container, false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
public void setTextDestination(String origin, String Destination, Long Date, String estimatedTime, boolean singleRole) {
TextView tv_Destination = (TextView) getView().findViewById(R.id.TextDestination);
TextView tv_origin = (TextView) getView().findViewById(R.id.TextOrigin);
TextView tv_Date = (TextView) getView().findViewById(R.id.textDatePager);
TextView tv_EstimatedTiem = (TextView) getView().findViewById(R.id.estimatedTimeRoute);
ImageView iv_roleType = (ImageView) getView().findViewById(R.id.ImgView_roleTypeLayout1);
tv_Destination.setText(Destination);
tv_origin.setText(origin);
iv_roleType.setImageResource(singleRole ? R.mipmap.steerorange3 : R.mipmap.handorange3);
tv_EstimatedTiem.setText(estimatedTime);
java.util.Date date = new Date(Date * 1000L);
DateFormat format = new SimpleDateFormat("dd-MM-yyyy hh:mm a");
format.setTimeZone(TimeZone.getDefault());
String formatted = format.format(date);
tv_Date.setText(formatted);
}
}
I created a list of fragment1 which is mu fragment class and added it to a list, then depending on how many items on the list I have is the number of instances I get, my set text function works correctly but I don't know how to do that with the list view!
Thanks! :D
moved the method that added the list view to the fragment that was instantiated.

Unknown error while trying startActivity from BaseAdapter

I am doing custom ListView in android and I have a share button on each item. After clicking button it must create chooser(facebook, etc). But when I click it the app crashes and logcat shows nothing. Please help me
Here is my Adapter:
public class AdapterQ extends BaseAdapter {
List<Quotes> sss;
Context ss;
Intent sendIntent;
public AdapterQ(Context ss, List<Quotes>items){
this.ss = ss;
this.sss = items;
}
#Override
public int getCount() {
return sss.size();
}
#Override
public Object getItem(int position) {
return sss.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
ss.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.quoteitem, null);
}
ImageView imgIcon = (ImageView) convertView.findViewById(R.id.legendImage);
final TextView tvTitle = (TextView) convertView.findViewById(R.id.legendName);
ImageView ssss= (ImageView) convertView.findViewById(R.id.Nation);
final Quotes quotes = sss.get(position);
imgIcon.setImageResource(quotes.getIcon());
tvTitle.setText(quotes.getText());
ssss.setImageResource(R.mipmap.ic_share_black_24dp);
ssss.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sendIntent =new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, tvTitle.getText());
sendIntent.setType("text/plain");
ss.startActivity(Intent.createChooser(sendIntent, "Поделиться..."));
}
});
return convertView;
}
}
Quotes:
public class Quotes {
private String mText;
private int mIcon;
public String getText() {
return mText;
}
public void setText(String mText) {
this.mText = mText;
}
public int getIcon() {
return mIcon;
}
public void setIcon(int mIcon) {
this.mIcon = mIcon;
}
}
Fragment:
public class AllFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rrrr = inflater.inflate(R.layout.fragment_all, container, false);
String[] ss = getResources().getStringArray(R.array.Абай);
String[] qq = getResources().getStringArray(R.array.Абылай);
String[] sr = new String[ss.length + qq.length];
System.arraycopy(ss, 0, sr, 0, ss.length);
System.arraycopy(qq, 0, sr, ss.length, qq.length);
ListView se = (ListView) rrrr.findViewById(R.id.listView2);
shuffleArray(sr);
List<Quotes> ser = new ArrayList<Quotes>();
for (int s = 0; s<sr.length; s++){
if(Arrays.asList(ss).contains(sr[s]) ){
Quotes quotes = new Quotes();
quotes.setText(sr[s]);
quotes.setIcon(R.drawable.abay);
ser.add(quotes);
} else if(Arrays.asList(qq).contains(sr[s])){
Quotes quotes = new Quotes();
quotes.setText(sr[s]);
quotes.setIcon(R.drawable.abylai);
ser.add(quotes);
}
}
AdapterQ ses = new AdapterQ(getActivity().getApplicationContext(), ser);
se.setAdapter(ses);
return rrrr;
}
Here:
AdapterQ ses = new AdapterQ(getActivity().getApplicationContext(), ser);
Try this:
AdapterQ ses = new AdapterQ(getActivity(), ser);

Cannot see listview in fragment

I am currently modifying an android app that I need to add a listview to an existing fragment. As I am new to android, I am just imitating the code from the apps. I created a new arrayadapter, a new class of data and made some modifies to the existing fragment class. The problem is I cannot see my list in the app. Below are my codes.
Adapter
public class RecordArrayAdapter extends ArrayAdapter<CheckInRecord.CheckInRec> {
private int resourceId;
private Context context;
private List<CheckInRecord.CheckInRec> checkInRec;
public RecordArrayAdapter(Context context, int resourceId, List<CheckInRecord.CheckInRec> checkInRec)
{
super(context, resourceId, checkInRec);
this.resourceId = resourceId;
this.context = context;
this.checkInRec = checkInRec;
}
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null){
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
convertView = inflater.inflate(resourceId, parent, false);
}
TextView textViewName = (TextView) convertView.findViewById(R.id.tv_name);
TextView textViewCheckInDate = (TextView) convertView.findViewById(R.id.tv_checkindate);
TextView textViewPoints = (TextView) convertView.findViewById(R.id.tv_points);
ImageView imageViewIcon = (ImageView) convertView.findViewById(R.id.iv_icon);
CheckInRecord.CheckInRec checkInrec = checkInRec.get(position);
textViewName.setText(checkInrec.providerName);
textViewCheckInDate.setText(checkInrec.checkInDate);
textViewPoints.setText(checkInrec.providerPoints);
ImageLoader.getInstance().displayImage(checkInrec.providerIcon, imageViewIcon, Utility.displayImageOptions);
return convertView;
}
public int getIsPrize(int position) {return (this.checkInRec.get(position).isPrize);}
}
Data type
public class CheckInRecord {
public int userPoints;
public String userName;
public String gender;
public String birthDate;
public String location;
public String userIcon;
public List<CheckInRec> checkInRecList = new ArrayList<CheckInRec>();
public void addCheckInRec(String providerName, String providerLocation, String providerIcon,
String checkInDate, int providerPoints, int isPrize){
CheckInRec checkInRec = new CheckInRec();
checkInRec.providerName = providerName;
checkInRec.providerLocation = providerLocation;
checkInRec.providerIcon = providerIcon;
checkInRec.checkInDate = checkInDate;
checkInRec.providerPoints = providerPoints;
checkInRec.isPrize = isPrize;
checkInRecList.add(checkInRec);
}
public List<String> recImages(){
List<String> resultList = new ArrayList<String>();
if (this.checkInRecList == null){
return resultList;
}
for (CheckInRec rec : this.checkInRecList){
resultList.add(rec.providerIcon);
}
return resultList;
}
public class CheckInRec{
public String providerName;
public String providerLocation;
public String providerIcon;
public String checkInDate;
public int providerPoints;
public int isPrize;
}
}
Fragment
public class MeFragment extends Fragment implements ApiRequestDelegate {
private TextView textViewName;
private TextView textViewPoints;
private ProgressDialog progressDialog;
private RecordArrayAdapter recordArrayAdapter;
private List<CheckInRecord.CheckInRec> checkInRec = new ArrayList<CheckInRecord.CheckInRec>();
public MeFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AppDataManager.getInstance().setAllowCheckIn(true);
progressDialog = ProgressDialog.show(getActivity(), "", "");
ApiManager.getInstance().checkInHistories(AppDataManager.getInstance().getUserToken(), AppDataManager.getInstance().getUserPhone(),
Utility.getPictureSize(), this);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_me, container, false);
textViewName = (TextView) view.findViewById(R.id.tv_name);
textViewPoints = (TextView) view.findViewById(R.id.tv_points);
ListView listViewCheckInRec = (ListView) view.findViewById(R.id.lv_histories);
recordArrayAdapter = new RecordArrayAdapter(this.getActivity().getApplicationContext(), R.layout.row_record, checkInRec);
listViewCheckInRec.setAdapter(recordArrayAdapter);
return view;
}
#Override
public void setMenuVisibility(boolean menuVisible) {
super.setMenuVisibility(menuVisible);
if (menuVisible) {
refreshName();
}
}
public void refreshName() {
progressDialog = ProgressDialog.show(getActivity(), "", "");
AppDataManager dataManager = AppDataManager.getInstance();
ApiManager.getInstance().checkInHistories(dataManager.getUserToken(), dataManager.getUserPhone(), Utility.getPictureSize(), this);
}
#Override
public void apiCompleted(ApiResult apiResult, HttpRequest httpRequest) {
if (progressDialog!=null){
progressDialog.dismiss();
}
if (!apiResult.success){
ApiManager.handleMessageForReason(apiResult.failReason, getActivity());
return;
}
CheckInRecord checkInRecord = (CheckInRecord) apiResult.valueObject;
if (checkInRecord != null){
textViewName.setText(checkInRecord.userName);
textViewPoints.setText(String.format("积分%d分", checkInRecord.userPoints));
// this.checkInRec.clear();
// this.checkInRec.addAll(checkInRecord.checkInRecList);
//
// recordArrayAdapter.notifyDataSetChanged();
}
}
}
The problem is I cannot see my list in the app.
That is because checkInRec does now have any elements inside of it.
I can really tell that it is empty because you commented this out:
// this.checkInRec.clear(); //clear the old data from the list
// this.checkInRec.addAll(checkInRecord.checkInRecList); //add all the data inside the checkInRecord.checkInRecList
//
// recordArrayAdapter.notifyDataSetChanged(); //refreshing the ListView to display the new data
now what are those doing is that clearing the old list array and adding the new set of data from checkInRecord.checkInRecList and refreshing the ListView so those new data are implemented/shown in your ListView.

how to deselect a row in ListFragment

I have the following code which displays a ListFragment. Once a row is selected i turn the row's background to red. If i click another row then that turns red but the first selected row remains red.
How can i turn the colour back for the deselected rows? I've tried a few things like, clearCoices(), list.invalidate(), list.requestLayout(), list.refreshDrawableState. None of them seem to work.
Thanks in advance, Matt
public class CarerDetailsFragment extends ListFragment{
private static final String TAG = CarerDetailsFragment.class.getSimpleName();
MySimpleArrayAdapter myAdapter;
TwoDimensionalArrayList rotaArray;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle b = getArguments();
if(b != null){
rotaArray = (TwoDimensionalArrayList) b.get("rotaArray");
Log.e(TAG, "rotaArray in CarerDetailsFragment has size " + rotaArray.size());
}else{
Log.e(TAG, "Bundle b = null!!!!!!!!!!!");
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.carerdetailsfragmentlayout, container, false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
myAdapter = (MySimpleArrayAdapter) new MySimpleArrayAdapter(getActivity(), rotaArray);
setListAdapter(myAdapter);
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Log.e(TAG, "onListItemClick");
l.clearChoices();
v.setBackgroundColor(Color.parseColor("#FF0000"));
String name;
String actTimeIn;
String actTimeOut;
String doubleUpValue;
String status;
String startTime;
String clientID;
String notes;
Bundle b = new Bundle();
b.putString("name", name);
b.putString("actTimeIn", actTimeIn);
Fragment newFragment = new CarerPurposeOfCallFragment();
newFragment.setArguments(b);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.carerpurposeofcall, newFragment);
//transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
}
private class MySimpleArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final ArrayList<?> list;
String justTime;
String statusField;
String callID;
String needName;
public MySimpleArrayAdapter(Context context, ArrayList<?> list) {
super(context, R.layout.rotarowlayout);
Log.e(TAG, "inside adapter constructor");
this.context = context;
this.list = list;
//Log.e(TAG, "list has size of " + this.list.size());
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.rotarowlayout, parent, false);
TextView startTime = (TextView) rowView.findViewById(R.id.rowstarttime);
TextView duration = (TextView) rowView.findViewById(R.id.rowduration);
TextView status = (TextView) rowView.findViewById(R.id.rowstatus);
TextView name = (TextView) rowView.findViewById(R.id.rowclientname);
final ImageView noteStatus = (ImageView)rowView.findViewById(R.id.notestatus);
return rowView;
}
#Override
public int getCount() {
if(this.list != null){
return this.list.size();
}else{
return 0;
}
}
}// end of adapter class
}//end of CarerListFragment
Use myAdapter.toggleSelection(position); to select or unselect.
You probably need to have selectedList and do checks when you toggle.

Categories

Resources