How to change sequence of list View items in android - android

I have a calendar like this:
materialise calendar
every tic icon shows their corresponding event.
and I have a json response like this:
{
"Table": [
{
"userid": 4,
"eventname": "adi",
"eventdate": "/Date(1484121600000-0800)/",
"eventcolor": "2413AD",
"autoid": 2005
},
{
"userid": 4,
"eventname": "Aditya",
"eventdate": "/Date(1479974400000-0800)/",
"eventcolor": "5986FF",
"autoid": 1011
},
{
"userid": 4,
"eventname": "aditya",
"eventdate": "/Date(1484812800000-0800)/",
"eventcolor": "13AD1A",
"autoid": 2006
},
{
"userid": 4,
"eventname": "dfgdgdgs",
"eventdate": "/Date(1478678400000-0800)/",
"eventcolor": "AD3D1F",
"autoid": 1005
},
{
"userid": 4,
"eventname": "dfgdgdgs",
"eventdate": "/Date(1478678400000-0800)/",
"eventcolor": "AD3D1F",
"autoid": 1006
}
]
}
Here comes the real problem.When I click any date on the calendar, above json response comes in a listView according to sequence of json. But, I want to change the sequence according to which date i clicked(i.e. Date i am clicked in the calendar, that date comes first in the list and remaining item comes after that).
I'm not posting any code, If any one wants to see my code, please ask.

When you parse the list from JSON at that time you check the date of each object and compare to the clicked date and store that particular index.
Then you simply parse the list from JSON response
at parsing time you remove that particular object at that stored index and move to the first position of that list.
Use code like:
list.removeAt(stored_index);
list.add(0, objectOfList);
and then simply add that list to adapter

Related

How to post row data in array using Json in retrofit2

I am looking for post data on the server using retrofit2 in this format.
[
{
"BeneficiaryNameID": 1,
"BeneficiaryName": "Ronak",
"RFIDTag": "00000001",
"Village": "valsad",
"Faliya": "valsad",
"createDate": "10/4/2021 2:57:31 PM",
"RehabStatus": 1
},
{
"BeneficiaryNameID": 2,
"BeneficiaryName": "Rachna",
"RFIDTag": "00000002",
"Village": "gurgoun",
"Faliya": "test",
"createDate": "10/4/2021 2:57:31 PM",
"RehabStatus": 2
} ]
I have searched so many links but didn't get results. I waste my 2,3 hrs. Please help me.

Parse Nested JSON data in Android

I am trying to parse JSON data which looks like this in android;
{
"data": [{
"http_code": 200,
"message": "success",
"created_at": "2016/10/12",
"categories": [{
"cat_id": 1,
"cat_name": "Business and Commerce",
"subcategories":[{
"subcat_id": 1,
"subcat_name": "Intellectual Property",
"articles":[{
"article_id": 1,
"article_heading": "What is intellectual?",
"article_url": "http://example.com/1"
},{
"article_id": 2,
"article_heading": "Types of intellectual Properties",
"article_url": "http://example.com/2"
}]
}, {
"subcat_id": 2,
"subcat_name": "Business Licensing",
"articles":[{
"article_id": 1,
"article_heading": "What is a license?",
"article_url": "http://example.com/1"
},{
"article_id": 2,
"article_heading": "Types of Business Licenses",
"article_url": "http://example.com/2"
}]
}]
}, {
"cat_id": 2,
"cat_name": "Family Law",
"subcategories":[{
"subcat_id": 3,
"subcat_name": "Domestic Violence",
"articles":[{
"article_id": 1,
"article_heading": "What is domestic violene?",
"article_url": "http://example.com/1"
},{
"article_id": 2,
"article_heading": "Types of domestic violence",
"article_url": "http://example.com/2"
}]
}, {
"subcat_id": 4,
"subcat_name": "Marriage",
"articles":[{
"article_id": 1,
"article_heading": "What is a marriage?",
"article_url": "http://example.com/1"
},{
"article_id": 2,
"article_heading": "Types of marriages",
"article_url": "http://example.com/2"
}]
}]
}]
}]
}
I am supposed to see data for a specific node when I select it via a listview list item click and so on....
So far I have managed to parse all the categories and display them in a listview, but I want to display subcategories for a category I select on the listview.
Set an onItemClickListener on your listview and use the position of the selected item to return the relevant subcategories?
For example, perhaps something along the lines of:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Switch(position) {
case 0:
//get first subcategories array values
break;
case 1:
//get second subcategories array values
break;
....
}
}
Use ExpandableListView, example given on this link.
For json parsing use Google gson, it will make your work easy and efficient.
It seems similar to How to parse this nested JSON array in android
But you can use this lib https://github.com/FasterXML/jackson
to make the work easy and faster using Objects.
It is a small tutorial: JacksonInFiveMinutes
Specially: Full Data Binding (POJO) Example. And JacksonDataBinding
Thanks everyone for your help. I finally managed to solve the issue;
Here is how I did it;
JSONObject jsonObj = new JSONObject(jsonStr);
JSONObject subcategories = jsonObj.getJSONObject("data").getJSONArray("categories").getJSONObject((int)getItemId(position));
Log.e("TAG","Subcategories: " + subcategories);
I don't know if that is the correct way, but it worked in my case.

