In alert dialog box, I want to show the selected value - java

public class QuotesFragment extends Fragment implements QuotesDataHelper.QuotesListener, QuotesAdapter.OnQuoteActionListener {
public static final String TAG = QuotesFragment.class.getSimpleName();
private Views mViews;
private QuotesDataHelper mDataHelper;
private Quote mSelectedQuote;
public QuotesFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment
*
* #return A new instance of fragment QuotesFragment.
*/
public static QuotesFragment newInstance() {
QuotesFragment fragment = new QuotesFragment();
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
/**
* Retrieve the request for quotes data from service
*/
private void getQuotesData() {
showProgress(true);
mDataHelper.getQuotesDetails(LobbSharedPreferences.getFromSharedPreferences(getContext(), StringUtils.LOBB_SHARED_PREFERENCE_TRUCK_OWNER_ID));
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_quotes_page, container, false);
mViews = new Views(rootView);
mDataHelper = new QuotesDataHelper(this);
getQuotesData();
return rootView;
}
#Override
public void onResume() {
super.onResume();
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
#Override
public void onDetach() {
super.onDetach();
}
/**
* Shows the progress UI and hides the dashboard.
*/
private void showProgress(final boolean show) {
mViews.mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
mViews.mQuotesView.setVisibility(show ? View.GONE : View.VISIBLE);
}
private void showNoDataView(String message) {
mViews.mNoDataView.setVisibility(View.VISIBLE);
mViews.mQuotesView.setVisibility(View.GONE);
mViews.mNoDataView.setText(message);
}
private void showToast(String message) {
Toast.makeText(getActivity(), message, Toast.LENGTH_LONG).show();
}
#Override
public void showQuotes(ArrayList<Quote> quotesList) {
showProgress(false);
if (quotesList != null && quotesList.size() > 0) {
mViews.mQuotesView.setHasFixedSize(true);
mViews.mQuotesView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
final LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mViews.mQuotesView.setLayoutManager(layoutManager);
QuotesAdapter mAdapter = new QuotesAdapter(getContext(), quotesList);
mAdapter.setQuoteActionListener(this);
mViews.mQuotesView.setAdapter(mAdapter);
} else {
showNoDataView(getResources().getString(R.string.quotes_nodata));
}
}
#Override
public void showError(String title, String errorMessage) {
showProgress(false);
showNoDataView(errorMessage);
}
#Override
public void onQuoteIgnored() {
showProgress(false);
showToast(getResources().getString(R.string.quote_ignore_success));
//Quote ignored . So reload the data
getQuotesData();
}
#Override
public void onQuoteIgnoreFailed() {
showProgress(false);
if (!((Activity) getContext()).isFinishing()) {
new android.app.AlertDialog.Builder(getContext()).setTitle(getResources().getString(R.string.error_title)).setMessage(getResources().getString(R.string.error_ignore_quote)).setNeutralButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).show();
}
}
#Override
public void onCreateQuoteSuccess() {
showProgress(false);
showToast(getResources().getString(R.string.quote_create_success));
//Quote created. So refresh
getQuotesData();
}
#Override
public void onCreateQuoteFailure() {
showProgress(false);
showToast(getResources().getString(R.string.quote_create_failure));
}
#Override
public void onQuote(Quote quote) {
mSelectedQuote = quote;
showConfirmationAlert();
}
private void showConfirmationAlert() {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setMessage(getResources().getString(R.string.quote_confirm));
builder.setCancelable(true);
builder.setPositiveButton(
getResources().getString(R.string.quote_confirm_yes),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
showProgress(true);
mDataHelper.createQuote(mSelectedQuote);
dialog.cancel();
}
});
builder.setNegativeButton(
getResources().getString(R.string.quote_confirm_no),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog confirmationAlert = builder.create();
confirmationAlert.show();
}
#Override
public void onIgnore(int requestID) {
showProgress(true);
mDataHelper.ignoreQuote(requestID);
}
private class Views {
RecyclerView mQuotesView;
View mProgressView;
TextView mNoDataView;
Views(View root) {
mQuotesView = (RecyclerView) root.findViewById(R.id.view_myquotes);
mProgressView = root.findViewById(R.id.progress_container_quotes);
mNoDataView = (TextView) root.findViewById(R.id.view_quotes_nodata);
}
}
}
[]
private void showConfirmationAlert() {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setMessage(getResources().getString(R.string.quote_confirm));
builder.setCancelable(true);
builder.setPositiveButton(
getResources().getString(R.string.quote_confirm_yes),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
showProgress(true);
mDataHelper.createQuote(mSelectedQuote);
dialog.cancel();
}
});
builder.setNegativeButton(
getResources().getString(R.string.quote_confirm_no),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog confirmationAlert = builder.create();
confirmationAlert.show();
}

