Which Json structure suitable for this work? - android

This function generate dummy data for this Structure .
private void createDummyData() {
for (int i = 1; i <= 10; i++) {
VerticalDataModel dm = new VerticalDataModel();
dm.setSectionTitle("Section " + i);
ArrayList<SingleItemModel> singleItem = new ArrayList<SingleItemModel>();
for (int j = 0; j <= 10; j++) {
singleItem.add(new SingleItemModel("Item " + j, "URL " + j));
}
dm.setSingleItemModelha(singleItem);
allSampleData.add(dm);
}
Know for generating this data from internet , i need json structor for this work .(I use volley library)
Thanks <3

{
data : [
{
"title":"section1",
"items":[{"name":"item1","url":"url1"},{"name":"item2","url":"url2"}]
},
{
"title":"section2",
"items":[{"name":"item3","url":"url3"},{"name":"item4","url":"url4"}]
}
]
}
so basically you have array of objects which contains title and items as its property. Again items is another array of objects which contains name and url as its property.

Related

How to properly notifyItemMoved, insert and removed in RecyclerView old data list when i get new fresh list to assign to it

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.

Get first argument in live-template

Recently i was creating my own live-template, i was customizing for loop, here is how default for loop is given in live-template.
for(int $INDEX$ = 0; $INDEX$ < $LIMIT$; $INDEX$++) {
$END$
}
but i want my method's first argument inplace of $LIMIT$, how can i do that?
public void getList(ArrayList<String> list)
{
}
then my for loop shoulde be
for(int i = 0; i < list.size; i++) {
...
}
I have seen template of logm, but it is printing all arguments of method
groovyScript("'\"' + _1.collect { it + ' = [\" + ' + it + ' + \"]'}.join(', ') + '\"'", methodParameters())
You can add the following live template:
for(int $INDEX$ = 0; $INDEX$ < $VAR$.size(); $INDEX$++){
$END$
}
Then go to right button "Edit variables" and put the following predefined methods in the expressions fields:
You can find all predefined methods in Jetbrains documentation

Sum of List data in retrofit

hear is my JSON Response,I want to total of list price.Use retrofit framework to parse JSON.
{
"price": [
"11",
"29"
]
}
int sum = 0;
int price = gridValues.get(position).getPrice().size();
for (int j = 0; j < price; j++) {
String total =gridValues.get(position).getPrice().get(j);
sum += Integer.parseInt(total);
}
fetch data from list and set in adapter and in adepter I put above code and solve my problem
I believe your request return type is JsonObject. To get sum of your price JsonArray, you need to loop through every object of it and sum it.
JsonObject object = parse.parse(data).getAsJsonObject();
JsonArray priceArray = object.getAsJsonArray("price");
int total = 0;
for (int i = 0; i < priceArray.size(); i++) {
total += priceArray.get(i).getAsInt();//reading (as int) each object of array
}
System.out.println("Total is " + total);

Null pointer exception on consecutive execution of the test script

I am automating a product using Robotium. In a module I have to validate the data derived.
For that I am using this code:
class sharefirstlevel {
public void A {
for(int k=1;k<=4;k+=2) {
ExpectedResult = solo.clickInList(k);
for (int i = 0; i < ExpectedResult.size(); i++) {
Actualvalues[i] += ExpectedResult.get(i).getText().toString() + " ";
solo.scrollListToLine(0, k);
ExpectedResult=solo.clickInList(1);
Actualvalues[i] += ExpectedResult.get(i).getText().toString() + " ";
ExpectedResult = solo.clickInList(2);
Actualvalues[i] += ExpectedResult.get(i).getText().toString() + " ";
Log.d("xyz","Values of the Strings are"+Actualvalues[i].toString());
}
}
}
}
Its extracting the values selected to an array.
Now when its derived, to validate I am using this code:
class sharedLevel {
public void B {
for(int i=0; i <= sharefirstlevel.Actualvalues.length; i++) {
Log.d("xyz","Values are:"+sharefirstlevel.Actualvalues[i]);
actual=solo.searchText(sharefirstlevel.Actualvalues[i]);
assertEquals(expected, actual);
Log.d("xyz","Values are:"+sharefirstlevel.Actualvalues[i]);
}
}
}
The values are getting extracted to the array and when printed, the proper values are getting inseted to the array and are even getting passed to another class.
But the execution of the junit test case will be successful only for 1 or 2 times. But from then i am getting a null pointer exception. Please help me. Am at a fix!!
The first value while getting inserted to the ArrayList itself is null. Here is the Code:
for(int k=1;k<=4;k+=2)
{
ExpectedResult = solo.clickInList(k);
for (int i = 0; i < ExpectedResult.size(); i++)
{
Actualvalues[i] += ExpectedResult.get(i).getText().toString() + " ";
solo.scrollListToLine(0, k);
ExpectedResult=solo.clickInList(1);
Actualvalues[i] += ExpectedResult.get(i).getText().toString() + " ";
ExpectedResult = solo.clickInList(2);
Actualvalues[i] += ExpectedResult.get(i).getText().toString() + " ";
Log.d(" ","Values of the Strings are"+Actualvalues[i].toString());
}
}
The value getting inserted at the first line within the for loop itself is null. Hence the problem.
But I am not getting, why null value is being inserted at 0th position. Please help me out for solutions.
In your sharedLevel class, change:
for(int i=0; i <= sharefirstlevel.Actualvalues.length; i++)
to:
for(int i=0; i < sharefirstlevel.Actualvalues.length; i++)
Since your counter starts at 0, it should less than the length of your array.

Passing String Array to A Robotium Class

I am automating a product using Robotium. In a module I have to validate the data derived.
For that I am using this code:
class sharefirstlevel {
public void A {
for(int k=1;k<=4;k+=2) {
ExpectedResult = solo.clickInList(k);
for (int i = 0; i < ExpectedResult.size(); i++) {
Actualvalues[i] += ExpectedResult.get(i).getText().toString() + " ";
solo.scrollListToLine(0, k);
ExpectedResult=solo.clickInList(1);
Actualvalues[i] += ExpectedResult.get(i).getText().toString() + " ";
ExpectedResult = solo.clickInList(2);
Actualvalues[i] += ExpectedResult.get(i).getText().toString() + " ";
Log.d("xyz","Values of the Strings are"+Actualvalues[i].toString());
}
}
}
}
Its extracting the values selected to an array.
Now when its derived, to validate I am using this code:
class sharedLevel {
public void B {
for(int i=0; i <= sharefirstlevel.Actualvalues.length; i++) {
Log.d("xyz","Values are:"+sharefirstlevel.Actualvalues[i]);
actual=solo.searchText(sharefirstlevel.Actualvalues[i]);
assertEquals(expected, actual);
Log.d("xyz","Values are:"+sharefirstlevel.Actualvalues[i]);
}
}
}
But the thing is in the first part of code proper values are extracted to the Array.
In the second part of code when I am passing the array values, I am not getting the values but the memory allocation object. Hence the test case is failing.
Please help me. Am at a fix !!
How about using
assertTrue(Arrays.equals(expected, actual));
EDIT: This answer is for the question for which the bounty was raised and not the edited question (https://stackoverflow.com/posts/18334315/revisions)
I solved the Problem. The change which I had to make in Class B was
for(int i=1;i<sharefirstlevel.Actualvalues.length;i++){
Log.d("Pratibha","Values are:"+sharefirstlevel.Actualvalues[i]);
actual=solo.searchText(sharefirstlevel.Actualvalues[i]);
assertEquals(expected, actual);
Log.d("TAG","Values are:"+sharefirstlevel.Actualvalues[i]);
}
And hence I am getting the values of the array in class B. Because the index started from 0, and I din't have any text values at 0th position, It was returning garbage values.
Thanks all for the responses.

Categories

Resources