Reputation: 11
I am new to Android and looking for some help. I am making a get request which returns a list as a string. For each entry in this list I want to add a card to a linearLayout in a horizontal scrollview within my fragment and set the text of the card to be the entry in the list.
I can use my .java file to add cards in onCreateView function, and also use findViewById to access and change the text of existing textViews in the PostExecute part of my AsyncTask, however when I try to create or add cards to the linearlayout in the PostExecute part of my AsyncTask my app crashes. I have tried so many different ways and cannot get it to work.
package com.example.theclassplan;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.os.AsyncTask;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import java.io.IOException;
import okhttp3.Response;
/**
* A simple {@link Fragment} subclass.
* Use the {@link HomeFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class HomeFragment extends Fragment {
static String data;
static View view;
static TextView text;
static LinearLayout upcomingplans;
static LinearLayout featuredplans;
static View card1;;
static View card2;;
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
public HomeFragment() {
// Required empty public constructor
}
public static HomeFragment newInstance(String param1, String param2) {
HomeFragment fragment = new HomeFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
Log.d("on create", String.valueOf(Thread.currentThread().getId()));
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_home, container, false);
upcomingplans = (LinearLayout) view.findViewById(R.id.upcomingplans);
featuredplans = (LinearLayout) view.findViewById(R.id.featuredplans);
for (int i =0; i<5; i++){ //will let me add cards here
card1 = getLayoutInflater().inflate(R.layout.card,null);
card2 = getLayoutInflater().inflate(R.layout.card,null);
upcomingplans.addView(card1);
featuredplans.addView(card2);
}
class ExampleAsync extends AsyncTask<Integer, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// ...
}
@Override
protected String doInBackground(Integer... integers) {
String id = Homepage.id;
String url = "http://localhost:10000/playlist?id=" + id;
Request request = new Request.Builder()
.url(url)
.build();
OkHttpClient client = new OkHttpClient();
Call call = client.newCall(request);
Log.d("on create view in thread", String.valueOf(Thread.currentThread().getId()));
String serverResponse = null;
try {
Response response = call.execute();
serverResponse = response.body().string();
} catch (IOException e) {
e.printStackTrace();
}
// ...
return serverResponse;
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
// ...
}
@Override
protected void onPostExecute(String string) {
super.onPostExecute(string);
Log.i("response",string);
String[] playlists = string.split("]");
for (int i = 0; i < 5 ; i++) {
playlists[i] = playlists[i].replaceAll("\\[", "");
/*i want to create cards here - this is what i tried but app crashes:
card1 = getLayoutInflater().inflate(R.layout.card,null);
Textview title = card1.findViewById(R.id.title)
title.setText(playlists[i])
upcomingplans.addView(card1);
*/
}
// ...
}
}
new ExampleAsync().execute();
return view;
}
}
Upvotes: 1
Views: 47