I'm developing an application which contains a listview with some article but I have confusion with using of notifyDataSetChanged().
My confusion derived from insert operation because I want add a new item to listview from dialog. The "problem" is on add button click of dialog: I create a new Product item and add to productsList then call notifyDataSetChanged() to refresh listview, but I could not call it because my listview is updated anyway and I not understand if it is correct or not also because if I not use dialog to add, without notify method listview doesn't refresh.
I did some research and all of them use notify method.
Below the code that I use in my Fragment:
productsList = new ArrayList<>();
mAdapter = new products_list_adapter(getActivity(), productsList);
asyncProducts = new GetProductsAsyncTasks(getActivity(), productsList, listViewProducts);
asyncProducts.execute();
final Dialog dialog = new Dialog(getActivity());
btnAggiungiProdotto.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.setTitle("Add");
dialog.setContentView(R.layout.input_dialog);
dialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, 1000);
dialog.setCancelable(true);
Button btnConferma = (Button) dialog.findViewById(R.id.btnConferma);
btnConferma.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText txtDescr = (EditText) dialog.findViewById(R.id.edtDescrizione);
EditText txtBarcode = (EditText) dialog.findViewById(R.id.edtBarocode);
Product p = new Product();
p.SetDescription(txtDescr.getText().toString());
p.SetBarCode(txtBarcode.getText().toString());
productsList.add(p);
mAdapter.notifyDataSetChanged();
dialog.dismiss();
}
});
I would know this behaviour is correct or not.
Related
The recyclerView is used to display a custom ArrayList of products and this view can be switched between different types of products by the user via radio buttons that call swapAdapter. The products are all filtered by diameter via spinner bar selection, and since the user will want to get products of matching diameter, the filter must hold as the adapters swap. All of this is working in the view, however when the user selects an item to add to the cart, the cart will show the wrong item. It will be from the same position however, from the previous adapter. As these adapters switch so does the last adapter. Why are the views refreshing but the data linked to the position is always one and only one step behind?
Here is the RecyclerView
mRecyclerView = findViewById(R.id.inventory_recycler_view);
mLayoutManager = new LinearLayoutManager(this);
aAdapter = new CustomAdapter(aList);
bAdapter = new CustomAdapter(bList);
cAdapter = new CustomAdapter(cList);
dAdapter = new CustomAdapter(dList);
eAdapter = new CustomAdapter(eList);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(aAdapter);
and here is the radio group
bButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mRecyclerView.swapAdapter(bAdapter, true);
bAdapter.getFilter().filter(filterDiameterString);
// bAdapter.notifyDataSetChanged();
}
});
cButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mRecyclerView.swapAdapter(cAdapter, true);
cAdapter.getFilter().filter(filterDiameterString);
// cAdapter.notifyDataSetChanged();
}
});
dButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mRecyclerView.swapAdapter(dAdapter, true);
dAdapter.getFilter().filter(filterDiameterString);
// dAdapter.notifyDataSetChanged();
}
});
eButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mRecyclerView.swapAdapter(miscAdapter, true);
eAdapter.getFilter().filter(filterDiameterString);
// eAdapter.notifyDataSetChanged();
}
});
The following has been attempted:
notifyDataSetChange
recyclerview.invalidate
adapter.clear()
I was able to resolve this issue by adding a setter to my arraylist in the custom adapter and then changed the list via the setter then called notify data set change.
What am I doing wrong? I cannot get my data to transfer from where I click it on the list to the detailed view activity that the click opens.
Here is my code at the button click... and the data is available here due to the fact that the list shows the items.
RecipeRepo repo = new RecipeRepo(this);
final ArrayList<HashMap<String, String>> recipeList = repo.getRecipeList();
if(recipeList.size()!=0) {
ListView lv = (ListView) findViewById(R.id.list);//getListView();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
recipe_Id = (TextView) view.findViewById(R.id.recipe_Id);
String recipeId = recipe_Id.getText().toString();
Intent objIndent = new Intent(getApplicationContext(),RecipeDetail.class);
objIndent.putExtra("recipe_Id", Integer.parseInt( recipeId));
startActivity(objIndent);
}
});
ListAdapter adapter = new SimpleAdapter( SousChef.this,recipeList, R.layout.view_recipe_entry, new String[] { "id","name"}, new int[] {R.id.recipe_Id, R.id.recipe_list_name});
lv.setAdapter(adapter);
}else {
Toast.makeText(this, "No recipe!", Toast.LENGTH_SHORT).show();
}
Like I said, I'm the data is available at this point because the listview shows the ID and the Name of each item.
After I click it using the following code... the activity is blank, and the toast in the item below comes up ID 0
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recipe_detail);
btnEdit = (Button) findViewById(R.id.detail_edit);
btnClose = (Button) findViewById(R.id.detail_close);
textName = (EditText) findViewById(R.id.detail_recipe_name);
textIngredients = (EditText) findViewById(R.id.detail_recipe_ingredients);
textInstruct = (EditText) findViewById(R.id.detail_recipe_instruct);
textCookTemp = (EditText) findViewById(R.id.detail_cook_temp);
textCookTime = (EditText) findViewById(R.id.detail_recipe_cooktime);
textServes = (EditText) findViewById(R.id.detail_recipe_servings);
btnEdit.setOnClickListener(this);
btnClose.setOnClickListener(this);
_Recipe_Id =0;
Intent intent = getIntent();
_Recipe_Id = intent.getIntExtra("recipe_Id", 0);
RecipeRepo repo = new RecipeRepo(this);
Recipe recipe = new Recipe();
recipe = repo.getRecipeById(_Recipe_Id);
textName.setText(recipe.name);
Toast.makeText(this, "Recipe id is " + recipe.recipe_Id, Toast.LENGTH_SHORT).show();
textIngredients.setText(recipe.ingredients);
textInstruct.setText(recipe.instructions);
textCookTemp.setText(recipe.cooktemp);
textCookTime.setText(recipe.cooktime);
textServes.setText(recipe.serves);
From everything I have read this should be working but I must be leaving something out. Also I am not producing any errors in the logCat or anything.
Word of advise... always make sure you mark the correct columns in your database... A simple mistake can cause you 2 days worth of work to go out the window. In this case I had the getRecipeByID code looking for the column category instead of the column recipe_Id. simple fix but could have been avoided.
I want to store selected checkbox values in ArrayList. There is five checkbox if I select three then they will store on ArrayList. I used String []ad = new String[5]; is it write on not to store the value of checkbox
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
List<String> mList = new ArrayList<>();
CheckBox android, java, python, php, unity3D;
Button submitButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
android = (CheckBox) findViewById(R.id.androidCheckBox);
android.setOnClickListener(this);
java = (CheckBox) findViewById(R.id.javaCheckBox);
java.setOnClickListener(this);
python = (CheckBox) findViewById(R.id.pythonCheckBox);
python.setOnClickListener(this);
php = (CheckBox) findViewById(R.id.phpCheckBox);
php.setOnClickListener(this);
unity3D = (CheckBox) findViewById(R.id.unityCheckBox);
unity3D.setOnClickListener(this);
submitButton = (Button) findViewById(R.id.submitButton);
submitButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.e("ArrayList Values*******",mList.toString());
}
});
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.androidCheckBox:
if (android.isChecked()) {
mList.add(String.valueOf(android.getText()));
Log.e("Android*******",mList.toString());
}
break;
case R.id.javaCheckBox:
if (java.isChecked()) {
mList.add(String.valueOf(java.getText()));
Log.e("Java*******",mList.toString());
}
break;
case R.id.phpCheckBox:
if (php.isChecked()) {
mList.add(String.valueOf(php.getText()));
Log.e("PHP*******",mList.toString());
}
break;
case R.id.pythonCheckBox:
if (python.isChecked()){
mList.add(String.valueOf(python.getText()));
Log.e("Python*******",mList.toString());
}
break;
case R.id.unityCheckBox:
if (unity3D.isChecked()){
mList.add(String.valueOf(unity3D.getText()));
Log.e("Unity*******",mList.toString());
}
break;
}
}
}
Just create a List and add values when your click events are fired:
final List<String> mList = new ArrayList<>();
mList.add("Your value");
Note: consider to implement onCheckChangeListener intead of onClickListener to handle your checkbox selection events.
No, it's not quite correct.
I strongly recommend creating the array when the user presses submitButton. Otherwise, if they check some boxes, and either
Rotate the screen, or
Put the app in the background and the Activity gets destroyed by the system (You can simulate this by selecting the "Don't keep Activities" option in Developer Options)
When they see your UI again, it will be correctly re-created - all the boxes the user has checked will still be checked, but your array will be empty! I recommend something like
submitButton = (Button) findViewById(R.id.submitButton);
submitButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String []ad = new String[5];
if (android.isChecked()) {
ad[0] = (String) android.getText();
}
if (java.isChecked()) {
ad[1] = (String) java.getText();
}
...
}
});
If you care about ad outside the context of submitting the user's choices, the best practice is to save it in the Bundle in public void onSaveInstanceState(Bundle outState) {}
and fetch and set it in your onCreate(Bundle savedInstanceState){}. This way you will not loose data even on orientation change, or on the system destroying your Activity. See this answer for details on how to do that.
I have a BottomSheet "buttomSheet" (in this sheet it is a list view that is GONE) and a normal Button "btnShowListView" (out from the "buttomSheet").
I want that when I click on "btnShowListView" the list view in the bottomSheet will be Visible its work but only after 2 clicks on button "btnShowListView"...
this is my code:
final View bottomSheetView = findViewById(R.id.bottomSheetLayout);
listView = (ListView) bottomSheet.findViewById(R.id.listView);
buttomSheet = BottomSheetBehavior.from(bottomSheetView);
btnShowListView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listView.setVisibility(View.VISIBLE);
bottomSheetView.requestLayout();
bottomSheetView.invalidate();
bottomSheet.setState(BottomSheetBehavior.STATE_EXPANDED);
}
});
You can make the listview already visible, there's no need to hide it; you can get te same effect by setting the state of the bottom sheet behavior to HIDDEN.
I tested the code below and it should work also for you.
final View bottomSheetView = findViewById(R.id.bottomSheetLayout);
bottomSheetBehavior= BottomSheetBehavior.from(bottomSheetView);
bottomSheetBehavior.setHideable(true);
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
btnShowListView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
bottomSheet.setState(BottomSheetBehavior.STATE_EXPANDED);
}
});
My problem is I have a button and that button is doing create new textview but that textviews removing when i click back button. How I saved textviews in activity?
My java sourcecodes here
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notlar);
Button btnNotEkle = (Button) findViewById(R.id.btnNotEkle);
final EditText etNot = new EditText(NotEkle.this);
final LinearLayout layoutNotlar = (LinearLayout) findViewById(R.id.layoutNotlar);
final TextView tv1 = (TextView) findViewById(R.id.tvnotOrtalama);
etNot.setInputType(InputType.TYPE_CLASS_NUMBER);
AlertDialog.Builder notEkle = new AlertDialog.Builder(NotEkle.this);
notEkle.setTitle("Notunuz");
notEkle.setView(etNot);
//Positive button
notEkle.setPositiveButton("Tamam", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
tvNot = new TextView(NotEkle.this);//girelen not burdaki textview e yazdırılacak.
girilenNot = etNot.getText().toString();//Girilen notu alıyoruz
tvNot.setText(girilenNot);//girilen notu textviewa veriyoruz
notTopla += Integer.parseInt(girilenNot);//Notları topluyoruz
layoutNotlar.addView(tvNot);
count = layoutNotlar.getChildCount();
dersOrtalamaYazdir=String.valueOf(dersOrtalama());
tv1.setText("Ders Ortalamanız : "+dersOrtalamaYazdir);
dialog.cancel();
}
});
final AlertDialog notEkleCreate = notEkle.create();
btnNotEkle.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
notEkleCreate.show();
}
});
}
}
Try giving your TextView objects ids.
You need to know that when you click back button - by default your activity is destroyed so all views are removed.
When you are adding new TextView you should add information about this TextView (like the text itself) to some list declared as field in your activity.
Then you can save this list when activity is recreated see: onSaveInstanceState/nRestoreInstanceState
You can also pass this list back or to new activity so that they can take actions based on this list.
Following my understanding your TextView had been created inside Dialog and after you press back button the dialog dismisses and all views you created inside will be removed and you can't access it from your Activity.
You may try to create TextView in onCreate, pass and in Dialog just call setText. I hope this is the answer you're looking for.
Cheers.