you get the mSelectedQuote in onQuote() mth.
Now you can get the amount on mSelectedQuote
Pass it to showConfirmationAlert(amount);
set it to AlertDialog as below -
private void showConfirmationAlert(int amount) {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setMessage(getResources().getString(R.string.quote_confirm) + amount);

Related

Save items added to scroll view android studios

I have created and been able to label items that have been added to a scroll view. I want to find a way to save and store the items created so that when the user clicks off the page they still remain there.
I wanted to do this through the use of sharedprefs however I am not sure how and where the best place would be to add code. Would appreciate any suggestions.
public class Editscreen extends AppCompatActivity {
// https://codevedanam.blogspot.com/2021/04/dynamic-views-in-android.html
Button add;
AlertDialog dialog;
LinearLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_editscreen);
add = findViewById(R.id.button7);
layout = findViewById(R.id.container);
buildDialog();
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.show();
}
});
}
private void buildDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
View view = getLayoutInflater().inflate(R.layout.dialog,null);
EditText name = view.findViewById(R.id.nameEdit);
name.getText().clear();
builder.setView(view);
builder.setTitle("Enter Quiz name");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
addCard(name.getText().toString());
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
dialog = builder.create();
}
private void addCard(String name) {
View view = getLayoutInflater().inflate(R.layout.cardlayout,null);
TextView nameView = view.findViewById(R.id.name);
Button delete = view.findViewById(R.id.deletequiz);
nameView.setText(name);
delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
layout.removeView(view);
}
});
layout.addView(view);
}
I believe you can add this file to your project and you can call this static functions to save your data to pref. This is the compact way.
public class PrefUtil {
private static SharedPreferences getPrefs(Context context) {
return context.getSharedPreferences(context.getPackageName() + "_preferences",Context.MODE_PRIVATE);
}
public static void setStringPref(Context context,String key,String deger){
getPrefs(context).edit().putString(key,deger).apply();
}
public static String getStringPref(Context context,String key){
return getPrefs(context).getString(key,"");
}
public static void setIntPref(Context context,String key,int deger){
getPrefs(context).edit().putInt(key,deger).apply();
}
public static int getIntPref(Context context,String key){
return getPrefs(context).getInt(key, -1);
}
}

onResume update view from recyclerview adapter?

