How to apply onClick function to open its URL - java

I have the following code:
try {
jArray = new JSONArray(result);
JSONObject json_data = null;
String myString = null;
for (int i = 0; i < jArray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
json_data = jArray.getJSONObject(i);
map.put("id", String.valueOf(json_data.getString("news_id")));
map.put("title", json_data.getString("news_title"));
myString = myString + "| "
+ json_data.getString("news_title").toString();
}
String myString1 = myString.replace("null", "");
txt1 = (TextView) findViewById(R.id.twxmorq);
txt1.setSelected(true);
txt1.setText(myString1);
I am printing whole title in text view with marquee style in single line with ' ! ' partition. Now I want to apply onclick on each title and open it in new activity with detail news in web view.
Detail news will load with this data:
"http://www.taxmann.com/TaxmannWhatsnewService/Services.aspx?service=getnewsdetails&newsid=" + id
Where id will differ for different title. Can you please tell me the logic or post some code for Implementation?
I'm not able to do this.

Either in layout xml file, for TextView attribute add,
android:autoLink="web"
Or,
txt1.setMovementMethod(LinkMovementMethod.getInstance());

Related

obj.getString - Styling in android studio, possible?

Looking to style (bold, italic etc.) "quote" and "name", but have tried several potential solutions with no luck:
private void settextView(String json) throws JSONException {
JSONArray jsonArray = new JSONArray(json);
String[] tasks = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject obj = jsonArray.getJSONObject(i);
tasks[i] = obj.getString("quote") + "\n\n" + obj.getString("name");
textView.setText(tasks[0]);
setBtnCopyOnClick(tasks[0]); //Here
String a = "quote";
SpannableString spanned = new SpannableString(a);
spanned.setSpan(new UnderlineSpan(), 1, a.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
}
They both generate in the same textView, so unfortunately I can only style all, or not at all.
If you are looking to style only specific parts of text you should be using spans. The Android Developers site has good documentation on this which you can read here.
For your particular use case pay most attention to the StyleSpan which allows you to set Typeface flags.
For example:
String a = "underlined text";
String b = " normal text";
SpannableString spanned = new SpannableString(a + b);
spanned.setSpan(new UnderlineSpan(), 0, a.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

Change text styling - settextView getString

I have two strings that are pulled from json, need to change styling for both elements.
My code is:
private void settextView(String json) throws JSONException {
JSONArray jsonArray = new JSONArray(json);
String[] tasks = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject obj = jsonArray.getJSONObject(i);
tasks[i] = obj.getString("quote") + "\n\n" + obj.getString("name");
TextView quote = null;
quote.setTextColor(Color.parseColor("#FFFFFF"));
textView.setText(tasks[0]);
setBtnCopyOnClick(tasks[0]); //Here
}
}
As you can see, I have tried to insert:
TextView quote = null;
quote.setTextColor(Color.parseColor("#FFFFFF"));
However this doesn't do anything. Anyone have any ideas? I could be doing this the completely wrong way.
I would have had both strings separate in my .xml file, however it is pulled through as one.
TextView quote=null;
quote=findViewById(R.id.textView1);
this is not working because you give only null value to TextView;
here textview1 is id of TextView in you xml file;
replace textView1 with your textview id

How to retrieve selected data from JSON Array?

The data on server I want to retrieve, data in JSON format but problem is that I want to retrieve only data having specific CHEF_NAME from JSON array.I am using Android Studio.
JSON Array
[{"status":"Success","recpie":[{"id":"162","image":"1580130013.png","cat_id":"17","name":"Tikka Boti Mix Recipe","chef_name":"Mehran","link":"https:\/\/www.youtube.com\/watch?v=sbPNGHIOpQY","uvlink":"","like":"2","view":"227"},{"id":"168","image":"1580211123.png","cat_id":"17","name":"Seekh Kabab Paratha Roll Recipe","chef_name":"Mehran","link":"https:\/\/www.youtube.com\/watch?v=q36Syxhjg3o","uvlink":"","like":"19","view":"158"},{"id":"176","image":"recipe_1583154151.jpg","cat_id":"17","name":"Windows","chef_name":"Imran Ahmed","link":"","uvlink":"","like":"2","view":"119"},{"id":"190","image":"recipe_1584214019.jpg","cat_id":"17","name":"Chicken Broast","chef_name":"Imran Developer","link":"","uvlink":"http:\/\/billing.synctechsol.com\/video\/VID-20200112-WA0020.mp4","like":"3","view":"108"},{"id":"161","image":"1580309239.png","cat_id":"14","name":"Sindhi Biryani Mix Recipe","chef_name":"Mehran","link":"https:\/\/www.youtube.com\/watch?v=OUG7gYTrnEk","uvlink":"","like":"4","view":"104"},{"id":"182","image":"recipe_1584126527.jpeg","cat_id":"17","name":"Broast","chef_name":"Imran Developer","link":"","uvlink":"","like":"1","view":"64"},{"id":"198","image":"recipe_1584737360.jpeg","cat_id":"17","name":"Distribution Board","chef_name":"Imran Ahmed","link":"","uvlink":"http:\/\/billing.synctechsol.com\/video\/VID-20200219-WA0035.mp4","like":"7","view":"60"},{"id":"191","image":"recipe_1584260119.png","cat_id":"17","name":"ggbvn","chef_name":"Imran Developer","link":"","uvlink":"http:\/\/billing.synctechsol.com\/video\/VID-20200315-WA0001.mp4","like":"1","view":"58"},{"id":"163","image":"1580134470.png","cat_id":"15","name":"Achar Gosht Mix Recipe","chef_name":"Mehran","link":"https:\/\/www.youtube.com\/watch?v=5EQWOSRpzHg","uvlink":"","like":"8","view":"54"},{"id":"101","image":"1580210522.png","cat_id":"12","name":"Fish Fry Mix Recipe","chef_name":"Mehran","link":"https:\/\/www.youtube.com\/watch?v=GeSloGLoPMk","uvlink":"","like":"9","view":"50"},{"id":"178","image":"recipe_1583407824.jpg","cat_id":"17","name":"uhnbj","chef_name":"hi igh","link":"","uvlink":"","like":"0","view":"47"},{"id":"177","image":"recipe_1583234674.jpeg","cat_id":"17","name":"Kabuli Pulao","chef_name":"Imran Ahmed","link":"","uvlink":"","like":"0","view":"46"}]}]
Java Code
JSONArray jsonArray = new JSONArray(response);
JSONObject jsonObject = jsonArray.getJSONObject(0);
String status = jsonObject.getString("status");
if (status.equals("Success")) {
JSONArray recipe = jsonObject.getJSONArray("recpie");
Log.e("Lenght", String.valueOf(recipe.length()));
for (int i = 0; i < recipe.length(); i++) {
JSONObject jsonObject1 = recipe.getJSONObject(i);
SearchGetSet searchGetSet = new SearchGetSet();
String id = jsonObject1.getString("id");
String image = jsonObject1.getString("image");
String cat_id = jsonObject1.getString("cat_id");
String name = jsonObject1.getString("name");
String chef_name = jsonObject1.getString("chef_name");
String link = jsonObject1.getString("link");
String uvlink = jsonObject1.getString("uvlink");
String like = jsonObject1.getString("like");
String view = jsonObject1.getString("view");
searchGetSet.setId(id);
searchGetSet.setRec_name(name);
searchGetSet.setRec_image(image);
searchGetSet.setLikes(like);
searchGetSet.setCat_id(cat_id);
searchGetSet.setChef_name(chef_name);
searchGetSet.setLink(link);
searchGetSet.setUVlink(uvlink);
searchGetSet.setView(view);
searchList.add(searchGetSet);}}
Create and add your searchGetSet object only if the chef_name is matching the name you're searching.
My guess is (I don't know Java), you've to use a simple if-statement, while looping :
for (int i = 0; i < recipe.length(); i++) {
JSONObject jsonObject1 = recipe.getJSONObject(i);
SearchGetSet searchGetSet = null; // Do not create your searchGetSet() object yet.
String chef_name = jsonObject1.getString("chef_name");
// Check the chef's name is 'John'.
if (chef_name.equals('John')) {
String id = jsonObject1.getString("id");
String image = jsonObject1.getString("image");
String cat_id = jsonObject1.getString("cat_id");
String name = jsonObject1.getString("name");
// ...
searchGetSet = new SearchGetSet();
searchGetSet.setId(id);
searchGetSet.setRec_name(name);
searchGetSet.setRec_image(image);
// ...
// Let's add the result we have found.
searchList.add(searchGetSet);
}
}

android error when trying to put json data in viewlist php+json+java

I am trying to make a search page for my andriod app using PHP,JSON and SQL.
here is the code that is giving me the error :
try{
JSONArray jArray = new JSONArray(result);
int jArrLeng = jArray.length();
for(int i=0; i<jArrLeng;i++){
JSONObject json_data= jArray.getJSONObject(i);
tempID += json_data.getString("ID") + "\n";
tempID += json_data.getString("heading") + "\n";
tempID += json_data.getString("rank") + "\n:";
}
arr = tempID.split(":");
resultLV.setAdapter(new ArrayAdapter<String>(SearchPage.this, android.R.layout.simple_list_item_1,arr));
}catch (Exception e){
String errMsg = "error when putting the json data in the list";
Toast.makeText(getApplicationContext(), errMsg, Toast.LENGTH_LONG).show();
}
i have already used this code on another page and it works perfectly but when using it another page/activity it gives me an error when trying to put the json data in listview.
i would guess the problem is when i am setting the ArrayAdapter, have i done anything wrong?
resultLV.setAdapter(new ArrayAdapter<String>(SearchPage.this, android.R.layout.simple_list_item_1,arr));
To answer your final question:
//array list
List<String> your_array_list = new ArrayList<String>();
try{
JSONArray jArray = new JSONArray(result);
int jArrLeng = jArray.length();
for(int i=0; i<jArrLeng;i++){
JSONObject json_data= jArray.getJSONObject(i);
your_array_list.add(json_data.getString("ID") + "\n");
your_array_list.add(json_data.getString("heading") + "\n");
your_array_list.add(json_data.getString("rank") + "\n:");
}
resultLV.setAdapter(new ArrayAdapter<String>(SearchPage.this, android.R.layout.simple_list_item_1,your_array_list));
//then, to get the items from inside the adapter:
for(String item_in_list : your_array_list){
System.out.println(item_in_list);
}
To answer the original question:
Check out http://jsonformatter.curiousconcept.com/ for formatting issues
Try looking at this info:
http://www.json.org/javadoc/org/json/JSONObject.html#JSONObject%28java.lang.String%29
JSONObject
public JSONObject(java.lang.String source)
throws JSONException
Construct a JSONObject from a source JSON text string. This is the most commonly used` JSONObject constructor.
Parameters:
source - `A string beginning with { (left brace) and ending with } (right brace).`
Throws:
JSONException - If there is a syntax error in the source string or a duplicated key.
FYI: The inbuilt JSONObject and JSONArray cannot be used to get certain json responses.
You could try downloading a small library named "json-simple-1.1.1.jar" from this link https://json-simple.googlecode.com/files/json-simple-1.1.1.jar.

How do I get the text from this JSON file using GSON?

Im using GSON to parse this JSON file:
{
"database" : {
"source" : "google",
"items" : [
{"title" : "hello world",
"id" : "id_hello_world"},
{"title" : "goodbye world",
"id" : "id_goodbye_world"}
]
}
}
Which ive read into String jsonLine
Im trying to parse it and output all the values but im getting a ClassCastException at the
JsonObject source = database.getAsJsonObject("source") line.
I think im searching for the data wrong. Im using this to search and output:
JsonElement jelement = new JsonParser().parse(jsonLine);
JsonObject jobject = (JsonObject) jelement;
JsonObject database = jobject.getAsJsonObject("database"); //Get Database
JsonObject source = database.getAsJsonObject("source"); //Get Source
System.out.println("Source: " + source.toString()); //Print source
JsonArray items = database.getAsJsonArray("items"); //Get items array
for(int i=0; i< items.size(); i++){ //for every item
JsonObject item = (JsonObject) items.get(i); //Select item i
JsonObject title = (JsonObject) item.getAsJsonObject("title");
JsonObject id = (JsonObject) item.getAsJsonObject("id");
System.out.println("Item " + i + " title : " + title.toString() + ", id : " + id.toString());
}
If someone could correct my code that would be perfect. I know there are other simpler ways to do this using GSON.fromJSON(jsonLine, Wrapper.class) but im trying to learn to do it this way too. Thanks for the help!
Before you are going to use Gson method, create your template structure:
class Item{
private String item = "";
private String id = ""; // for sure you can use 'int'
}
public class DataBase{
private List<Item> items = null;
private String source = "";
}
Now your main class:
public class YourClass{
private DataBase database = "";
....
database = gson.fromJson(yourString, YourClass.class);
...
}
Don't forget try/catch wrapper
Enjoy
use JsonPrimitive instead of JsonObject. (it must be internal bug of GSON, because, trying to convert JsonPrimitive to JsonObject was impossible, as far as the version of GSON 2.2.3)
So, your code comes like this:
JsonElement jelement = new JsonParser().parse(jsonLine);
JsonObject jobject = (JsonObject) jelement;
JsonObject database = jobject.getAsJsonObject("database"); // Get
// Database
JsonPrimitive source = database.getAsJsonPrimitive("source"); // Get
// Source
System.out.println("Source: " + source.toString()); // Print source
JsonArray items = database.getAsJsonArray("items"); // Get items array
for (int i = 0; i < items.size(); i++) { // for every item
JsonObject item = (JsonObject) items.get(i); // Select item i
JsonPrimitive title = item.getAsJsonPrimitive("title");
JsonPrimitive id = item.getAsJsonPrimitive("id");
System.out.println("Item " + i + " title : " + title.toString()
+ ", id : " + id.toString());
}
As Maxim Shoustin showed, creating template will be good solution for it.

Categories