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:
- A MainActivity class that works, it uses swipe-able tabs
- An XML with the design I want on my custom ListView.
- An XML called fragment1 with a ListView.
These are normal errors I get:
- "The method findViewById(int) is undefined for the type Fragment1UG"
- "The method setContentView(int) is undefined for the type Fragment1UG. 1 quick fix available: Create method 'setContentView()'"
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());
}
}