How to Parse the following json response in Android?

The problem with the following JSON response parsing is that is giving the error as "Unterminated object at character 32".
{
"0": {
"review": {
"reviewTime": "2015-09-24 22:07:03",
"author": "John Doe",
"rating_5": 1.5,
"rating": 2
}
},
"1": {
"review": {
"reviewTime": "2015-09-25 18:05:14",
"author": "Samantha",
"rating_5": 5,
"timestamp": 1443184514,
"rating": 5
}
},
"count": 3,
"review_url": "https://localhost/reviews"
}
I'm validated your json using in http://jsonformatter.curiousconcept.com/ and it's valid.
The 32 character is a date and must be quoted. Check in debug mode if the date is quoted.
In one of your comments to your question you wrote that your response looks like:
{ count=2041.0, 4={review={reviewTime=2015-09-24 22:07:03, author=Neha Primith, rating_5=1.5, rating=2.0, reviewTimeFriendly=yesterday}}, 1={review={reviewTime=2015-09-24 22:07:03, author= John Doe, rating_5=1.5, rating=2.0,}}, 0={review={reviewTime=2015-09-25 18:05:14, author=Samantha, rating_5=5.0, rating=5.0}}, , review_url=https://localhost/reviews,count=2 }
But it's not json format. Json format is sensitive to quotes. So make sure you get a properly formatted response

How do you find a user's twitter id from a user's twitter username in Android

In my android app, I want users to be able to enter a twitter username and from there the twitter app launches on the entered username's page. I did some research and found out the link needed to open the twitter app from another app is twitter://user?user_id=id_num I was wondering if there is a way to get the user's twitter id from a twitter username in Android so I can make this happen. Any help would be greatly appreciated, thank you.
You should read through the Twitter API, in particular the section about user lookups
Edit: Links updated to current Twitter API docs though answer remains targeted at API v1.1 as when asked.
That particular request has optional fields to search by id or screen_name and will return a list of matches which you can parse for user ids. So you can search by screen_name and read the response to get an id.
The request you are (were) looking for is:
GET https://api.twitter.com/1/users/lookup.json?screen_name=somename
The provided response in the API examples is (was):
[
{
"name": "Twitter API",
"profile_sidebar_border_color": "87bc44",
"profile_background_tile": false,
"profile_sidebar_fill_color": "e0ff92",
"location": "San Francisco, CA",
"profile_image_url": "http://a3.twimg.com/profile_images/689684365/api_normal.png",
"created_at": "Wed May 23 06:01:13 +0000 2007",
"profile_link_color": "0000ff",
"favourites_count": 2,
"url": "http://apiwiki.twitter.com",
"contributors_enabled": true,
"utc_offset": -28800,
"id": 6253282,
"profile_use_background_image": true,
"profile_text_color": "000000",
"protected": false,
"followers_count": 160752,
"lang": "en",
"verified": true,
"profile_background_color": "c1dfee",
"geo_enabled": true,
"notifications": false,
"description": "The Real Twitter API. I tweet about API changes, service issues and appily answer questions about Twitter and our API. Don't get an answer? It's on my website.",
"time_zone": "Pacific Time (US & Canada)",
"friends_count": 19,
"statuses_count": 1858,
"profile_background_image_url": "http://a3.twimg.com/profile_background_images/59931895/twitterapi-background-new.png",
"status": {
"coordinates": null,
"favorited": false,
"created_at": "Tue Jun 22 16:53:28 +0000 2010",
"truncated": false,
"text": "#Demonicpagan possible some part of your signature generation is incorrect & fails for real reasons.. follow up on the list if you suspect",
"contributors": null,
"id": 16783999399,
"geo": null,
"in_reply_to_user_id": 6339722,
"place": null,
"source": "TweetDeck",
"in_reply_to_screen_name": "Demonicpagan",
"in_reply_to_status_id": 16781827477
},
"screen_name": "twitterapi",
"following": false
},
{
"name": "Twitter",
"profile_sidebar_border_color": "EEEEEE",
"profile_background_tile": false,
"profile_sidebar_fill_color": "F6F6F6",
"location": "San Francisco, CA",
"profile_image_url": "http://a1.twimg.com/profile_images/878669694/twitter_bird_normal.jpg",
"created_at": "Tue Feb 20 14:35:54 +0000 2007",
"profile_link_color": "038543",
"favourites_count": 2,
"url": "http://twitter.com",
"contributors_enabled": true,
"utc_offset": -28800,
"id": 783214,
"profile_use_background_image": true,
"profile_text_color": "333333",
"protected": false,
"followers_count": 3305606,
"lang": "en",
"verified": true,
"profile_background_color": "ACDED6",
"geo_enabled": true,
"notifications": false,
"description": "Always wondering what's happening. ",
"time_zone": "Pacific Time (US & Canada)",
"friends_count": 257,
"statuses_count": 774,
"profile_background_image_url": "http://s.twimg.com/a/1276896641/images/themes/theme18/bg.gif",
"status": {
"coordinates": null,
"favorited": false,
"created_at": "Tue Jun 22 16:40:19 +0000 2010",
"truncated": false,
"text": "9 cool things to do with your Twitter account (via #pastemagazine) http://example.com",
"contributors": [
16739704
],
"id": 16783169544,
"geo": null,
"in_reply_to_user_id": null,
"place": null,
"source": "web",
"in_reply_to_screen_name": null,
"in_reply_to_status_id": null
},
"screen_name": "twitter",
"following": false
}
]
So you can access the id from the results by parsing that JSON response for the "id" field.
Note however that the returned is a JSON array of results and not just a single definitive answer. You will need to work out yourself which is the correct one (order is not guaranteed either so do not assume the first entry is the most likely).
Also worth noting is that even though this is documented as returning 'id' and an integer value, the current preferred usage of ids is to supply 'id_str' and a string representation of the integer. This is due to inconsistencies in how various platforms handle and limit integers. So even if you receive 'id' you should use 'id_str' for future interactions.
Assuming you have a bearer token from the Twitter Developer Platform, you can get the id for a given username with the Twitter API v2 users lookup endpoint.
So for example, if I send an authorized GET request to
https://api.twitter.com/2/users/by?usernames=TwitterDev,TwitterAPI
Response:
{
"data": [
{
"id": "2244994945",
"name": "Twitter Dev",
"username": "TwitterDev"
},
{
"id": "6253282",
"name": "Twitter API",
"username": "TwitterAPI"
}
]
}
Python Code Example:
import requests
bearer_token = '<YOUR_TWITTER_BEARER_TOKEN_HERE>'
headers = {"Authorization": f"Bearer {bearer_token}"}
params = {"usernames": "TwitterDev,TwitterAPI"}
url = "https://api.twitter.com/2/users/by"
# Get list of twitter follows
r = requests.get(url, params=params, headers=headers, timeout=20)
if r.status_code != 200:
raise Exception(
"Request returned an error: {} {}".format(
r.status_code, r.text
)
)
json_response = r.json()
for u in json_response['data']:
username = u['username']
id = u['id']
print(f"The user id for {username} is {id}")
Output:
The user id for TwitterDev is 2244994945
The user id for TwitterAPI is 6253282
See also:
Twitter Developer Platform - Users lookup
Twitter APIv2 sample code - get_users_with_bearer_token.py
TweeterID - Twitter ID and username converter

retrieve video (categoryID) from youtube using json

i want to upload youtube videos from my app and find those videos after.
to do so i uploaded the videos using v3 youtube api
here is part of the code that use to add title, description Etc.
snippet.setTitle("Test Upload via Java on " + cal.getTime());
snippet.setDescription(
"Video uploaded via YouTube Data API V3 );
// Set your keywords.
List<String> tags = new ArrayList<String>();
tags.add("test");
tags.add("example");
tags.add("java");
tags.add("YouTube Data API V3");
tags.add("erase me");
snippet.setTags(tags);
i want to add catagotyid so i just add :
snippet.setCategoryId("test");
in this moment the upload code stop to work, why ?
to retrieve all the video that upload by my app i need to fill in part line "snippet" (without Quotes) and in id line add my category id like "test" (without Quotes),
and then result catch by json ?
UPDATE :
How should I fill the "try it!" ? to recieve list of catagoryID, And assume that I got the list, how do I know which category to choose?, I think I'm missing something and I do not know what
Try a GET request on this:
https://www.googleapis.com/youtube/v3/videoCategories?part=snippet&regionCode=US&key={YOUR_API_KEY}
Of course you replace the {YOUR_API_KEY} with your key. You should get back something like this:
{
"kind": "youtube#videoCategoryListResponse",
"etag": "\"Fn7tolrWrLXf7uknBpwCU9OfMA8/jOw8Jx4EQq_udW-0UgiRcCMWqWQ\"",
"items": [
{
"id": "1",
"kind": "youtube#videoCategory",
"etag": "\"Fn7tolrWrLXf7uknBpwCU9OfMA8/gljmmWdkbtlCrMi74KVYLrbTaIA\"",
"snippet": {
"channelId": "UCBR8-60-B28hp2BmDPdntcQ",
"title": "Film & Animation"
}
},
{
"id": "2",
"kind": "youtube#videoCategory",
"etag": "\"Fn7tolrWrLXf7uknBpwCU9OfMA8/n1RtRMaPpKgph8MvS9wf742Tm5U\"",
"snippet": {
"channelId": "UCBR8-60-B28hp2BmDPdntcQ",
"title": "Autos & Vehicles"
}
},
{
"id": "10",
"kind": "youtube#videoCategory",
"etag": "\"Fn7tolrWrLXf7uknBpwCU9OfMA8/1LzYK9Uf9Nt0hzBqNqhuHw0LPI0\"",
"snippet": {
"channelId": "UCBR8-60-B28hp2BmDPdntcQ",
"title": "Music"
}
},

Categories

Resources