在ListItem中无法应用于java.lang.String android

时间:2017-11-02 16:24:06

标签: java android json android-recyclerview listitem

在这种情况下,当我尝试向ListItem添加字符串时出现问题,我按照tutorial进行此操作。我完全按照视频告诉我的那样做了,但它仍然让我得到错误'在ListItem中无法应用于java.lang.String'而且我不明白为什么。这是我的代码:

public class ListScientistsFragment extends Fragment {


private final String TAG = "ListScientistsFragment";
private static final String URL_DATA = "http://jsonplaceholder.typicode.com/photos";

private String[] scientistNames = {"Marie Curie","Thomas Edison","Albert Einstein","Michael Faraday","Galileo Galilei",
        "Stephen Hawking","Johannes Kepler","Issac Newton","Nikola Tesla"};
private String[] birthYear = {"1867","1847","1879","1791","1564","1942","1571","1643","1856"};
private String[] deathYear = {"1934","1931","1955","1867","1642","Present","1630","1727","1943"};
private int[] image = {R.drawable.curie,R.drawable.edison,R.drawable.einstein,R.drawable.faraday,R.drawable.galileo,R.drawable.hawking,R.drawable.kepler,R.drawable.newton,R.drawable.tesla};

private ArrayList<Scientist> mScientists;
private RecyclerView mScientistRecyclerView;
private List<LauncherActivity.ListItem> listItems;
private ScientistAdapter mAdapter;
public ImageView mImageView;

@Override
public void onCreate(Bundle savedInstanceState) {
    mScientists = new ArrayList<>();
    listItems = new ArrayList<>();
    for(int i =0;i<scientistNames.length;i++){
        Scientist s = new Scientist();
        s.setName(scientistNames[i]);
        s.setBirthYear(birthYear[i]);
        s.setDeathYear(deathYear[i]);
        mScientists.add(s);
    }
    loadImagesToView();
    super.onCreate(savedInstanceState);
}

private void loadImagesToView() {
    final ArrayList<String> list = new ArrayList<String>();
    StringRequest stringRequest = new StringRequest(Request.Method.GET,
            URL_DATA,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        JSONArray jsonarray = new JSONArray(response);
                        for(int i=0;i<jsonarray.length();i++) {
                            JSONObject o = jsonarray.getJSONObject(i);
//HERE'S MY PROBLEM ------------------------------------
                                LauncherActivity.ListItem item;
                                item = new LauncherActivity.ListItem(
                                        o.getString("url")
                                );
//--------------------------------------------
                            listItems.add(item);
                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            }
    );

我只想添加每个&#34;网址&#34;我从特定的URL_DATA获取,这是一个带有其他字段的JSON,但我需要专门的URL。我将把它加载到RecyclerView中。希望你明白。

1 个答案:

答案 0 :(得分:0)

类LauncherActivity.ListItem没有带String参数的构造函数。 https://developer.android.com/reference/android/app/LauncherActivity.ListItem.html