i have some problems with updating fragment view after item deleted. So i have one fragment which one showing all items added from another activity. And when im trying to delete item it's not updating view so i should restart fragment to see updated fragment.
There is my fragment
public class MyGarageFragment extends Fragment {
FloatingActionButton fab;
RecyclerView mRecyclerView;
DatabaseHelper databaseHelper;
RVAdapter adapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_my_garage, null);
mRecyclerView = v.findViewById(R.id.recyclerView);
databaseHelper = new DatabaseHelper(getContext());
showRecord();
fab = v.findViewById(R.id.addFabButton);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// startActivity(new Intent(getContext(), AddRecordActivity.class));
Intent intent = new Intent(getContext(), AddAutoActivity.class);
intent.putExtra("editMode",false);
startActivity(intent);
}
});
return v;
}
private void showRecord() {
adapter = new RVAdapter(getContext(),databaseHelper.getAllData(Constants.C_ADD_TIMESTAMP + " DESC"));
mRecyclerView.setAdapter(adapter);
}
#Override
public void onResume(){
super.onResume();
showRecord();
}
also my Recyclerview Adapter
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.Holder> {
private Context context;
private ArrayList<Model> arrayList;
DatabaseHelper databaseHelper;
#NonNull
#Override
public Holder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item_my_garage,parent,false);
return new Holder(view);
}
public RVAdapter(Context context, ArrayList<Model> arrayList) {
this.context = context;
this.arrayList = arrayList;
databaseHelper = new DatabaseHelper(context);
}
#Override
public void onBindViewHolder(#NonNull Holder holder, int position) {
Model model = arrayList.get(position);
String id = model.getId();
String image = model.getImage();
String name = model.getName();
String age = model.getAge();
String phone = model.getPhone();
String addTimeStamp = model.getAddTimeStamp();
String updateTimeStamp = model.getUpdateTimeStamp();
holder.profileIv.setImageURI(Uri.parse(image));
holder.name.setText(name);
holder.age.setText(age);
holder.phone.setText(phone);
holder.editButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
PopupMenu popupMenu = new PopupMenu(context, holder.editButton);
popupMenu.inflate(R.menu.popup_menu);
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_edit:
editDialog(
""+position,
""+id,
""+name,
""+age,
""+phone,
""+image,
""+addTimeStamp,
""+updateTimeStamp
);
break;
case R.id.action_delete:
deleteDialog(""+id);
break;
default:
break;
}
return false;
}
});
popupMenu.show();
}
});
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context,"item " + id, Toast.LENGTH_SHORT).show();
}
});
}
private void deleteDialog(String id) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Delete");
builder.setMessage("Are you sure?");
builder.setCancelable(false);
builder.setIcon(R.drawable.ic_baseline_delete_24);
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
databaseHelper.deleteInfo(id);
((MyGarageFragment)context).onResume();
Toast.makeText(context, "Delete Success!", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.create().show();
}
private void editDialog(String position, String id, String name, String age, String phone, String image, String addTimeStamp, String updateTimeStamp) {
Intent intent = new Intent(context, EditAutoActivity.class);
intent.putExtra("ID",id);
intent.putExtra("NAME",name);
intent.putExtra("AGE",age);
intent.putExtra("PHONE",phone);
intent.putExtra("IMAGE",image);
intent.putExtra("ADD_TIMESTAMP",addTimeStamp);
intent.putExtra("UPDATE_TIMESTAMP",updateTimeStamp);
intent.putExtra("editMode",true);
context.startActivity(intent);
}
#Override
public int getItemCount() {
return arrayList.size();
}
class Holder extends RecyclerView.ViewHolder {
ImageView profileIv;
TextView name,age,phone;
ImageView editButton;
public Holder (#NonNull View itemView){
super(itemView);
profileIv = itemView.findViewById(R.id.profileIv);
name = itemView.findViewById(R.id.name);
age = itemView.findViewById(R.id.age);
phone = itemView.findViewById(R.id.phone);
editButton = itemView.findViewById(R.id.editBtn);
}
}
}
so what should i do in deleteDialog to delete cardview item and refresh ui?
private void deleteDialog(String id) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Delete");
builder.setMessage("Are you sure?");
builder.setCancelable(false);
builder.setIcon(R.drawable.ic_baseline_delete_24);
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
databaseHelper.deleteInfo(id);
((MyGarageFragment)context).onResume();
Toast.makeText(context, "Delete Success!", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.create().show();
}

Item not deleting from sharedpreferences

I have a list that I saved in sharedpreferences I want to delete an item from the listview when the user longclicks so on the long click the app prompts you with a question to delete or not when you confirm it deletes it. This works but when i come back to the activity the list still contains that item that has been deleted
This is the code for the onCreateView. tinyDB is sharedprefrences.Thank you
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_tab_secondsemester, container, false);
btnAddNew = (Button)rootView.findViewById(R.id.btnAddNewYearOneSecondSemester);
listView = (ListView)rootView.findViewById(R.id.listViewSubjectsMyCoursesSecondSemester);
tinyDB = new TinyDB(getContext());
storedList = new ArrayList<>(tinyDB.getListString("storedList"));
generalList = new ArrayList<>();
spinnerValues = new ArrayList<>();
getActivity().setTitle("Year One");
setHasOptionsMenu(true);
btnAddNew.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(getContext(),R.style.DialogeTheme);
// Setting Dialog Title
alertDialog.setTitle("Add new");
// Setting Dialog Message
String getUsername = tinyDB.getString("Username");
alertDialog.setMessage("Hello "+ getUsername + ", please write the new subject");
final EditText input = new EditText(getContext());
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
input.setLayoutParams(lp);
input.setTextColor(Color.parseColor("#f06292"));
alertDialog.setView(input);
alertDialog.setPositiveButton("Add", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
String getSubjectInput = input.getText().toString();
storedList.add(getSubjectInput);
tinyDB.putListString("storedList", storedList);
arrayAdapter.notifyDataSetChanged();
}
});
alertDialog.setNegativeButton("CANCEL",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to execute after dialog
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.create();
alertDialog.show();
}
});
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(final AdapterView<?> adapterView, View view, final int i, long l) {
new SweetAlertDialog(getContext(), SweetAlertDialog.WARNING_TYPE)
.setTitleText("Delete")
.setContentText("Are you sure you want to delete this course")
.setConfirmText("YES")
.setCancelText("NO")
.setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
#Override
public void onClick(SweetAlertDialog sweetAlertDialog) {
storedList.remove(i);
arrayAdapter.notifyDataSetChanged();
tinyDB.registerOnSharedPreferenceChangeListener(new SharedPreferences.OnSharedPreferenceChangeListener() {
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) {
arrayAdapter.notifyDataSetChanged();
}
});
sweetAlertDialog.dismiss();
}
})
.setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {
#Override
public void onClick(SweetAlertDialog sweetAlertDialog) {
sweetAlertDialog.dismiss();
}
})
.show();
return true;
}
});
arrayAdapter = new CustomListAdapter(getContext(),storedList);
listView.setAdapter(arrayAdapter);
arrayAdapter.notifyDataSetChanged();
return rootView;
}
Use this when the user deletes it
public void removeAt(int position) {
mDataset.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, mDataSet.size());
}
mDataset is the array/list in which you save all the data for the adapter

