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.