ArrayAdapter with string array and Jsoup - android

I am able to extract the href elements from a page and store the results into a string array. Then display it inside a TextView. The problem comes if I try to display to a ListView. I don't know how to work with ArrayAdapters in this case. This is my working code that displays into a TextView.
package com.example.jsouptestarray;
import java.io.IOException;
import java.util.ArrayList;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import com.example.jsouptestarray.R;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.text.method.ScrollingMovementMethod;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView text;
ListView list;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new MyTask().execute();
}
private class MyTask extends AsyncTask<Void, Void, ArrayList<String>> {
ArrayList<String> arr_linkText=new ArrayList<String>();
#Override
protected ArrayList<String> doInBackground(Void... params) {
Document doc;
String linkText = "";
try {
doc = Jsoup.connect("https://www.google.com/").get();
Elements links = doc.getElementsByTag("a");
for (Element el : links) {
linkText = el.attr("href");
arr_linkText.add(linkText); // add value to ArrayList
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return arr_linkText; //<< retrun ArrayList from here
}
#Override
protected void onPostExecute(ArrayList<String> result) {
// get all value from result to display in TextView
for (String temp_result : result) {
System.out.println("links :: "+temp_result);
text = (TextView) findViewById (R.id.textView2);
text.append(temp_result + "\n" + "\n");
text.setMovementMethod(new ScrollingMovementMethod());
}
}
}
}
This is my attempt with the ListView, but I get an error and I don't understand how to fix it.
list = (ListView) findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, temp_result);
// Assign adapter to ListView
list.setAdapter(adapter);
Here is a screenshot of the error
I hope someone can help me fix it, and I appreciate your time!

Fourth element of the constructor is excess in your case. It can be used to set data at once, you should see API.
Try this:
list = (ListView) findViewById(R.id.listView1);
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1,
android.R.id.text1);
for (String temp_result : result)
{
adapter.add(temp_result);
}
// Assign adapter to ListView
list.setAdapter(adapter);

Replace this with MainActivity.this. When you're inside the AsyncTask, this refers to the AsyncTask instance, which can't be passed to the constructor (different, unrelated, incompatible classes)
So you need to call an Activity as the argument and since your AsyncTask is part of an Activity, you can explictly reference the Activity instance with ClassName.this.

Related

setting values from an json array to spinner on application level