how to test MultiChoiceItems android dialog box

I build an AlertDialog box with
public class ConstantDialogFragment extends DialogFragment {
private AlertDialog.Builder builder;
private AlertDialog alertDialog;
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final int[] constantProtocol = {0};
builder = new AlertDialog.Builder(getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
builder.setTitle(getResources().getString(R.string.some_message))
.setMultiChoiceItems(R.array.some_choice, null,
new DialogInterface.OnMultiChoiceClickListener() {
#Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
// some method
}
})
.setPositiveButton(getResources().getString(R.string.ok_dialog), new DialogInterface.OnClickListener() {
class LoadConfigTask {
private ProgressDialog dialog;
private Activity activity;
public LoadConfigTask(Activity activity) {
this.activity = activity;
dialog = new ProgressDialog(activity, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
}
public void execute() {
try {
// some method
} catch (Exception e) {
// some method
}
}
}
#Override
public void onClick(DialogInterface dialog, int id) {
// some method
}
})
.setNegativeButton(getResources().getString(R.string.cancel_dialog), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
// some thing
}
});
builder.create();
alertDialog = builder.show();
return alertDialog;
}
public AlertDialog getAlertDialog(){
return alertDialog;
}
Then I tried this espresso implementation:
onView(withText("my first choice")).
perform(click());
and I get
NoMatchingViewException: No views in hierarchy found matching: with text: is my string"
How can I fix that ?
Fetching in the Android source code, I found useful methods. So to get control on the items of the AlertDialog, I had to do that
ListView listView = alertDialog.getListView();
View child = listView.getChildAt(0);
child.performClick();

Getting indexOutOfBoundsException while trying to fetch data from parse

