I am trying to create a search bar on android in which urls are fetched from azure blob storage. I want to display the video list after the search query. But I am unable to do so.
Below is the code along with error
class SearchMovies extends AsyncTask<String, String, ArrayList<String>> {
String query;
String url;
String urlOne;
String itemOne;
ArrayList<String> list;
ArrayAdapter<String> adapter;
Context context;
ListView listView;
@Override
protected void onPostExecute(ArrayList<String> s) {
Log.i("###",s.toString());
runOnUiThread(new Runnable() {
@Override
public void run() {
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.i("###", "BEFORE");
itemOne = (String) parent.getItemAtPosition(position); //null
Log.i("###AFTER", itemOne);
}
});
}
});
/* Intent intent = new Intent(MainActivity.this,Video.class);
intent.setData(Uri.parse("https://moviestarstorage.blob.core.windows.net/action-movies/ant.mp4"));
startActivity(intent);*/
/*Intent intent = new Intent(context,Video.class);
intent.setData(Uri.parse(itemOne));
startActivity(intent);*/
super.onPostExecute(s);
}
public SearchMovies(String passQuery, ListView passListView, Context passContext) {
this.query = passQuery;
this.context = passContext;
this.listView = passListView;
}
@Override
protected ArrayList<String> doInBackground(String... params) {
try {
HttpHandler httpHandler = new HttpHandler();
Log.i("###URLFromAPI", "Entered");
url = "http://13.71.112.116/?keyword=" + query; // This is the API call
String jsonstr = httpHandler.makeServiceCall(url); //
Log.i("###URLFromAPI", "Entered");
JSONObject jsonObject = new JSONObject(jsonstr);
urlOne = jsonObject.getString("urls"); // urls are being fetched and logged on the screen
String[] urlArray = urlOne.split(","); //seperating the url
list = new ArrayList<String>();
for(int i=0;i<3;i++)
{
list.add(i,urlArray[i]);
Log.i("###FROMLOOP",list.get(i));
}
adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_expandable_list_item_1,list);
listview.setAdapter(adapter);
/*runOnUiThread(new Runnable() {
//String item;
@Override
public void run() {
adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_expandable_list_item_1,list);
Log.i("###","hello");
listview.setAdapter(adapter);
Log.i("###","hello");
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.i("###","BEFORE");
itemOne = (String) parent.getItemAtPosition(position);
Log.i("###AFTER", itemOne);
}
});
}
});*/
/* Log.i("###",urlOne);
if (urlOne == "") {
Log.i("###", "URL is null");
}
else
Log.i("###URLFromAPI", urlOne.toString());*/
}
catch (Exception e){
e.printStackTrace();
}
return list;
}
}
Below is the error.
com.base.vrushali.collapsingtoolbar W/System.err: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 03-31 11:50:37.005 26602-27033/com.base.vrushali.collapsingtoolbar W/System.err: at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:7988) 03-31 11:50:37.005 26602-27033/com.base.vrushali.collapsingtoolbar W/System.err: at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:1392) 03-31 11:50:37.005 26602-27033/com.base.vrushali.collapsingtoolbar W/System.err: at android.view.ViewGroup.invalidateChild(ViewGroup.java:5426) 03-31 11:50:37.005 26602-27033/com.base.vrushali.collapsingtoolbar W/System.err: at android.view.View.invalidateInternal(View.java:14959) 03-31 11:50:37.006 26602-27033/com.base.vrushali.collapsingtoolbar W/System.err: at android.view.View.invalidate(View.java:14923) 03-31 11:50:37.006 26602-27033/com.base.vrushali.collapsingtoolbar W/System.err: at android.view.View.invalidate(View.java:14907) 03-31 11:50:37.006 26602-27033/com.base.vrushali.collapsingtoolbar W/System.err: at android.widget.AbsListView.resetList(AbsListView.java:2791) 03-31 11:50:37.006 26602-27033/com.base.vrushali.collapsingtoolbar W/System.err: at android.widget.ListView.resetList(ListView.java:592) 03-31 11:50:37.006 26602-27033/com.base.vrushali.collapsingtoolbar W/System.err: at android.widget.ListView.setAdapter(ListView.java:533) 03-31 11:50:37.006 26602-27033/com.base.vrushali.collapsingtoolbar W/System.err: at com.base.vrushali.collapsingtoolbar.MainActivity$SearchMovies.doInBackground(MainActivity.java:356) 03-31 11:50:37.007 26602-27033/com.base.vrushali.collapsingtoolbar W/System.err: at com.base.vrushali.collapsingtoolbar.MainActivity$SearchMovies.doInBackground(MainActivity.java:282) 03-31 11:50:37.007 26602-27033/com.base.vrushali.collapsingtoolbar W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:305) 03-31 11:50:37.007 26602-27033/com.base.vrushali.collapsingtoolbar W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237) 03-31 11:50:37.007 26602-27033/com.base.vrushali.collapsingtoolbar W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243) 03-31 11:50:37.007 26602-27033/com.base.vrushali.collapsingtoolbar W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) 03-31 11:50:37.007 26602-27033/com.base.vrushali.collapsingtoolbar W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) 03-31 11:50:37.007 26602-27033/com.base.vrushali.collapsingtoolbar W/System.err: at java.lang.Thread.run(Thread.java:762) 03-31 11:51:36.672 26602-26602/com.base.vrushali.collapsingtoolbar W/IInputConnectionWrapper: finishComposingText on inactive InputConnection 03-31 11:51:36.795 26602-26602/com.base.vrushali.collapsingtoolbar W/IInputConnectionWrapper: getCursorCapsMode on inactive InputConnection 03-31 11:51:36.865 26602-26602/com.base.vrushali.collapsingtoolbar W/IInputConnectionWrapper: getCursorCapsMode on inactive InputConnection 03-31 11:51:36.958 26602-26602/com.base.vrushali.collapsingtoolbar W/IInputConnectionWrapper: getSelectedText on inactive InputConnection 03-31 11:51:36.959 26602-26602/com.base.vrushali.collapsingtoolbar W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection 03-31 11:51:36.960 26602-26602/com.base.vrushali.collapsingtoolbar W/IInputConnectionWrapper: getTextAfterCursor on inactive InputConnection 03-31 11:52:04.413 1678-8196/? W/ActivityManager: Duplicate finish request for ActivityRecord{947dd63d0 u0 com.base.vrushali.collapsingtoolbar/.MainActivity t22054 f}
runOnUiThread
not required in onPostExecute
it's only require when you updating View
from doInBackground
protected void onPostExecute(ArrayList<String> s) {
super.onPostExecute(s);
Log.i("###",s.toString());
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.i("###", "BEFORE");
itemOne = (String) parent.getItemAtPosition(position); //null
Log.i("###AFTER", itemOne);
}
});
}
If you are updating view in doInBackground
@Override
protected ArrayList<String> doInBackground(String... params) {
try {
HttpHandler httpHandler = new HttpHandler();
Log.i("###URLFromAPI", "Entered");
url = "http://13.71.112.116/?keyword=" + query; // This is the API call
String jsonstr = httpHandler.makeServiceCall(url); //
Log.i("###URLFromAPI", "Entered");
JSONObject jsonObject = new JSONObject(jsonstr);
urlOne = jsonObject.getString("urls"); // urls are being fetched and logged on the screen
String[] urlArray = urlOne.split(","); //seperating the url
list = new ArrayList<String>();
for(int i=0;i<3;i++)
{
list.add(i,urlArray[i]);
Log.i("###FROMLOOP",list.get(i));
}
runOnUiThread(new Runnable() {
//String item;
@Override
public void run() {
adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_expandable_list_item_1,list);
Log.i("###","hello");
listview.setAdapter(adapter);
Log.i("###","hello");
});
/* Log.i("###",urlOne);
if (urlOne == "") {
Log.i("###", "URL is null");
}
else
Log.i("###URLFromAPI", urlOne.toString());*/
}
catch (Exception e){
e.printStackTrace();
}
return list;
}