I want to set Json values into a spinner list. The data I have:
JSONArray lang = jsonObject.getJSONArray("languages");
for (int i = 0; i < lang.length(); i++) {
String language = lang.getString(i);
}
How can i load String values into a spinner list?
ArrayAdapter<String> arrayAdapter = new ArrayAdapter(yourContext, android.R.layout.activity_list_item)
arrayAdapter.addAll(langList); // you need to add your lang.getString(i) to an array
yourSpinner.setAdapter(arrayAdapter);
and voila!
As i understand your problem, you have list of records and you want to show them in spinner in multiple activity.
For this you can create an Class that extends Application Class and create a list in that class.
once You receive response from you web api set data in list of application class.
Now where ever you want to display data in Spinner get it from Application class and render your spinner
See below Example -
public class MyApp extends Application{
private static ArrayList<String> mDataArrayList;
public static ArrayList<String> getmDataArrayList() {
return mDataArrayList;
}
public static void setmDataArrayList(ArrayList<String> mDataArrayList) {
MyApp.mDataArrayList = mDataArrayList;
}
#Override
public void onCreate() {
super.onCreate();
}
}
Call your web api and parse data as you code in your question -
ArrayList<String> mArrayList = new ArrayList<String>();
for (int i = 0; i < lang.length(); i++) {
String language = lang.getString(i);
mArrayList.add(language);
}
//Set Application class list
MyApp.setmDataArrayList(mArrayList);
Now when you want to display data in any class then get list by calling -
ArrayList<String> mArrayList = MyApp.getmDataArrayList();
//Code to display data in Spinner
Hope it will help you.
And dont forgot to add it to manifest of your application -
<application
android:allowBackup="true"
android:name=".MyApp"
....>
....
</application>
I'd suggest you to use Spinner class activity for future usages. Here is the example of spinner activity class (data receiver and displayer):
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
public class mySpinnerClass extends Activity implements
OnItemSelectedListener {
Spinner spinner;
int selectedValue;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spinner);
selectedValue = -1;
Intent i = getIntent();
dataSelector = i.getStringExtra("MessageText");
// Spinner element
spinner = (Spinner) findViewById(R.id.mySpinner);
// Spinner click listener
spinner.setOnItemSelectedListener(this);
// Loading spinner data
loadSpinnerData();
}
private void loadSpinnerData() {
// Spinner Drop down elements
List<String> lables = yourData.get(dataSelector);
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, lables);
// Drop down layout style - list view with radio button
dataAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
}
public void onItemSelected(AdapterView<?> parent, View arg1, int position, long arg3) {
// TODO Auto-generated method stub
String label = parent.getItemAtPosition(position).toString();
//get the label and do anything you want with it
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
In caller activity:
Intent i = new Intent(getApplicationContext(), mySpinnerClass.class);
i.putExtra("MessageText",yourData);
startActivity(i);
If you want to send non-primitive data (in your example, this is a list) you need to use Serializable type while sending and receiving it. The following link illustrates this technique well:
http://javatechig.com/android/pass-a-data-from-one-activity-to-another-in-android

Listview setonitemclicklistener

I've looked up similar post regarding this question but they didn't solve my issue, so here it is.
I'm trying to set a setOnItemClickListener, for when I click on any item of my list I will open a dialog with the info (retrieve from the list) on the clicked list and more info. But i'm struggling to implement the setOnItemClickListener part.
I'm getting and error in line: myList.setOnItemClickListener(new OnItemClickListener()){
package com.example.proyectoprueba;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;;
public class PlantillaChina extends ActionBarActivity{
// DB Class to perform DB related operations
DBController controller = new DBController(this);
// Progress Dialog Object
ProgressDialog prgDialog;
HashMap<String, String> queryValues;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.lista);
Intent myIntent = getIntent();
String value = myIntent.getStringExtra("key");
TextView titulo=(TextView)findViewById(R.id.titulo);
// Get Platos records from SQLite DB
ArrayList<HashMap<String, String>> platoList = controller.getAllEntremeses();
if (value.equals("entremeses")){
platoList = controller.getAllEntremeses();
titulo.setText(value);
}else if(value.equals("arroces y pasta")){
platoList = controller.getAllArrocesyPasta();
titulo.setText(value);
}else if(value.equals("mar")){
platoList = controller.getAllMar();
titulo.setText(value);
}else if(value.equals("carnes")){
platoList = controller.getAllCarnes();
titulo.setText(value);
}
// If Platos exists in SQLite DB
if (platoList.size() != 0) {
// Set the Plato Array list in ListView
ListAdapter adapter = new SimpleAdapter(PlantillaChina.this, platoList, R.layout.itemlista, new String[] {
"platoId", "platoNombre", "platoDescripcion", "platoPrecio" }, new int[] {R.id.codigo, R.id.nombre, R.id.descripcion, R.id.precio });
ListView myList = (ListView) findViewById(R.id.listaplatos);
myList.setAdapter(adapter);
myList.setOnItemClickListener(new OnItemClickListener()){
}
}
Thank you for your time
It think you want something like this.
myList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int
position, long id)
{
// You have the position available here as well as the clicked view.
}
});
You need to actual define the OntItemClickListener to do something.
First, the code you have doesn't make much sense. You want something like this:
myList.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3)
{
// Your code here
}
});
Basically you are creating your own OnItemClickListener class, and overriding the OnItemClick method. This callback will get called whenever an item in your list is clicked. You can do whatever processing you would like inside that function.

undefined method or constructor error while extend to BaseFragment