I am trying to fetch some data from parse, but my app is getting crashed every time due to this error: java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0 at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) at java.util.ArrayList.get(ArrayList.java:308) at com.abc.xyz.AcceptARequest$1$1.done(AcceptARequest.java:157)
Here's AcceptARequest.java(in which I'm fetching data from parse) file's code :
public class AcceptARequest extends Fragment{
private OnFragmentInteractionListener mListener;
public List<ListContentAAR> listContentAARs;
public RecyclerView recyclerView;
ImageView hPicAccept;
TextView hDescriptionAccept, currentCoordinatesTagAccept, currentLatAccept, currentLngAccept, post_date, post_time, posted_by;
String hDescriptionAcceptS, currentCoordinatesTagAcceptS, currentLatAcceptS, currentLngAcceptS, post_dateS, post_timeS, posted_byS;
Button btn_accept, btn_share;
LinearLayout latContainerAccept, lngContainerAccept, dateTimeContainer;
ParseQuery<ParseObject> query;
String hDescription, cLat, cLng;
public AcceptARequest() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_accept_a_request, container, false);
listContentAARs = new ArrayList<>();
hPicAccept = (ImageView) rootView.findViewById(R.id.h_pic_accept);
hDescriptionAccept = (TextView) rootView.findViewById(R.id.h_description_accept);
currentCoordinatesTagAccept = (TextView) rootView.findViewById(R.id.currentCoordinatesTagAccept);
currentLatAccept = (TextView) rootView.findViewById(R.id.currentLatAccept);
currentLngAccept = (TextView) rootView.findViewById(R.id.currentLngAccept);
btn_accept = (Button) rootView.findViewById(R.id.btn_accept);
btn_share = (Button) rootView.findViewById(R.id.btn_share);
latContainerAccept = (LinearLayout) rootView.findViewById(R.id.latContainerAccept);
lngContainerAccept = (LinearLayout) rootView.findViewById(R.id.lngContainerAccept);
dateTimeContainer = (LinearLayout) rootView.findViewById(R.id.date_time_container);
post_date = (TextView) rootView.findViewById(R.id.post_date);
post_time = (TextView) rootView.findViewById(R.id.post_time);
posted_by = (TextView) rootView.findViewById(R.id.posted_by);
hDescriptionAcceptS = hDescriptionAccept.getText().toString();
currentCoordinatesTagAcceptS = currentCoordinatesTagAccept.getText().toString();
currentLatAcceptS = currentLatAccept.getText().toString();
currentLngAcceptS = currentLngAccept.getText().toString();
post_dateS = post_date.getText().toString();
post_timeS = post_time.getText().toString();
posted_byS = posted_by.getText().toString();
currentCoordinatesTagAccept.setVisibility(View.INVISIBLE);
currentLatAccept.setVisibility(View.INVISIBLE);
currentLngAccept.setVisibility(View.INVISIBLE);
latContainerAccept.setVisibility(View.INVISIBLE);
lngContainerAccept.setVisibility(View.INVISIBLE);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
query = ParseQuery.getQuery("HomelessDetails");
query.whereEqualTo("objectId", ParseUser.getCurrentUser().getObjectId());
query.addDescendingOrder("createdAt");
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> list, ParseException e) {
if (e == null) {
currentCoordinatesTagAccept.setVisibility(View.VISIBLE);
currentLatAccept.setVisibility(View.VISIBLE);
currentLngAccept.setVisibility(View.VISIBLE);
latContainerAccept.setVisibility(View.VISIBLE);
lngContainerAccept.setVisibility(View.VISIBLE);
hDescriptionAcceptS = list.get(1).getString("hDescription");
hDescriptionAccept.setText(hDescriptionAcceptS);
currentLatAcceptS = list.get(1).getString("hLatitude");
currentLatAccept.setText(currentLatAcceptS);
currentLngAcceptS = list.get(1).getString("hLongitude");
currentLngAccept.setText(currentLngAcceptS);
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(e.getMessage());
builder.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
}
});
}
}, 1000);
recyclerView = (RecyclerView) rootView.findViewById(R.id.accept_request_list);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setHasFixedSize(true);
recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getActivity(), recyclerView, new ClickListener() {
#Override
public void onClick(View view, int position) {
Button btnAccept = (Button) view.findViewById(R.id.btn_accept);
btnAccept.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.request_accepted_txt);
builder.setPositiveButton("Navigate me", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getActivity(), "Navigating you to the needy...", Toast.LENGTH_LONG).show();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getActivity(), "The help-request has been rejected.", Toast.LENGTH_LONG).show();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
});
Button btnShare = (Button) view.findViewById(R.id.btn_share);
btnShare.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// share the help-request here.
}
});
}
#Override
public void onLongClick(View view, int position) {
}
}));
initializeAdapter();
return rootView;
}
private void initializeAdapter(){
RVAdapterAAR adapter = new RVAdapterAAR(listContentAARs);
recyclerView.setAdapter(adapter);
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
public void itemClicked(View view, int position) {
}
class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {
private GestureDetector gestureDetector;
private ClickListener clickListener;
public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final ClickListener clickListener) {
this.clickListener = clickListener;
gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener(){
#Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
#Override
public void onLongPress(MotionEvent e) {
View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
if (child != null && clickListener != null) {
clickListener.onLongClick(child, recyclerView.getChildPosition(child));
}
super.onLongPress(e);
}
});
}
#Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
View child = rv.findChildViewUnder(e.getX(), e.getY());
if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
clickListener.onClick(child, rv.getChildPosition(child));
}
return false;
}
#Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
#Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}
public static interface ClickListener {
public void onClick(View view, int position);
public void onLongClick(View view, int position);
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p/>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
I don't know what's going wrong here.
Please let me know.
Please cooperate if question seems to be badly formatted, I'm still learning to post good questions.
You are trying to access the ArrayList which does not have any element in it. SO list.get(1) won't work.
Make sure that your ArrayList is getting populated correctly.
#Hammad:
The error that you are getting is because of the array list that you are using does not contain any elements/objects in it.
You need to populate the arraylist using list.Add()(or based on your preference) method.

Categories

Resources