Getting value for only single Item in TextView from CartActivity, but want to fetch detail for all Item(s) placed in Cart
Like: I have 4 items in Cart, but once i am trying to show these item details in another activity, so here i am only getting single item detail (only for 4th Item, not for all fours) why?
CODE:
for (int i = 0; i < Session.sItem_Detail.size(); i++) {
String title=Constants.sItem_Detail.get(i).get(
ProductInformationActivity.KEY_TITLE);
String qty=Constants.sItem_Detail.get(i).get(
ProductInformationActivity.KEY_QTY);
String cost=Constants.sItem_Detail.get(i).get(
ProductInformationActivity.KEY_COST);
textDetails =(TextView)findViewById(R.id.txtDetails);
textDetails.setText("(Title:" +title + "(Qty:" + qty + ")" + "(Cost:" + "Rs." + cost + ")");
public class Session {
public static ArrayList<HashMap<String, String>> sItem_Detail = new ArrayList<HashMap<String, String>>();
}
So here is my question, how can i get item details for all items stored in Cart, not just for one..
Try this
use append(charctersequence) instead of setText(charctersequence)
textDetails.append("(Title:" +title + "(Qty:" + qty + ")" + "(Cost:" + "Rs." + cost + ")"+"\n");
Related
I am populating new data in my RecyclerView adapter all at once, so there are no insert or remove one item actions.
So simply, i have an old list and when some Event occurs i get the new list and i can assign the new list to the old.
Problems are i cannot make properly the animation for each item in the old list
when item has new position in the new list (should notifyItemMoved from old position to new)
when there is a new item in the new list (should notifyItemInserted with that position in the new list)
when the old item is not present in the new list (should notifyItemRemoved with that position)
Here is something i have now, which i thought will work for first case - item move to new position:
if(currentAdapterData!= null){
for(int i = 0; i < currentAdapterData.size(); i++){
for(int j = 0; j < newData.size(); j++){
if(currentAdapterData.get(i).getSomeIdentifier().equals(newData.get(j).getSomeIdentifier())){
Log.v("same item", "currentAdapterData index :" + i + " ," + currentAdapterData.get(i).getSomeIdentifier() + " == newData index: " + j + " ," + newData.get(j).getSomeIdentifier());
if(i != j){
notifyItemMoved(i, j);
}
}
}
}
}
currentAdapterData = newData;
However it does not work as expected, and there is difference between logs(which are correct) and the list appearing on the phone(with wrong items positions, some duplicates, buggy etc.)
So how can i make it work? With notifyItemMoved, notifyItemInserted and notifyItemRemoved?
I don't want to just use NofifyDataSetChanged, because it refresh the entire list instead of just updating the items with animations that have changed.
It looks like that your new data is also a form of list, not a single item. I think this could be a good candidate for using DiffUtil in the support library.
Here is also a nice tutorial for it.
It will allow you to calculate the difference in the new data and only update needed fields. It will also offload the work asynchronously.
You just need to implement a DiffUtil.Callback to indicate if your items are the same or the contents are the same.
You update your recyclerView like that:
DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(diffCallback);
diffResult.dispatchUpdatesTo(yourAdapter);
Simply use DiffUtil like
final MyDiffCallback diffCallback = new MyDiffCallback(prevList, newList);
final DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(diffCallback);
Create a Callback by extending MyDiffCallback and override methods and do as needed.
public class MyDiffCallback extends DiffUtil.Callback
// override methods
Well for this ,I feel this would be the easiest.Just follow it ->
Replace this
if(currentAdapterData!= null){
for(int i = 0; i < currentAdapterData.size(); i++){
for(int j = 0; j < newData.size(); j++){
if(currentAdapterData.get(i).getSomeIdentifier().equals(newData.get(j).getSomeIdentifier())){
Log.v("same item", "currentAdapterData index :" + i + " ," + currentAdapterData.get(i).getSomeIdentifier() + " == newData index: " + j + " ," + newData.get(j).getSomeIdentifier());
if(i != j){
notifyItemMoved(i, j);
}
}
}
}
}
currentAdapterData = newData;
with
if(currentAdapterData!= null){
for(int i = 0; i < currentAdapterData.size(); i++){
for(int j = 0; j < newData.size(); j++){
if(currentAdapterData.get(i).getSomeIdentifier().equals(newData.get(j).getSomeIdentifier())){
Log.v("same item", "currentAdapterData index :" + i + " ," + currentAdapterData.get(i).getSomeIdentifier() + " == newData index: " + j + " ," + newData.get(j).getSomeIdentifier());
if(i != j){
notifyDataSetChanged();
new CountDownTimer(250, 250) {
#Override
public void onTick(long millisUntilFinished) {
Log.d("millisUntilFinished", "" + millisUntilFinished);
}
#Override
public void onFinish() {
notifyItemMoved(i, j);
}
}.start();
}
}
}
}
}
this will update the values and after 250 millisecond(1/4th a second),the value with be moved with animation.
I have 4 textviews which take their values from dropdown list (spinner) selected at previous screen.
There can be either 2 or 4 numbers/letters as result of this selection.
The first position will always be a number and the second position will always be a letter. The third position can be a number or blank and the fourth position can be a letter or blank.
If position 3 and position 4 are blank then I need to make them equal to positions 1 & 2 respectively.
String myGrade = intent.getStringExtra("parameter_name_grade");
// above takes value of 'myGrade' from spinner selection at previous screen
String mDisplayGradeNumberEff = (" " + myGrade.charAt(0));
TextView displayGradeNumberEff = (TextView) findViewById(R.id.gradeNumberEffTV);
displayGradeNumberEff.setText(mDisplayGradeNumberEff);
String mDisplayGradeLetterEff = (" " + myGrade.charAt(1));
TextView displayGradeLetterEff = (TextView) findViewById(R.id.gradeLetterEffTV);
displayGradeLetterEff.setText(mDisplayGradeLetterEff);
// above works correctly
// from here down only works when a character is present in both positions
// if positions 3(2) and 4(3) are empty app stops running.
String mDisplayGradeNumberDia = (" " + myGrade.charAt(2));
if (mDisplayGradeNumberDia.isEmpty()) {
mDisplayGradeNumberDia = mDisplayGradeNumberEff;
}
TextView displayGradeNumberDia = (TextView) findViewById(R.id.gradeNumberDiaTV);
displayGradeNumberDia.setText(mDisplayGradeNumberDia);
String mDisplayGradeLetterDia = (" " + myGrade.charAt(3));
if (mDisplayGradeLetterDia.isEmpty()) {
mDisplayGradeLetterDia = mDisplayGradeLetterEff;
}
TextView displayGradeLetterDia = (TextView) findViewById(R.id.gradeLetterDiaTV);
displayGradeLetterDia.setText(mDisplayGradeLetterDia);
}
I Guess you have a array out of bounds exception, please provide Logcat....
Check if "myGrade" has 3/4 Characters, if it does not you can't read them with charAt(3)...
You can check the length of the String with "myGrade.length()"
When I asked this question I was fairly new to the site and didn't understand that I should post back the solution for future reference. Solution below worked so thanks to rocket for your help and sorry for the delay!
int myGradeLength = mGrade.length();
if (myGradeLength != 4) {
mDisplayGradeNumberEff = ("" + mGrade.charAt(0));
mDisplayGradeLetterEff = ("" + mGrade.charAt(1));
mDisplayGradeNumberDia = ("" + mGrade.charAt(0));
mDisplayGradeLetterDia = ("" + mGrade.charAt(1));
} else {
mDisplayGradeNumberEff = ("" + mGrade.charAt(0));
mDisplayGradeLetterEff = ("" + mGrade.charAt(1));
mDisplayGradeNumberDia = ("" + mGrade.charAt(2));
mDisplayGradeLetterDia = ("" + mGrade.charAt(3));
}
how can I add item to list adapter, but every time I iterate n, it will add to a different list and different adapter, thanks!
//my first code
int n = 1;
while (n != 16) {
Day(n, Fullname);
n +=1 ;
}
//the other one that will add to the list and adapter
private void Day(int n,String Fullname){
String Date = "0" + n + "-" + Month + "-" + Year;
Cursor c = db.GetSpecific(Fullname,Date);
String allowancecount = "";
int intallowancecount = 0;
double totalCom = 0;
while(c.moveToNext()){
String serviceprice = c.getString(4);
String serviceperformedbynumber = c.getString(10);
allowancecount = c.getString(12);
intallowancecount = intallowancecount + Integer.parseInt(allowancecount.trim());
double income = Integer.parseInt(serviceprice.trim())/
Integer.parseInt(serviceperformedbynumber.trim());
totalCom = totalCom + income ;
//here what to do?
String add ="List" + n + ".add(String.valueOf(" + income + "));";
do add;//???????
}
//here what to do?
String Dynamic = "Com" + n + ".setText(String.valueOf(" + totalCom + "))";
do Dynamic ;//?????????
String Dynamicadapter = "Lv" + n + ".setAdapter(adapter" + n + ")";
do Dynamicadapter ;//????????
}
i'm just thinking if this was possible, but if not, i'll do it on the other way i know,open for any suggestions, thanks again.
It isn't possible in Java to assign variable using a variable name. However, there are things that you can use for these types of situations which allow you to almost do this, but everything is created correctly at runtime without the need for dynamic behaviour. For example, you can think about this in terms of using an associative array.
Taken from Wikipedia -https://en.wikipedia.org/wiki/Comparison_of_programming_languages_(mapping)#Java
In Java associative arrays are implemented as "maps"; they are part of
the Java collections framework. Since J2SE 5.0 and the introduction of
generics into Java, collections can have a type specified; for
example, an associative array mapping strings to strings might be
specified
So in light of this, you could use a Map and restructure your code to do the following:
Create your adapter object
Set it's properties (such as setAdapter())
Add it to the map by using i (or whatever you want to use as a key for this)
Access it later on from the map using the key/dynamic 'variable name'
Map<Integer, ArrayAdapter<String>> map = new HashMap<Integer, ArrayAdapter<String>>();
for(int i = 0; i < 10; i++)
{
map.put(i, yourAdapter);
}
I've got a question. text. for example
String str = "line1"+"\n" +
"line2"+"\n" +
"line3"+"\n" +
"line4"+"\n" +
"line5"+"\n" +
"line6"+"\n" +
"line7"+"\n" +
"line8"+"\n" +
"line9"+"\n" +
"line10"+"\n" +
"line11"+"\n" +
"line12"+"\n" +
"line13"+"\n" +
"line14"+"\n" +
"line15"+"\n" +
"line16"+"\n" +
"line17"+"\n";
I have a component that creates the page.
I need my text is divided into blocks (pages) and place it on the page.
Now I work like that. I break the text into lines, and each page can place the line. But I need to break up the text such as 20 lines per 1 page. and these blocks of 20 lines displayed on a separate view. Now I work as follows:
LayoutInflater inflater = LayoutInflater.from(this);
List<View> pages = new ArrayList<View>();
String[] str1 = str.split("\n");
View[] page1 = new View[7];
TextView[] textView1 = new TextView[7];
for (int i=1;i<page1.length;i++){
page1[i] = inflater.inflate(R.layout.page, null);
textView1[i] = (TextView) page1[i].findViewById(R.id.text_view);
textView1[i].setText(str1[i+1]);//then I do not need one row and 20
pages.add(page1[i]);
}
I have a list view with check boxes.I want to get all the checked items ids or data in the particular position.
Please any one help me with sample code
Thanks in advance
I found this answer from Internet
Working properly what i need
my_sel_items=new String("Selected Items");
SparseBooleanArray a = lView.getCheckedItemPositions();
for(int i = 0; i < lv_items.length ; i++)
{
if (a.valueAt(i))
{
/*
Long val = lView.getAdapter().getItemId(a.keyAt(i));
Log.v("MyData", "index=" + val.toString()
+ "item value="+lView.getAdapter().getItem(i));
list.add(lView.getAdapter().getItemId((a.keyAt(i))));
*/
my_sel_items = my_sel_items + ","
+ (String) lView.getAdapter().getItem(i);
}
}
Log.v("values",my_sel_items);