I am getting some compile time error while extend to
BaseFragment.If I extend my VideoDetailFragment.java to Activity I doesn't get any
errors.
But I need to extend it to BaseFragment.Because I need to get the
ListViewwhile clicking that I can get the video displayed in that
url.
My issue is I am getting the Following Error:
The constructor ArrayAdapter(VideoDetailFragment, int, String[]) is undefined
The method finish() is undefined for the type VideoDetailFragment
The constructor Intent(VideoDetailFragment,
Class) is undefined
The constructor ProgressDialog(VideoDetailFragment) is undefined
Below I mentioned these all errors at the end of the Line.
VideoDetailFragment.java:
package com.sit.fth.frgment;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.app.ProgressDialog;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.fth.android.R;
import com.sit.fth.app.BaseFragment;
import com.sit.fth.model.JSONParser;
import com.sit.fth.activity.YoutubePlayActivity;
import java.util.ArrayList;
import java.util.List;
public class VideoDetailFragment extends BaseFragment {
// Categories must be pre-set
private String[] data = {"Category1", "Category2", "Category3"};
private final String TAG_VIDEOS = "videos";
private final String TAG_CAT = "video_category";
private final String TAG_URL = "video_url";
private final String TAG_TITLE = "video_title";
private List<String> videoTitles = new ArrayList<String>();
private List<String> videoURLs = new ArrayList<String>();
private ArrayAdapter<String> adapter;
private Object extras;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.list1, null);
ListView listView = ((ListView)view.findViewById(R.id.listview));
AdapterView.OnItemClickListener clickListener = null;
// Category view:
if (extras == null) {
clickListener = new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(VideoDetailFragment.this,VideoDetailFragment.class); ----->3rd Error
intent.putExtra("categoryName", data[position]);
startActivity(intent);
}
};
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, data); --->1st Error at this line
}
else { // Child view
// Get the category of this child
String categoryName = ((Bundle) extras).getString("categoryName");
if (categoryName == null)
finish(); ------>2nd Error
// Populate list with videos of "categoryName", by looping JSON data
new JSONParse(categoryName).execute();
clickListener = new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(VideoDetailFragment.this, YoutubePlayActivity.class); ----->Third Error
// Send video url and title to YoutubeActivity
intent.putExtra("videoUrl", videoURLs.get(position));
intent.putExtra("videoTitle", videoTitles.get(position));
startActivity(intent);
}
};
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, videoTitles); ---->First Error
}
listView.setAdapter(adapter);
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(clickListener);
return listView;
}
private class JSONParse extends AsyncTask<String, String, JSONObject> {
private ProgressDialog pDialog;
private String categoryName;
// Constructor // Get the categoryName of which videos will be found
public JSONParse(String category) {
this.categoryName = category;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
// Create a loading dialog when getting the videos
pDialog = new ProgressDialog(VideoDetailFragment.this); ---->4th Error
pDialog.setMessage("Getting Videos...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
#Override
protected JSONObject doInBackground(String... args) {
JSONParser jParser = new JSONParser();
// Get JSON from URL
JSONObject json = jParser.getJSONFromUrl(JSONUrl);
if (json == null)
return null;
try {
// Get video array
JSONArray videos = json.getJSONArray(TAG_VIDEOS);
// Loop all videos
for (int i=0; i<videos.length(); i++) {
JSONObject video = videos.getJSONObject(i);
Log.e("JSON:", "cat: "+video.getString(TAG_CAT)+",title: "+video.getString(TAG_TITLE)+", url: "+video.getString(TAG_URL));
// Check if video belongs to "categoryName"
if (video.getString(TAG_CAT).equals(categoryName)) {
addVideo(video);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return json;
}
private void addVideo(JSONObject video) {
try {
// Add title and URL to their respective arrays
videoTitles.add(video.getString(TAG_TITLE));
videoURLs.add(video.getString(TAG_URL));
} catch (JSONException e) {
e.printStackTrace();
}
}
#Override
protected void onPostExecute(JSONObject json) {
// Close the "loading" dialog
pDialog.dismiss();
if (json == null) {
// Do something when there's no internet connection
// Or there are no videos to be displayed
}
else // Let the adapter notify ListView that it has new items
adapter.notifyDataSetChanged();
}
}
}
I doesn't know how to solve these.Anybody can help me with these.thank you.
#stephen.
Did u try to look for the crash where u are getting.
Plz see the following references where i think ur error will get resolved
1) Error: The constructor MainActivity.ScreenSlidePagerAdapter(FragmentManager) is undefined
2) FragmentPagerAdapter troubles and woes: Constructor Undefined
3)Why am I getting a Constructor Undefined error?
Let me know if you find some more issue or any kind of help is needed.
Thanks
You need to provide a reference to a Context, not a Fragment. Instead of adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, videoTitles), you should be doing adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, videoTitles);, and similarly passing in getActivity() instead of this for all the other methods where you have erros.
Fragments are not context objects. You need to pass the Activity object by using getActivity() as first argument in array adapter.
Instead of passing this in this line ==> adapter = new
ArrayAdapter(this,
android.R.layout.simple_list_item_1, data);
pass getActivity() like this:
adapter = new ArrayAdapter(getActivity(),
android.R.layout.simple_list_item_1, data);

Android: Jsoup: GET put all items on ListView

