Reputation: 241
I trying to build an app with 4 tabs. Every tab has a different fragment linked to it. The issue is that I want to make a custom listView for each fragment, but it ends with some unsolvable error... I have talked to other developers, but I still can't make one that works! It's really frustrating!
I have:
These are normal errors I get:
One of the guides I'm trying to understand and use:
This is my 1st fragment:
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;
public class Fragment1test extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
//This layout contains your list view
View view = inflater.inflate(R.layout.fragment1, container, false);
//now you must initialize your list view
ListView yourListView = (ListView)view.findViewById(R.id.ListView1);
ListView.setAdapter(new ListAdapter());
return view;
}
}
My ListAdapter.java code (from a tutorial):
import java.util.List;
import android.content.ClipData.Item;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class ListAdapter extends ArrayAdapter<Item> {
public ListAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
// TODO Auto-generated constructor stub
}
private List<Item> items;
public ListAdapter(Context context, int resource, List<Item> items) {
super(context, resource, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.list_design, null);
}
Item p = items.get(position);
if (p != null) {
TextView tt = (TextView) v.findViewById(R.id.game_txtTitle);
TextView tt1 = (TextView) v.findViewById(R.id.game_txtRelease);
TextView tt3 = (TextView) v.findViewById(R.id.game_txtPlatform);
if (tt != null) {
tt.setText(p.getId());
}
if (tt1 != null) {
tt1.setText(p.getCategory().getId());
}
if (tt3 != null) {
tt3.setText(p.getDescription());
}
}
Upvotes: 3
Views: 42302
Reputation: 8081
public class Fragment1test extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
//This layout contains your list view
View view = inflater.inflate(R.layout.fragment_basic, container, false);
//now you must initialize your list view
ListView listview =(ListView)view.findViewById(R.id.your_listview);
//EDITED Code
String[] items = new String[] {"Item 1", "Item 2", "Item 3"};
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, items);
listview.setAdapter(adapter);
//To have custom list view use this : you must define CustomeAdapter class
// listview.setadapter(new CustomeAdapter(getActivity()));
//getActivty is used instead of Context
return view;
}
}
Refer this link & question to know how to create custom adapter
Note : do not use List fragment or List activity to create custom listview
EDIT
ListView yourListView = (ListView)view.findViewById(R.id.ListView1);
//Here items must be a List<Items> according to your class instead of String[] array
ListAdapter listadapter = new ListAdapter(getActivity(), android.R.layout.simple_list_item_1, items)
ListView.setAdapter( listAdapter);
Upvotes: 12
Reputation: 11
public class fragmentpassword extends Fragment {
String[] name={"A","B","C","D"};
int [] image={R.drawable.ic_drawer,R.drawable.ic_drawer,R.drawable.ic_drawer,R.drawable.ic_drawer};
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.layoutpassword, container, false);
ListView li=(ListView)v.findViewById(R.id.listViewPassword);
li.setAdapter(new PasswordAdapter(getActivity(),R.layout.passwordlay,name));
return v;
}
class PasswordAdapter extends ArrayAdapter {
public PasswordAdapter(Context context, int resource, String[] objects) {
super(context, resource, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v=((Activity)getContext()).getLayoutInflater().inflate(R.layout.passwordlay,null);
TextView txt1 = (TextView) v.findViewById(R.id.textViewpasslay);
txt1.setText(name[position]);
ImageView img = (ImageView) v.findViewById(R.id.imageViewpasslay);
img.setBackgroundResource(image[position]);
return v;
}
}
}
**XML for customlist: passwordlay.xml**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageViewpasslay" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/textViewpasslay"/>
</LinearLayout>
**XML for fragment: layoutpassword.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="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listViewPassword"/>
</LinearLayout>
Upvotes: 1
Reputation: 3910
Fragments don't use setContentView(int)
, so you can't use that, use the inflater
in public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
and return the view inflated. As for findViewById(int)
do it like this getActivity().findViewById(int)
.
Upvotes: 0