I am new in Android and I have a ListFragment that can successfully show the items (only text) in the list.
//More.class
public class More extends ListFragment{
private ListView mListView;
String android_versions[] = new String[]{"1","2","3","4",};
@Override
public void onActivityCreated(Bundle savedInstanceState) {
ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(getActivity().getBaseContext(), android.R.layout.simple_list_item_1, android_versions);
mListView.setAdapter(mAdapter);
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.more_listview, container, false);
mListView = (ListView)view.findViewById(android.R.id.list);
return view;
}
}
This is my Adapter class
//ImageAdapter.class
public class ImageAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public ImageAdapter(Context context, String[] values) {super(context, R.layout.more_item, values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.more_item, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
textView.setText(values[position]);
// Change icon based on name
String s = values[position];
System.out.println(s);
if (s.equals("About App")) {
imageView.setImageResource(R.drawable.more_about);
} else if (s.equals("Tools Tutorial")) {
imageView.setImageResource(R.drawable.more_about);
} else if (s.equals("EnfaMama A+ Club")) {
imageView.setImageResource(R.drawable.more_app);
} else if (s.equals("App Technical Contact")) {
imageView.setImageResource(R.drawable.more_app);
}
return rowView;
}
}
and this is my settings for the text and image
//more_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/icon"
android:contentDescription="@string/image"
android:layout_width="80dp"
android:layout_height="80dp"
android:padding="10dp"
android:src="@drawable/tool_settings" >
</ImageView>
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@+id/label"
android:textSize="20dp"
android:textStyle="bold" /></LinearLayout>
I want to achieve two things here.
1) I have a custom adapter that contains an Image and a Text and I would like to use this adapter. How do I integrate it into More.class?
ImageAdapter = new ImageAdapter(this, android_versions);
but i get constructor undefined.
2) Correct me if I am wrong, as I understand android.R.layout.simple_list_item_1 is the default android list, and I would like to use my own custom layout which is R.layout.more_item.
What should I do? What did I do wrong?
This is my first post, and I apologized if I asked a silly question. I hope I am clear enough to make you understand what I want. I have very low knowledge in Android, so I hope someone could guide me how to do it here. Thanks in advance.
ImageAdapter = new ImageAdapter(this, android_versions);
but i get constructor undefined.
You need to use getActivity()
instead of this
, since you are inside a fragment (not in activity)
I tried replacing R.layout.simple_list_item_1 with R.layout.more_item but my application crash.
you cannot use your custom layout with default ArrayAdapter directly. If that crash occur with ImageAdapter
, post that crash log.
try like this
ImageAdapter mAdapter = new ImageAdapter(getActivity(), android_versions);
mListView.setAdapter(mAdapter);