deleting a row in parse server? - java

the problem is the list item on which it is clicked does not get deleted but an item behind or in front of it gets deleted (for list2.get(d) or list2.get(d-1))how to get the item which is clicked to get deleted?)
Intent intent=getIntent();
k=intent.getStringExtra("this is a");
list=(ListView)findViewById(R.id.listview);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
d=i;
AlertDialog.Builder dialog=new AlertDialog.Builder(Main3Activity.this);
dialog.setMessage("this is a");
dialog.setPositiveButton("yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int l){
ParseQuery<ParseObject> b=new ParseQuery<ParseObject>("message");
b.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, ParseException e)
{
if(e==null) {
for (ParseObject n : objects) {
if (n.getString("message").equals(list2.get(d))&&(n.getString("receiver").equals(ParseUser.getCurrentUser().getUsername().toString())&&n.getString("sender").equals(k)))
{
try {
Toast.makeText(Main3Activity.this,list2.get(d+1), Toast.LENGTH_SHORT).show();
n.delete();
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
}
else
{
Log.i("this is a",e.getMessage());
}
}
});
list2.remove(d);
a.notifyDataSetChanged();

this in this case refers to onItemClickListener context. You can either use application context by calling getApplicationContext() OR if you need local context, use CLASSNAME.this
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
Intent intent=getIntent();
list=(ListView)findViewById(R.id.listview);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
AlertDialog.Builder dialog=new AlertDialog.Builder(getApplicationContext());
}
});
OR
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
Intent intent=getIntent();
list=(ListView)findViewById(R.id.listview);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
AlertDialog.Builder dialog=new AlertDialog.Builder(MainActivity.this);
}
});

Related

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 take a different action for every position in ListView?

i would like to make a different action(maybe Intent) for every item in ListView rather than Toast, Thanks.
this is my java code
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Array of strings...
final String[] mobileArray = {"Android", "IPhone", "WindowsMobile", "Blackberry",
"WebOS", "Ubuntu", "Windows7", "Max OS X"};
final ArrayAdapter adapter = new ArrayAdapter<String>(this,
R.layout.activity_listview, mobileArray);
final ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
Toast.makeText(MainActivity.this, "hello world", Toast.LENGTH_SHORT).show();
}
});
}
}
Use position for doing different things on different lines in your listView.setOnItemClickListener like this
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
switch (position){
case 0:
// your code here
break;
case 1:
// your code
break;
..... so on
}
}
});
You could make a function that recive position as it parameter
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
doSomething(position);
}
public void doSomething(int position){
switch(position)
{
case 1:
//do this
break;
case 2:
//do this
break;
}
}});

How to call method of activity in Fragment?

