Is it possible to return peepWithPics after sending it to my XMLPullParserHandler so I can call it onCreate and set it to a listView adapter. I am not sure how to pass it to any other method or class for us. Calling the listViewAdapter works in this method but I am unable to get setOnItemClickLIstener to work so thinking if I can create the adapter in the onCreate and then setOnItemCLickLister, should help me with me move forward with my development.
Also with OkHttp3 I read that doing the call with enqueue was doing so asynchronously yet I get an error.
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. This error is at the code line
listView.setAdapter(adapter);
The whole method is as
public void getXMLData() {
OkHttpClient client = getUnsafeOkHttpClient();
Request request = new Request.Builder()
.url(myURL)
.build();
client.newCall(request).enqueue(new Callback() {
// android.os.Handler mainHanlder = new android.os.Handler(context.getMainLooper());
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final String responseData = response.body().string();
final InputStream stream = new ByteArrayInputStream(responseData.getBytes());
/*mainHanlder.post(new Runnable() {
@Override
public void run() {*/
ArrayList<PeepWithPic> peepWithPics;
XMLPullParserHandler parserHandler = new XMLPullParserHandler();
peepWithPics = (ArrayList<PeepWithPic>) parserHandler.parse(stream);
/*ListViewAdapter adapter = new ListViewAdapter(getApplicationContext(), peepWithPics);
listView.setAdapter(adapter);
listView.setVisibility(View.VISIBLE);
mProgressBar.setVisibility(View.GONE);*/
/*}
});*/
}
});
}
I have tried adding after mProgressBar is set to gone.
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(getApplicationContext(), "WORKS FROM RUNONUI", Toast.LENGTH_LONG).show();
}
I have also tried the same setOnItemClickListener in the onCreate method
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
context = getApplicationContext();
listView = (ListView) findViewById(R.id.contactListView);
mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
mProgressBar.setVisibility(View.VISIBLE);
getXMLData();
}
The getXMLData method is called in the onCreate