Actually I had a list view when I touch on the list view I will get popup window with check boxes when I check the check box and press OK then the dialog box closes and when I touch again the list view then the checked row position will change the background color and and stored into the data base. actually what was the issue is when I again select different item position and checked and press OK and again touch the list view instead of entering the checked position entering into data base the previously checked position also entering into data base along with current checked position. so I need to get previous position and clear position before the list view sees the checked positions. so that it only enters current position.
My activity
listView1.setOnTouchListener(new AdapterView.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
newListitems2.clear();
newListitems2.addAll(itemsList1);
dialog = new Dialog(PendingOrdersActitvity.this);
dialog.setContentView(R.layout.itembumping);
dialog.show();
//listView1.setTag(position);
list1 = (ListView) dialog.findViewById(R.id.list1);
ItemBumpingAdapter adapter2 = new ItemBumpingAdapter(PendingOrdersActitvity.this, newListitems2);
list1.setAdapter(adapter2);
Button okButton = (Button) dialog.findViewById(R.id.ok1);
okButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
Button cancelButton = (Button) dialog.findViewById(R.id.Cancel1);
cancelButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
}
return true;
}
});
Implimented code :
listView1.setOnTouchListener(new AdapterView.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP){
newListitems2.clear();
newListitems2.addAll(itemsList1);
dialog = new Dialog(PendingOrdersActitvity.this);
dialog.setContentView(R.layout.itembumping);
dialog.show();
list1=(ListView )dialog.findViewById(R.id.list1);
ItemBumpingAdapter adapter2 = new ItemBumpingAdapter(PendingOrdersActitvity.this,newListitems2);
list1.setAdapter(adapter2);
Button okButton = (Button)dialog.findViewById(R.id.ok1);
okButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
Connection con1 = DbHandler.dbConnection();
try{
PreparedStatement stmt1 = con1
.prepareStatement("Select Line_No, ItemName,DeleteFlag from PendingOrders_Dtl where Inv_No=? ");
stmt1.setString(1,invNo);
ResultSet rsSetup1 = stmt1.executeQuery();
if (rsSetup1.next()) {
ItemsBean bean1 = new ItemsBean();
bean1.setLinenum(rsSetup1.getInt("Line_No"));
bean1.setProdnum(rsSetup1.getInt("ItemName"));
bean1.setDeleteFlag(rsSetup1.getInt("DeleteFlag"));
disablelist.add(bean1);
}
CustomAdapter adapter1 = new CustomAdapter(PendingOrdersActitvity.this, itemsList1);
for(int i=0;i<itemsList1.size();i++)
{
for(int j=0;j<disablelist.size();j++)
{
if(itemsList1.get(i).getProdnum()==disablelist.get(j).getProdnum())
{
itemsList1.get(i).setSelection(true);
}
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
});
try something like this
int previousPosition;
public int getPreviousPosition() {
return previousPosition;
}
public void setPreviousPosition(int previousPosition) {
this.previousPosition = previousPosition;
}
and in your listview on item selected listener
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int prePos= getPreviousPosition();
// do what ever you want to do with this prePos
View viewchild= parent.getChildAt(getPreviousPosition());
CheckBox prevCheckBox=(CheckBox)viewchild.findViewById(R.id.Checkbox_sort_list);
prevCheckBox.setChecked(false);
//at ending of list view write this
setPreviousPosition(position);
}
});
I think you have kept the checked positions in itemList1, so after you copy it to newListitems2, you need to clear the checked positions in newListitems2.
Try this:
listView1.setOnTouchListener(new AdapterView.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
newListitems2.clear();
newListitems2.addAll(itemsList1);
dialog = new Dialog(PendingOrdersActitvity.this);
dialog.setContentView(R.layout.itembumping);
dialog.show();
//listView1.setTag(position);
list1 = (ListView) dialog.findViewById(R.id.list1);
// Uncheck all items of newListitems2
for(int i=0; i<newListitems2.size(); i++){
ItemsBean tmpItem = newListitems2.get(i);
tmpItem.setChecked(false);
newListitems2.remove(i);
newListitems2.add(i, tmpItem);
}
ItemBumpingAdapter adapter2 = new ItemBumpingAdapter(PendingOrdersActitvity.this, newListitems2);
list1.setAdapter(adapter2);
Button okButton = (Button) dialog.findViewById(R.id.ok1);
okButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
Button cancelButton = (Button) dialog.findViewById(R.id.Cancel1);
cancelButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
}
return true;
}
});
For the other issue:
Method 1:
If the list in the popup dialog is the same as main list, then may be try: when dialog is dismissed, get the list from popup dialog and copy it to main list.
Add method in Adapter class to get the list from adapter2:
public ArrayList<ItemsBean> getItemList(){
return newListitems;
}
When okbutton clicked, over-write the main list and dismiss the dialog:
itemsList1 = adapter2.getItemList();
adapter1 = new ItemBumpingAdapter(PendingOrdersActitvity.this, itemList1);
adapter1.notifyDataSetChanged();
dialog.dismiss();
Method 2:
Add method to adapter1 to check items:
public void checkItemsAccordingDatabase(){
for(int i=0; i<newListItems; i++){
ItemsBean tmpItem = newListItems.get(i);
// query tmpItem in database.
// if tmpItem exits, newListItems.get(i).setChecked(true).
}
notifyDataSetChanged();
}
When okbutton clicked, update the main list and dismiss the dialog:
adapter1.checkItemsAccordingDatabase();
dialog.dismiss();
Try this:
// Move this after setSelection.
// CustomAdapter adapter1 = new CustomAdapter(PendingOrdersActitvity.this, itemsList1);
for(int i=0;i<itemsList1.size();i++){
for(int j=0;j<disablelist.size();j++){
if(itemsList1.get(i).getProdnum()==disablelist.get(j).getProdnum()){
itemsList1.get(i).setSelection(true);
break;
}
}
}
CustomAdapter adapter1 = new CustomAdapter(PendingOrdersActitvity.this, itemsList1);
listView1.setAdapter(adapter1);
And modify getView() of CustomAdapter similar to your ItemBumpingAdapter:
if(newListItems.isSelected()){
convertView.setBackgroundColor(...);
else{
convertView.setBackgroundColor(...);
}
retrun convertView;
Related
how can get spinner values when I used if else then pass value to setonClick?
in my code, I used this methods to set spinner values
DatabaseReference chk_sub = FirebaseDatabase.getInstance()......
chk_sub.addValueEventListener(new ValueEventListener() {
if(xxxxxx){
List<String> sub = new ArrayList<>();
sub.add(0, getApplicationContext().getResources().getString(R.string.choose_sub));
sub.add("USA");
sub.add("CA");
ArrayAdapter<String> dataSpinnerAdapter;
dataSpinnerAdapter = new ArrayAdapter(getApplication(), android.R.layout.simple_spinner_item, sub);
dataSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataSpinnerAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if(parent.getItemAtPosition(position).equals(getApplicationContext().getResources().getString(R.string.choose_sub))){
//do nothing
} else {
spinner_item = parent.getItemAtPosition(position).toString();
//Toast.makeText(parent.getContext(),"Selected : "+item, Toast.LENGTH_SHORT).show();
}
}
}
and I hope I can get spinner sub value because I need to judge
post.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if( -- how can get spinner value in here? --){
//
}else {
Toast.makeText(getApplication(), getApplicationContext().getResources().getString(R.string.must_upload), Toast.LENGTH_SHORT).show();
}
}
});
If you just want selected spinner item to be used in if(condition=spinner value) on click event of post i.e., post.onClick(), just get the spinner selected item
your code will look like this
post.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(spinner.getSelectedItem().equals("your compare value")){
// do whatever you want
}else {
Toast.makeText(getApplication(), getApplicationContext().getResources().getString(R.string.must_upload), Toast.LENGTH_SHORT).show();
}
}
});
I have a listview with a series of elements.
The click on an item of listview shows me a custom dialog.
In custom dialog I have a layout with:
A spinner
Two buttons (OK / ANNULLA)
This is the normal situation:
When I select the spinner, he shows a list of items.
When I select an item from the spinner, the text that was present on the buttons disappear in this way:
ps: this does not happen on Android 6.0, but it happens in the lower versions (such as 5.0)
The Code:
public void showDialogTagAssociation (Activity activity, Handler handler,
String msg, final MyOperator elemento, final BluetoothDevice device,
final int position){
mHandler = handler;
//-----------------------------------------------------
// DIALOG
dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
//-----------------------------------------------------
//---------------------------------------------------------------------
// LAYOUT
dialog.setContentView(R.layout.alert_dialog_custom_tag);
**// Spinner element
spinner = (Spinner) dialog.findViewById(R.id.spinner);**
// Spinner click listener
**spinner.setOnItemSelectedListener(new OnSpinnerItemClicked());**
//---------------------------------------------------------------------
//----------------------------------------------------------------------
// BUTTON OK
dialogButtonOK = (Button) dialog.findViewById(R.id.acd_btn_ok);
dialogButtonOK.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "Devo assegnare il tag al nome....");
Log.d(TAG, "Nome: " +tmpNome+"\n" +
"TAG: "+device.getName()+" - "+device.getAddress());
dialog.dismiss();
}
});
// BUTTON ANNULLA
dialogButtonNO = (Button) dialog.findViewById(R.id.acd_btn_no);
dialogButtonNO.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
//----------------------------------------------------------------------
dialog.show();
}
**private class OnSpinnerItemClicked implements android.widget.AdapterView.OnItemSelectedListener {**
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
//NOME
tmpNome = parent.getItemAtPosition(pos).toString();
}
#Override
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
Thanks for the future help
Remove that piece of code:
spinner.setOnItemSelectedListener(new OnSpinnerItemClicked());
and the thing which you want will still work.
I have a listview with items. I have developed onitemclick() function with a custom dialog to enter a value. I need to update the listview current item with value we enter in the dialog box.
i have done like this
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
showCustomDialog(view);
if(quantity>0){
TextView tv=(TextView)view.findViewById(R.id.row_item_quantity);
tv.setTextColor(R.color.white);
tv.setText(quantity+"");
quantity=0;
Toast.makeText(getActivity(), "entered "+quantity , Toast.LENGTH_SHORT).show();
}
}
showCustomDialog function is
protected void showCustomDialog(View view) {
// TODO Auto-generated method stub
final Dialog dialog;
dialog = new Dialog(getActivity());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.alert_enter_quantity);
dialog.setCancelable(true);
final EditText editText;
editText = (EditText)dialog.findViewById(R.id.quantity_number);
Button buttonCancel;
buttonCancel = (Button)dialog.findViewById(R.id.quantity_cancel_button);
buttonCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
Button buttonOK;
buttonOK = (Button)dialog.findViewById(R.id.quantity_ok_button);
buttonOK.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Toast.makeText(getActivity(), "entered"+editText.getText().toString() , Toast.LENGTH_SHORT).show();
String text=editText.getText().toString();
if(text=="")
text=""+0;
quantity =Integer.parseInt(text);
//setQuantity(qty);
dialog.dismiss();
}
});
dialog.show();
}
It gets the value and will update on the next item i clicks. How can i correct this problem.
use
tv.setText(String.valueOf(quantity);
instead of
tv.setText(quantity+"");
if you are using array for list data you have to update the list in exact position.then call adapter.notifiDataChanged().If u want to change it temporally use view.invalidate();
I have done it.
As below.
write a custom dialog box with an edittext and two buttons for OK and Cancel.
buttonOK = (Button)dialog.findViewById(R.id.quantity_ok_button);
buttonOK.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String text;
text=editText.getText().toString();
quantity=text;
//Toast.makeText(getActivity(), "entered"+editText.getText().toString() , Toast.LENGTH_SHORT).show();
setQuantity(quantity,position);
dialog.dismiss();
}
});
void setQuantity(String quantity,int position){
if(!selectedItemsMenu.contains(data))
// lines ///
selectedItemsMenu.add(data);
listMenu.add(position, data);
Log.e("new list size", selectedItemsMenu.size() + "");
adapter.notifyDataSetChanged();
I am having problem on back button of hardware. In my main Activity, I have one List view(say 1). When I click on item of this List view(1), one Alert Dialog appears, in this alert dialog, there is one List view(say 2). Data of this List view(2) are being repeated when I press back button of hardware. I have also put cancel image on this alert dialog to dismiss, when I press this cancel image, data are not being repeated. I tried different methods onResume(), onPause(), onDestroy(), onRestart() to clear the array for List view(2), but nothing works. Here is my code...
case LIST_DIALOG :
LayoutInflater inflater2 = LayoutInflater.from(this);
View dialogview1 = inflater2.inflate(R.layout.listdialog, null);
AlertDialog.Builder dialogbuilder2 = new AlertDialog.Builder(this);
dialogbuilder2.setView(dialogview1);
dialogDetails = dialogbuilder2.create();
case LIST_DIALOG:
AlertDialog alertDialog1 = (AlertDialog) dialog;
// Cancel Alert Dialog
ImageView ivCancel = (ImageView) alertDialog1.findViewById(R.id.imgCancel);
ivCancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
dismissDialog(LIST_DIALOG);
arr2.clear();
}
});
// Friend List
showFriendList();
break;
// List view data are inserted with this function call
private void showFriendList() {
// TODO Auto-generated method stub
Request.executeMyFriendsRequestAsync(friendSession, new GraphUserListCallback() {
#Override
public void onCompleted(List<GraphUser> users, Response response) {
// TODO Auto-generated method stub
// arr2 = new ArrayList<String>();
for(GraphUser user : users)
{
arr2.add(user.getName());
}
adapter2 = new ArrayAdapter<String>(getBaseContext(), R.layout.single_row, R.id.txt,arr2);
lvDialog.setAdapter(adapter2);
lvDialog.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
adapter2.notifyDataSetChanged();
itemCount = lvDialog.getCount();
Toast.makeText(getBaseContext(), "" + itemCount, 1000).show();
}
});
}
// I tried these methods, but nothing works...
#Override
public void onResume()
{
super.onResume();
ShowSavedFiles();
arr2.clear();
}
#Override
public void onPause()
{
super.onPause();
arr1.clear();
arr2.clear();
}
#Override
public void onBackPressed() {
//super.onBackPressed();
// finish your Activity
arr2.clear();
return;
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
arr2.clear();
dismissDialog(LIST_DIALOG);
}
return false;
}
I'm not sure but might be adding the following code to your onKeyDown method might help out :
return super.onKeyDown(keyCode, event);
I am trying to use a spinner control that will enable the user to delete any list element.
I have an 'add' button to add elements to the list, and a 'delete' button that removes the currently-displayed item from the list.
It works as expected except when the user deletes the last item in the list. At that point, all of the list's items are deleted.
My code is as follows:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// grab our UI elements so we can manipulate them (for the Spinner)
// or add listeners to them (in the case of the buttons)
m_myDynamicSpinner = (Spinner)findViewById(R.id.dynamicSpinner);
m_addItemText = (EditText)findViewById(R.id.newSpinnerItemText);
Button addButton = (Button)findViewById(R.id.AddBtn);
Button clearButton = (Button)findViewById(R.id.ClearBtn);
// create an arrayAdapter an assign it to the spinner
m_adapterForSpinner = new ArrayAdapter(this, android.R.layout.simple_spinner_item);
((ArrayAdapter)m_adapterForSpinner).setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
m_myDynamicSpinner.setAdapter(m_adapterForSpinner);
// add listener for addButton
addButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
addNewSpinnerItem();
}
});
clearButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
clearSpinnerItems();
}
});
}
// add listener for addButton
private void addNewSpinnerItem() {
if (m_addItemText.getText().length() == 0) {
Toast.makeText(getApplicationContext(), "The textView is empty", Toast.LENGTH_LONG).show();
} else {
CharSequence textHolder = "" + m_addItemText.getText();
((ArrayAdapter) m_adapterForSpinner).add(textHolder);
}
m_addItemText.setText("");
}
private void clearSpinnerItems() {
m_myDynamicSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Object t = m_adapterForSpinner.getItem(pos);
((ArrayAdapter) m_adapterForSpinner).remove((CharSequence) t);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO
}
});
}
Does anyone have any ideas or suggestions on how to make this work?
The problem with your code is that the deletion is inside the onItemSelected callback, which gets called every time you are deleting an entry, thus deleting recursively until you effectively do not have any more entries to select. If you add a log inside that method:
Log.d("Spinner", "Count: " + m_adapterForSpinner.getCount());
you will see what I mean. I'm sure you can come up with more elegant code, but a quick and dirty hack is to set up a boolean flag to stop the recursion after the first deletion. See the snippet below and add the commented lines to your own code:
public class SpinnerTest extends Activity {
Spinner m_myDynamicSpinner;
EditText m_addItemText;
ArrayAdapter m_adapterForSpinner;
public static boolean cleared = false; // <--- set up a static boolean here
#Override
public void onCreate(Bundle savedInstanceState) {
// all your code unchanged
clearButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
cleared=false; // <--- nope, we did not clear the value yet
clearSpinnerItems();
}
});
}
// code unchanged
private void clearSpinnerItems() {
m_myDynamicSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Object t = m_adapterForSpinner.getItem(pos);
Log.d("Spinner", "Count: " + m_adapterForSpinner.getCount());
if (!cleared) // <--- did I do it already?
((ArrayAdapter) m_adapterForSpinner).remove((CharSequence) t);
Log.d("Spinner", "Count: " + m_adapterForSpinner.getCount());
cleared=true; // I did it!
}
// code unchanged
i cann't understand your question.any way you can get the position of the selected item by using getSelectedItemPosition() method.