I have the following listView:
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//CheckBoxAdapter cb = new CheckBoxAdapter(this.getActivity(),R.layout.item, getResources().getStringArray(R.array.products));
//lv.setAdapter(cb);
ArrayAdapter<String> arrayAdapter;
arrayAdapter = new ArrayAdapter<String>(this.getContext(), android.R.layout.simple_list_item_1, categories);
ListView lv = (ListView) this.getActivity().findViewById(R.id.listProduct);
lv.setAdapter(arrayAdapter);
lv.setOnItemClickListener(
new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String type = String.valueOf(parent.getItemAtPosition(position));
Toast.makeText(getContext(), parent.getItemAtPosition(position)+" is selected", Toast.LENGTH_LONG).show();
try {
MainActivity.AddSubscription(String.valueOf(parent.getItemAtPosition(position)));
} catch (TimeoutException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
);
But currently I cannot reference addSubscription which is nonstatic
MainActivity.AddSubscription(String.valueOf(parent.getItemAtPosition(position)));
I need to create a non static listener which can call addsubscription, is it possible with a listview? If not can I use another type of view?
Simply if your fragment is placed in MainActivity do this:
((MainActivity)getActivity()).AddSubscription(String.valueOf(parent.getItemAtPosition(position)));
Do the following in the listener:
YourFragmentName.this.((MainActivity)getActivity()).AddSubscription(String.valueOf(parent.getItemAtPosition(position)));
I would not use getActivity() and cast it as this approach seems to fragile to me.
I typed this out of my head without an IDE so there might be typos.
Let the Activity implement an interface SubscriptionHandler
public interface SubscriptionHandler {
public void addSubscription(String identifier);
}
Actitiy
public MainActivity extends AppCompatActivity implements SubscriptionHandler {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyFragment fragment = (MyFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_id);
fragment.setSubscriptionHandler(this);
}
#Override
public void addSubscription(String identifier){
//logic
}
}
and then use this in the Fragment
public class MyFragment extends Fragment {
#Nullable private SubscriptionHandler subscriptionHandler;
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ArrayAdapter<String> arrayAdapter;
arrayAdapter = new ArrayAdapter<String>(this.getContext(), android.R.layout.simple_list_item_1, categories);
ListView lv = (ListView) this.getActivity().findViewById(R.id.listProduct);
lv.setAdapter(arrayAdapter);
lv.setOnItemClickListener(
new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String type = String.valueOf(parent.getItemAtPosition(position));
Toast.makeText(getContext(), parent.getItemAtPosition(position)+" is selected", Toast.LENGTH_LONG).show();
try {
if(subscriptionManager != null) {
subscriptionHandler.addSubscription(String.valueOf(parent.getItemAtPosition(position)));
}
} catch (TimeoutException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
);
public void setSubscriptionHandler (SubscriptionHandler subscriptionHandler) {
this.subscriptionHandler = subscriptionHandler;
}
}

How to get the spinner value not in integer but in character

public class MainActivity extends AppCompatActivity {
Spinner spinner;
ArrayAdapter<CharSequence> adapter;
int noc = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner =(Spinner)findViewById(R.id.spinner);
String text= spinner.getSelectedItem().toString();
adapter= ArrayAdapter.createFromResource(this,R.array.Coffee_names,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getBaseContext(),parent.getItemIdAtPosition(position) + " is selected",Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
My app stops when i add " String text= spinner.getSelectedItem().toString(); " to my code but it works fine when i remove this line from the program. But when i select a item from dropdown list it says 4 is selected.
I want it to say the name of the item i selected and not the index value.
Please help.
I think it should be:
Toast.makeText(getBaseContext(), parent.getSelectedItem() + " is selected", Toast.LENGTH_SHORT).show();
Alternative way
final String[] coffeeNames = getResources().getStringArray(R.array.Coffee_names);
spinner.setAdapter(new ArrayAdapter<>(activity, android.R.layout.simple_list_item_1, coffeeNames));
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selectedCoffeeName = coffeeNames[position];
Toast.makeText(getBaseContext(), selectedCoffeeName + " is selected", Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});

How to get listview item string?

How to get item where i click in list-view?
Here is my code:
public class MainClass extends Activity {
ArrayList <String> listItems = new ArrayList<String>();
ArrayAdapter <String> adapter;
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems);
listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(adapter);
listItems.add("Item 1");
listItems.add("Item 2");
listItems.add("Item 3");
adapter.notifyDataSetChanged();
listView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),
listView.getItemAtPosition(0).toString(),
Toast.LENGTH_LONG).show();
}
});
}
}
but it force closes everytime.
change:
listView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),
listView.getItemAtPosition(0).toString(),
Toast.LENGTH_LONG).show();
}
});
to:
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Toast.makeText(getApplicationContext(), listView.getItemAtPosition(arg2).toString(), Toast.LENGTH_LONG).show();
}
});
You need to use a onItemClickListener if you want to interact with the items in the list. This has a position argument in the method which you can then use to retrieve the value at that position.
try :
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView parent,
View view, int pos, long id) {
String item=(String)((TextView) view).getText();
Toast.makeText(getApplicationContext(),
listView.getItemAtPosition(0).toString(),
Toast.LENGTH_LONG).show();
}
});

Categories

Resources