0

My Fragment---> FragmnetOne

package com.example.jerry.myapp;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import java.util.ArrayList;
import android.widget.ArrayAdapter;

public class FragmentOne extends Fragment {

ArrayList<String> bookList;

public static Fragment newInstance(Context context) {
    FragmentOne f = new FragmentOne();
    return f;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_one, null);
    inflatebookList(root);
    return root;
}

void inflatebookList(ViewGroup root){
    ListView lv = (ListView) root.findViewById(R.id.listView_bookList);
    bookList = new ArrayList<String>();
    getBookNames();
    ArrayAdapter<String> arrayAdapter =
            new ArrayAdapter<String>(getActivity(),R.layout.simple_list_item_1,bookList);
    lv.setAdapter(arrayAdapter);

   /* bookList.setOnItemClickListener(new OnItemClickListener()
    {
        // argument position gives the index of item which is clicked
        public void onItemClick(AdapterView<?> arg0, View v,int position, long arg3)
        {
            String selectedBook=bookList.get(position);
            Toast.makeText(getApplicationContext(), "Book Selected : "+selectedBook,   Toast.LENGTH_LONG).show();
        }
    });*/
}

void getBookNames()
{
    bookList.add("Book 1");
    bookList.add("Book 2");
    bookList.add("Book 3");        
}

}

And My xml fragment_one.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/listView_bookList"
    android:layout_weight="1" />
</LinearLayout>

Also there is an error after "error: cannot find symbol variable listView_bookList" saying: no suitable constructor found for ArrayAdapter(FragmentOne,int,ArrayList) (actual and former arguments list differ in length) Any help will be appreciated

Error:

Error:(35, 64) error: cannot find symbol variable simple_list_item_1
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Error:Execution failed for task ':app:compileDebugJava'.

Compilation failed; see the compiler error output for details.

Jerry
  • 410
  • 6
  • 17
  • `ArrayAdapter` is looking for a Context for the first parameter, not a Fragment. – Dan Harms Jul 06 '15 at 18:34
  • So what should I replace the Fragment with? – Jerry Jul 06 '15 at 18:38
  • I did that, but the first error still remains i.e., cannot find symbol variable listView_bookList – Jerry Jul 06 '15 at 18:40
  • `android.R.id.listView_bookList` -> `R.id.listView_bookList` and import the R that matches your package name – njzk2 Jul 06 '15 at 18:51
  • Thank you for adding your imports. Can you see if the changes listed below fix your issue? – Derek Jul 08 '15 at 10:34
  • The imports were the ones causing the problem, but its now working and i did not have to import R specifically...but it now works and thanks to all – Jerry Jul 08 '15 at 11:09

1 Answers1

2

You've shown two errors, one now removed by an edit to the question.

Using resources you've created

One error was a failure to find android.R.id.listView_bookList when binding the view using findViewById.

Change the view lookup to:

ListView lv = (ListView) root.findViewById(R.id.listView_bookList);

Add the import:

import com.example.jerry.myapp.R;

Using resources supplied by the Android SDK

The second error is "cannot find symbol variable simple_list_item_1". This is caused because you removed the android prefix to R.layout.simple_list_item_1.

Use this to instantiate the ArrayAdapter:

ArrayAdapter<String> arrayAdapter =
        new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, bookList);

The use of android.R.layout.simple_list_item_1 in the ArrayAdapter creation is correct as it refers to a layout supplied in the Android SDK. You can read more here: What is "android.R.layout.simple_list_item_1"?

Community
  • 1
  • 1
Derek
  • 1,572
  • 1
  • 14
  • 25
  • It should be `R.id.listView_bookList` not `android.R...` – Dan Harms Jul 06 '15 at 18:50
  • No, R is looking at your list of generated ids, whereas android.R is looking in the list of android's sdk ids. You defined the id, so it needs to be referenced from your R file. – Dan Harms Jul 06 '15 at 18:52
  • @dharms: well the problem persists even after R.id.listView_bookList – Jerry Jul 06 '15 at 18:54
  • I apologize, I typed too quickly there. Thank you dharms. Jerry, can you please paste the entire error into your question? – Derek Jul 06 '15 at 18:56
  • @Derek: Are there any more imports to be made or packages to be included? – Jerry Jul 06 '15 at 19:19
  • The two relevant ones here are the two `R` files, the android one for `simple_list_item_1` and your own one which is auto generated by the build process which has the IDs defined in your xml. – Derek Jul 06 '15 at 19:24