If doc isn't null i want to put all data from doc to ListView, how do it?
If to write Element = doc.select("someSelector"); then i can't to put it in ListView;
Sorry for my english(i'am Russian)
Code:
package com.example.phpfunctions;
import java.io.IOException;
import java.util.Locale;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.widget.AutoCompleteTextView;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
private final String lang = Locale.getDefault().getLanguage();
private final String functions_list = "someURL";
private final ListView lv = (ListView) findViewById(R.id.listView1);
Document doc = null;
AutoCompleteTextView input;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new getData().execute(functions_list);
if(doc != null)
{
//--Write code here--//
}
else
Toast.makeText(this, "error", Toast.LENGTH_LONG).show();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
class getData extends AsyncTask<String, Void, Document> {
protected Document doInBackground(String... urls) {
try {
Document data = Jsoup.connect(urls[0]).get();
return data;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
protected void onPreExecute() {
}
protected void onPostExecute(Document result) {
doc = result;
}
}
}
You can select all of the Elements from a page if you use the * as a wildcard character when calling doc.select(). To add all of the elements to a ListView you need to save each element into some type of array, e.g. an ArrayList and also use an ArrayAdapter.
For example:
ArrayList<String> htmlElements = new ArrayList<String>();
if(doc != null)
{
//--Write code here--//
Elements elements = doc.select("*"); // select all elements from that page
for (Element e : elements) {
htmlElements.add(e.html()); // or e.text(), depends on what you require
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, htmlElements);
lv.setAdapter(adapter);
}
If you only want to list the elements from the body of the document, call doc.body().select("*") instead. The documentation is worth a read for some other tricks.

setAdapter is not calling getView.

I know this question has been asked several times, but the solutions have been specific to the askers' problems. Consequently, none of those solutions helped me, even though I tried following all of their suggestions.
So here goes.
I have a movies Activity like this. Notice that I have a MoviesAdapter inner class, that's supposed to populate the moviesDisplay ListView.
package com.example.midtermexam;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.database.DataSetObserver;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MoviesActivity extends Activity {
public String url;
public ListView moviesDisplay;
public static ArrayList<Movie> thisMovies;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_movies);
url=getIntent().getExtras().getString("url");
moviesDisplay = (ListView)findViewById(R.id.listView1);
new AsyncMoviesGet(this).execute(url);
}
public void populateListView()
{
Log.d("listview","adapter created");
Log.d("listview","Listview declared");
Log.d("listview","adapter populated");
moviesDisplay.setAdapter(new MoviesListAdapter());
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.movies, menu);
return true;
}
private class MoviesListAdapter extends ArrayAdapter<Movie> {
public MoviesListAdapter() {
super(MoviesActivity.this, R.layout.movies_activity_listview, thisMovies);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Make sure we have a view to work with (may have been given null)
View movieView = convertView;
if (movieView == null) {
movieView = getLayoutInflater().inflate(R.layout.movies_activity_listview, parent, false);
}
// Find the car to work with.
return movieView;
}
}
}
The populateListView() method is called from an AsyncTask called AsyncMoviesGet, whose postExecute() looks like this.
#Override
protected void onPostExecute(ArrayList<Movie> result) {
if(result != null){
m.thisMovies = result;
m.populateListView();
Log.d("demo", result.toString());
} else{
Log.d("demo", "null result");
}
}
You can see the Log messages inside the populateListView() method. These 3 statements get executed. However, the setAdapter() function doesn't seem to call the "GetView" function.
Add stuff to ArrayAdapter at least, use mAdapter.addAll():
private MoviesListAdapter mAdapter;
. . . . . .
#Override
protected void onCreate(Bundle savedInstanceState) {
. . . . .
mAdapter = new MoviesListAdapter();
moviesDisplay.setAdapter(mAdapter);
}
. . . . .
protected void onPostExecute(ArrayList<Movie> result) {
if(result != null){
m.thisMovies = result;
mAdapter.clear();
mAdapter.addAll(result);
mAdapter.notifyDatasetInvalidated();
Log.d("demo", result.toString());
} else{
Log.d("demo", "null result");
}
}
You have to add a constructor to your Custom Adapter class, one that takes a context, resource id & a data structure containing the items you'd like to display, in this case your thisMovies.
public MoviesListAdapter(Context context, int resourceId,
ArrayList<String> viewItems)
{
super(context, resourceId, viewItems);
}
Then you have to construct your adapter based on the results you've received in your onPostExecute(). Before this is done, create an instance variable in the activity which will be used to store the adapter.
MoviesListAdapter mMovieAdapter = null;
Afterwards, construct it by changing this
moviesDisplay.setAdapter(new MoviesListAdapter());
to something like below
if(thisMovies != null && thisMovies.size() > 0) {
mMovieAdapter = new MoviesListAdapter(getApplicationContext(), R.layout.movies_activity_listview, thisMovies);
moviesDisplay.setAdapter(mMovieAdapter);
} else {
Log.w("MyApplication","onPostExecute() did not return any items!");
}
You should initialise and call adapter by this way..........this is just an e.g. you need to modify it according to your controls:
ListView lv = (ListView) v.findViewById(R.id.listview1);
ListViewAdapter adapter = new ListViewAdapter(container.getContext(),
android.R.layout.simple_list_item_1, R.id.textview1);
adapter.notifyDataSetChanged();
lv.setAdapter(adapter);
And inside the custom adapter class, its constructor should also have these kind of parameters:
public ListViewAdapter(Context context,int resource, int textViewResourceId, List<YourObjectType> items) {
super(context,resource,textViewResourceId, items);
this.context1 = context;
// TODO Auto-generated constructor stub
}

Categories

Resources