I have a problem with callback an activity
, as the main activity. My story is that I get a list of branches from an `API then I pass the list back to activity to make a RecyclerView.
I should say that I don't have a problem with the TextView
, for example, to show one item, but I get this error when debugging this line:
branchRecyclerView.setAdapter(branchAdapter);
The error is:
Only the original thread that created a view hierarchy can touch its views.
EDIT:
There is a solution by using Runnable
method. But I don't know how to use it because of these reasons:
I use
WebApi
to get information by using OkHttp.OnResponse
of this library return void, so I should callback a method in Activity_Main to create a list.For solving No.1, I pass Main_Activity variable as a constructor parameter to this class.
I want to use
Runnable
as a solution inactivity_main
to create object from my API class and call my APIThat constructor in No.3 only accept parameter as a
Runnable
class! So I can't passthis
to it and I have to pass nothing! So I get this error: incompatible types: < anonymous Runnable > cannot be converted to MainActivity
My codes (with some reduction):
Main_Activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
CentralAPIs api = new CentralAPIs();
api.run();
} catch (Exception e1) {
e1.printStackTrace();
}
}
public void generateBranchList(List<BranchModel> branches) {
RecyclerView branchRecyclerView;
RecyclerView.Adapter branchAdapter;
RecyclerView.LayoutManager branchLayoutManager;
branchRecyclerView = (RecyclerView) findViewById(R.id.branchRecycleView);
try {
branchLayoutManager = new LinearLayoutManager(this);
branchRecyclerView.setLayoutManager(branchLayoutManager);
} catch (Exception ex) {
ex.printStackTrace();
}
}
CentralAPI
MainActivity activity;
public CentralAPIs(MainActivity activity){
this.activity = activity;
}
public void onResponse(Call call, Response response) throws IOException {
if (!response.isSuccessful())
throw new IOException("Unexpected code " + response);
Headers responseHeaders = response.headers();
for (int i = 0, size = responseHeaders.size(); i < size; i++) {
System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
}
Gson gson = new GsonBuilder()
.setLenient()
.create();
try {
List<BranchModel> branches = gson.fromJson(response.body().charStream(),ArrayList.class);
//********** HERE CALLBACK ACTIVITY METHOD ****************
activity.generateBranchList(branches);
} catch (Exception ex) {
ex.printStackTrace();
}
Adapter
public class BranchAdapter extends RecyclerView.Adapter<BranchAdapter.BranchViewHolder>{
private List<BranchModel> branchList;
public static class BranchViewHolder extends RecyclerView.ViewHolder {
public View currentView;
public BranchViewHolder(View currentView) {
super(currentView);
this.currentView = currentView;
}
}
public BranchAdapter(List<BranchModel> branchList) {
this.branchList = branchList;
}
@Override
public BranchViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.activity_main, viewGroup, false);
BranchViewHolder pvh = new BranchViewHolder(v);
return pvh;
}
@Override
public int getItemCount() {
return branchList.size();
}
}