Reputation: 294
I want to Add Simple List View of some items under Fragment
activity which is extending Fragment
but not ListFragment
.
Here is My code :
public class ProductListFragment extends Fragment {
String[] ibizzproducts=
{
"Attendance",
"GPS",
"CCTV"
"Website"
"Application"
};
ListView listView;
public static ProductListFragment newInstance(int sectionNumber) {
ProductListFragment fragment = new ProductListFragment();
Bundle args = new Bundle();
args.putInt(Constants.ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public ProductListFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
for(int i=0;i < ibizzproducts.length;i++){
HashMap<String, String> hm = new HashMap<String,String>();
hm.put("txt", "Products Here : " + ibizzproducts[i]);
aList.add(hm);
}
String[] from = { "Products Here" };
int[] to = { R.id.listView1};
View v = inflater.inflate(R.layout.fragment_product_list, container, false);
// ListView list = (ListView)v.findViewById(R.id.listView1);
listView = (ListView)v.findViewById(R.id.listView1);
SimpleAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.fragment_product_list, from, to);
listView.setAdapter(adapter);
return v;
If I am using this in onCreateView()
, thn its giving me error:
java.lang.IllegalStateException: android.widget.ListView is not a view that can be bounds by this SimpleAdapter
I just want to add simple ListView
, so that if i select Product
from the navigation Drawer, it should show me this simple List of product items.
please , help me with this. Thanks
Upvotes: 0
Views: 448
Reputation: 981
i hope it will help you
public class FragmentShowAll extends Fragment {
ListView listViewMsg;
ArrayList<String> array_list=new ArrayList<>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_fragment_all, container, false);
array_list.add("hello");
array_list.add("hello");
array_list.add("hello");
array_list.add("hello");
array_list.add("hello");
listViewMsg = (ListView) v.findViewById(R.id.listViewMsg);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,array_list);
listViewMsg.setAdapter(adapter);
listViewMsg.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//on click action
}
});//listview on click
return v;
}
}
Upvotes: 1
Reputation: 3212
Change your code from listView.setAdapter(adapter);
to list.setAdapter(adapter);
Keep your eyes open while copy-paste is being done
Upvotes: 2
Reputation: 132982
Here:
listView.setAdapter(adapter);
listView
object is null
because getActivity().findViewById
is used to access ListView from fragment_product_list
layout which is layout of Fragment.
Because list
object of ListView is initialized from fragment_product_list
which is returned from onCreateView
method so used it to setAdapter
for ListView
:
list.setAdapter(adapter);
Upvotes: 2
Reputation: 2391
It is better:
for(int i = 0; i < ibizzproducts.length; i++) {
HashMap<String, String> hm = new HashMap<String,String>();
hm.put("txt", "Products Here : " + ibizzproducts[i]);
aList.add(hm);
}
Upvotes: 1