I'm working with Fragments in my current app. I tried using a Spinner in one of the fragments, to sort the listview below it in a specific format. The Adapter is set correctly, but I'm unable to open the dropdown on touch to select the choices. How I know the adapter is set correctly, is that in the AVD, I'm able to navigate to it using the keyboard and open it.
Code:
public class Upcoming_Reminders extends Fragment {
private View view;
private ProgressDialog progressDialog;
private ListView listView;
private String TAG = "Loggin";
RemindersAdapter remindersAdapter;
private Spinner mSortSpinner;
private String[] test;
public Upcoming_Reminders() {
// Required empty public constructor
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mSortSpinner = (Spinner) view.findViewById(R.id.sort_spinner);
ArrayAdapter<CharSequence> mSortAdapter = new ArrayAdapter<CharSequence>(getContext(), android.R.layout.simple_spinner_item, test);
mSortAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSortSpinner.setAdapter(mSortAdapter);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_upcoming__reminders, container, false);
test = getActivity().getResources().getStringArray(R.array.sort_by);
listView = (ListView) view.findViewById(R.id.contracts_list);
return view;
}
I tried putting it in the OnCreateView method as well, below the view assignment. It still isn't clickable.
The XML
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.onerooftechnologiesamc.Upcoming_Reminders">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingTop="70dp">
<TextView
android:id="#+id/te"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="bottom|end"
android:text="#string/sort_by" />
<Spinner
android:id="#+id/sort_spinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
/>
</LinearLayout></FrameLayout>
What could be the cause? I tried Googling a lot, haven't found someone with the exact same problem as me.
Thank you
Edit 1: The Resources file
<resources>
<array name="sort_by">
<item>Option 1</item>
<item>Option 2</item>
<item>Option 3</item>
<item>Option 4</item>
</array></resources>
public class SpinnerFragment extends Fragment {
private Spinner spinner;
private String[] test = {"1","2","3","4","5"};
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_spinner,container,false);
spinner = (Spinner) view.findViewById(R.id.spinner);
ArrayAdapter<CharSequence> mSortAdapter = new ArrayAdapter<CharSequence>(getActivity(), android.R.layout.simple_spinner_item, test);
mSortAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(mSortAdapter);
return view;
}
}
Try this:
public class SpinnerAdapter extends ArrayAdapter<String>
{
private Activity context;
String[] data = null;
public SpinnerAdapter(Activity context, int resource,
String[] data2)
{
super(context, resource, data2);
this.context = context;
this.data = data2;
}
...
#Override
public View getDropDownView(int position, View convertView,
ViewGroup parent)
{
View row = convertView;
if(row == null)
{
//inflate your customlayout for the textview
LayoutInflater inflater = context.getLayoutInflater();
row = inflater.inflate(R.layout.spinner_layout, parent, false);
}
//put the data in it
String item = data[position];
if(item != null)
{
TextView text1 = (TextView) row.findViewById(R.id.rowText);
text1.setTextColor(Color.WHITE);
text1.setText(item);
}
return row;
}
...
}
and then set the adapter for the spinner:
Spinner mySpinner = (Spinner) findViewById(R.id.mySpinner);
final String[] data = getResources().getStringArray(
R.array.data);
final ArrayAdapter<String> adapter = new SpinnerAdapter(
MainActivity.this, android.R.layout.simple_spinner_item,
data);
mySpinner.setAdapter(adapter);
The problem seems to have been with FrameLayout. I switched to a RelativeLayout and it started working perfectly.
Related
I have a class that extends the Fragment. I want to add a Spinner but he needs to expand Activity. Can it be done differently?
The following should work if I expanded a class Activity but I can not do in this case.
public class Parameters extends Fragment implements AdapterView.OnItemSelectedListener{
Spinner spinner;
ArrayAdapter<CharSequence> adapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.parameters_tab3, container, false);
return rootView;
spinner = (Spinner) findViewById(R.id.spinner);
adapter = ArrayAdapter.createFromResource(this.getContext(), R.array.strength_class_of_concrete, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
TextView myText = (TextView) view;
Toast.makeText(this, "selected" + myText.getText(), Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.kaminski.bartek.columnec2.MainActivity$PlaceholderFragment">
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="68dp"
android:id="#+id/spinner" />
</RelativeLayout>
.
<resources>
<string name="app_name">Column EC2</string>
<string name="action_settings">Settings</string>
<string-array name="strength_class_of_concrete">
<item>C12/15</item>
<item>C16/20</item>
<item>C20/25</item>
<item>C25/30</item>
<item>C30/37</item>
<item>C35/45</item>
<item>C40/50</item>
<item>C45/55</item>
<item>C50/60</item>
<item>C55/67</item>
<item>C60/75</item>
</string-array>
</resources>
You are exiting the function before it sets up spinner. Write return rootView after setting spinner up. Like this
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.parameters_tab3, container, false);
spinner = (Spinner) rootView.findViewById(R.id.spinner);
adapter = ArrayAdapter.createFromResource(this.getContext(), R.array.strength_class_of_concrete, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
return rootView;
}
EDIT
If you are working with fragments, then view is inflated in onCreate. Now you should find views in that inflated view. Like
spinner = (Spinner) rootView.findViewById(R.id.spinner);
I ve made the adapter but I only need 15 places in the list and I dont have an array to put to int resource, what do I do?
lets say that you want a custom list view that looks like this :
list_look.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"
android:gravity="right"
android:orientation="horizontal" >
<TextView
android:id="#+id/1st_text"
style="#style/liststyle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="2dp"
android:gravity="center"
/>
</LinearLayout>
now you want to fill a ListView in your main.xml that has the list_view id.
the information of your lists is in an ArrayList called data
this will be your adapter:
MyAdapter
public class MyAdapter extends ArrayAdapter<String>
{
private ArrayList<String> data;
public MyAdapter(Context context, ArrayList<String> values)
{
super(context, R.layout.list_look,values);
this.data = values;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = LayoutInflater.from(getContext());
View view = inflater.inflate(R.layout.list_look, parent, false);
String s = data.get(position);
TextView text = (TextView) view.findViewById(R.id.1st_text);
text.setText(s);
return view;
}
now use this adapter in your mainActivity like this :
MainActivity.onCreate()
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> data = getYourOwnDataArray();
ListView list = (ListView) findViewById(R.id.main_list);
MyAdapter ma = new MyAdapter(this, data);
list.setAdapter(ma);
}
and thats it, you can adapt a custom data array that you don't have a resource xml file for it in your ListView
I hope it helps :)
i want to combine two LinearLayout, both have different TextView arrangement in them, in a single ListView. so the final look should be like below:
and i run it with my code, but the app wouldn't start. below are my code.
Activity class:
public class MainActivity extends Activity {
// Create list of items
String[] publicModeItems = {
"AAAAA",
"BBBBB"
};
String[] publicModeParameters = {
"YES",
"NO"
};
String[] publicModeResetExe = {
"CCCCC",
"DDDDD"
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
populateListView();
}
private void populateListView() {
CustomList adapter1 = new CustomList(this, publicModeItems, publicModeParameters);
// Build adapter
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(
this, // context for the activity
R.layout.text_view_test, // layout to use (create)
publicModeResetExe); // items to display
// Configure the list view
ListView list = (ListView) findViewById(R.id.PublicModeListView);
list.setAdapter(adapter1);
list.setAdapter(adapter2);
}
private class CustomList extends ArrayAdapter<String> {
private final Activity context;
private final String[] publicModeItems;
private final String[] publicModeParameters;
public CustomList(Activity context,
String[] publicModeItems,
String[] publicModeParameters) {
super(context, R.layout.text_views_1, publicModeItems);
this.context = context;
this.publicModeItems = publicModeItems;
this.publicModeParameters = publicModeParameters;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater layoutinflater = context.getLayoutInflater();
View rowView = layoutinflater.inflate(R.layout.text_views_1, null, true);
TextView txtPublicModeItems = (TextView) rowView.findViewById(R.id.PublicModeItems);
TextView txtPublicModeParameters = (TextView) rowView.findViewById(R.id.PublicModeParameters);
txtPublicModeItems.setText(publicModeItems[position]);
txtPublicModeParameters.setText(publicModeParameters[position]);
return rowView;
}
}
}
text_views_1 xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/PublicModeLayoutForTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/PublicModeItems"
android:layout_width="200sp"
android:layout_height="wrap_content"
android:textColor="#drawable/text_color_change" />
<TextView
android:id="#+id/PublicModeOpenBracket"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="["
android:textColor="#drawable/text_color_change" />
<TextView
android:id="#+id/PublicModeParameters"
android:layout_width="100sp"
android:layout_height="wrap_content"
android:gravity="end"
android:textColor="#drawable/text_color_change" />
<TextView
android:id="#+id/PublicModeCloseBracket"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="]"
android:textColor="#drawable/text_color_change" />
</LinearLayout>
text_view_test xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#drawable/text_color_change" >
</TextView>
activity_main xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/PublicModeListViewLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/black"
android:baselineAligned="true"
android:orientation="vertical"
tools:context="com.example.mycalendar.MainActivity" >
<LinearLayout
android:id="#+id/PublicModeListViewLayout02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/custom_border"
android:padding="5dp" >
<ListView
android:id="#+id/PublicModeListView"
android:layout_width="308dp"
android:layout_height="wrap_content"
android:choiceMode="singleChoice"
android:listSelector="#color/yellow"
android:smoothScrollbar="true" >
</ListView>
</LinearLayout>
</LinearLayout>
Problem : I just run my code just now, and only 'CCCCC' and 'DDDDD' is showing. so do you have any idea on this?
You should inherit your list adapter from BaseAdapter rather than ArrayAdapter.
Rewrite adapter's getView() method to inflate layout according position, like below.
public View getView(int position, View convertView, ViewGroup parent) {
...
View rootView = null;
if (position == 0 || position == 1) {
rootView = layoutinflater.inflate(R.layout.text_views_1, null, true);
} else {
rootView = layoutinflater.inflate(R.layout.text_views, null, true);
}
...
return rootView;
}
You set the list adapter twice
list.setAdapter(adapter1);
list.setAdapter(adapter2);
in this case the current adapter is the second, not them both.
Use ListView with SimpleAdapter or a custom adapter and that will solve all your problems, check out this for SimpleAdapter tutorial to learn a the basics and this tutorial to learn how you can make multiple views instead of just 1. This tutorial is about creating custom adapter
BTW, I have few comments on your code and xml layout
First, there is no need for 2 TextViews for your bracket, you can easily add them programmatically to the String[] or even to YES/NO TextView.
Second, You're not using a ViewHolder in your CustomList adapter and this is not a good practice as every time you scroll your list, it creates new view although it can use already existing one.
I already solve this problem. but it seems not so convenient to use this method.
public class PublicModeActivity extends Activity {
// Create list of items
String[] publicModeItems = {
"AAAAA",
"BBBBB"
};
String[] publicModeParameters = {
"YES",
"NO"
String[] publicModeResEx = {
"",
"",
"CCCCC",
"DDDDD"
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
populateListView();
}
private void populateListView() {
CustomList adapter = new CustomList(this, publicModeItems, publicModeParameters, publicModeResEx);
// Configure the list view
ListView list = (ListView) findViewById(R.id.PublicModeListView);
list.setAdapter(adapter);
}
private class CustomList extends BaseAdapter {
private final Activity context;
private final String[] publicModeItems;
private final String[] publicModeParameters;
private final String[] publicModeResEx;
public CustomList(Activity context,
String[] publicModeItems,
String[] publicModeParameters,
String[] publicModeResEx) {
super();
this.context = context;
this.publicModeItems = publicModeItems;
this.publicModeParameters = publicModeParameters;
this.publicModeResEx = publicModeResEx;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater layoutinflater = context.getLayoutInflater();
View rowView = null;
Log.i("PublicModeActivity", "" + position);
if (position < 2) {
rowView = layoutinflater.inflate(R.layout.text_views_1, null, true);
TextView txtPublicModeItems = (TextView) rowView.findViewById(R.id.PublicModeItems);
TextView txtPublicModeParameters = (TextView) rowView.findViewById(R.id.PublicModeParameters);
txtPublicModeItems.setText(publicModeItems[position]);
txtPublicModeParameters.setText(publicModeParameters[position]);
} else {
rowView = layoutinflater.inflate(R.layout.text_view_test, null, true);
TextView txtPublicModeResEx = (TextView) rowView.findViewById(R.id.PublicModeResEx);
txtPublicModeResEx.setText(publicModeResEx[position]);
}
return rowView;
}
#Override
public int getCount() {
return 4;
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
}
}
I need to put that blank string "", to match the number of row with the upper layout (text_views_1) even-though those blank string are use in different layout. so it is not so convenient especially when you want to display so many data. If someone here have much simpler method than this. feel free to share with me/us. i'm eager to learn. thank you!
I'm trying to create a contact list with a spinner where you can choose different roles for contacts.
I'm always getting a NullPointerException on adding the spinner to the layout of my contact list (list_item.xml) and I just can't find my mistake(s).
kontakte_fragment - XML File
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="match_parent"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1"
android:background="#ffffff">
<ListView
android:layout_width="fill_parent"
android:layout_height="657dp"
android:id="#+id/listViewKontakte" />
</LinearLayout>
</ScrollView>
The Layout for my List (list_item.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="match_parent"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#ffffff">
<com.github.siyamed.shapeimageview.CircularImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:src="#drawable/screen1"
app:siBorderWidth="2dp"
app:siBorderColor="#color/colorPrimary"
android:layout_gravity="center"
android:id="#+id/kontaktListePic"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/titelKontaktListe"
android:id="#+id/textview1kontakteTitel"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="15dp"
android:layout_alignTop="#+id/kontaktListePic"
android:layout_toRightOf="#+id/kontaktListePic"
android:layout_toEndOf="#+id/kontaktListePic"
android:layout_marginBottom="0dp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/unterTitelKontaktListe"
android:id="#+id/untertitelKontaktListe"
android:textSize="12sp"
android:layout_below="#+id/textview1kontakteTitel"
android:layout_alignLeft="#+id/textview1kontakteTitel"
android:layout_alignStart="#+id/textview1kontakteTitel" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinnerKontakte"
android:layout_alignBottom="#+id/untertitelKontaktListe"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginLeft="250dp"
android:layout_marginTop="15dp"
android:layout_marginBottom="-5dp" />
</RelativeLayout>
KontakteFragment - ActivityClass
public class KontakteFragment extends Fragment {
private ArrayList<String> data = new ArrayList<String>();
Spinner spinner;
ArrayAdapter<CharSequence> adapter;
public static KontakteFragment newInstance() {
KontakteFragment fragment = new KontakteFragment();
return fragment;
}
public KontakteFragment(){
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.kontakte_fragment, container, false);
ListView lv = (ListView) rootView.findViewById(R.id.listViewKontakte);
generateListContent();
lv.setAdapter(new MyListAdapter(getContext(), R.layout.list_item, data));
spinner = (Spinner) rootView.findViewById(R.id.spinnerKontakte);
adapter = ArrayAdapter.createFromResource(getActivity().getBaseContext(), R.array.profile_kontakte, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
return rootView;
}
private void generateListContent() {
for(int i = 0; i < 55; i++){
data.add("This is row number" + i);
}
}
private class MyListAdapter extends ArrayAdapter<String>{
private int layout;
public MyListAdapter(Context context, int resource, List<String> objects) {
super(context, resource, objects);
layout = resource;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder mainViewHolder = null;
if(convertView == null){
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(layout, parent, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.thumbnail = (ImageView) convertView.findViewById(R.id.kontaktListePic);
viewHolder.titel = (TextView) convertView.findViewById(R.id.textview1kontakteTitel);
viewHolder.unterTitel = (TextView) convertView.findViewById(R.id.untertitelKontaktListe);
convertView.setTag(viewHolder);
}
else{
mainViewHolder = (ViewHolder) convertView.getTag();
mainViewHolder.titel.setText(getItem(position));
mainViewHolder.unterTitel.setText(getItem(position));
}
return convertView;
}
}
public class ViewHolder {
ImageView thumbnail;
TextView titel;
TextView unterTitel;
}
} // This is the end of our KontakteFragment Class
You're looking for the Spinner in the Fragment's layout, but it's in the LitsView's elements layout.
You can get a proper reference to your Spinner inside your Adapter's getView method, like so :
Spinner yourSpinner = (Spinner) convertView.findViewById(R.id.spinnerKontakte);
Where did you added Spinner in fragment? you are doing this in wrong manner
public class KontakteFragment extends Fragment {
private ArrayList<String> data = new ArrayList<String>();
ArrayAdapter<CharSequence> adapter;
public static KontakteFragment newInstance() {
KontakteFragment fragment = new KontakteFragment();
return fragment;
}
public KontakteFragment(){
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.kontakte_fragment, container, false);
ListView lv = (ListView) rootView.findViewById(R.id.listViewKontakte);
generateListContent();
lv.setAdapter(new MyListAdapter(getContext(), R.layout.list_item, data));
adapter = ArrayAdapter.createFromResource(getActivity().getBaseContext(), R.array.profile_kontakte, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
return rootView;
}
private void generateListContent() {
for(int i = 0; i < 55; i++){
data.add("This is row number" + i);
}
}
private class MyListAdapter extends ArrayAdapter<String>{
private int layout;
public MyListAdapter(Context context, int resource, List<String> objects) {
super(context, resource, objects);
layout = resource;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder mainViewHolder = null;
if(convertView == null){
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(layout, parent, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.thumbnail = (ImageView) convertView.findViewById(R.id.kontaktListePic);
viewHolder.titel = (TextView) convertView.findViewById(R.id.textview1kontakteTitel);
viewHolder.unterTitel = (TextView) convertView.findViewById(R.id.untertitelKontaktListe);
convertView.setTag(viewHolder);
viewHolder.spinner = (Spinner)convertView.findViewById(R.id.spinnerKontakte);
}
else{
mainViewHolder = (ViewHolder) convertView.getTag();
mainViewHolder.titel.setText(getItem(position));
mainViewHolder.unterTitel.setText(getItem(position));
// here you have to set data to spinner
}
return convertView;
}
}
public class ViewHolder {
ImageView thumbnail;
TextView titel;
TextView unterTitel;
Spinner spinner
}
}
i want to change my android app in that way, that i implements fragments. In the Mainactivity i am getting the content from a xml file, that i want to populate with the listview in a fragment. But unfortunatey it doesnt work, please can you help me? Here are the codes.
The fragments layout file:
<?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="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00"
android:drawSelectorOnTop="false" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/toptext"/>
</LinearLayout>
And this is the fragment file:
public class RoomFragment extends ListFragment {
public RoomFragment() {
}
private MyArrayAdapter mAdapter;
public static final String ARG_SECTION_NUMBER = "section_number";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
((Dailyquran)getActivity()).main(); // Here I am calling the method in the parent activity to get the content from the xml file. It fills "Tweets"
Dailyquran basem = (Dailyquran)getActivity();
View v = inflater.inflate(R.layout.fragmentlayout, container, false);
final ListView lv = getListView();
mAdapter = new MyArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, basem.tweets);
lv.setAdapter(mAdapter);
return v;
}
public class MyArrayAdapter extends ArrayAdapter<Tweet>
{
Context mContext;
private ArrayList<Tweet> tweets;
Tweet data;
public MyArrayAdapter(Context context, int textViewResourceId, ArrayList<Tweet> items)
{
super(context,textViewResourceId, items);
mContext = context;
this.tweets = items;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
if(row == null)
{
LayoutInflater inflater =((Activity)mContext).getLayoutInflater();
row = inflater.inflate(R.layout.fragmentlayout, null, false);
TextView tt = (TextView) row.findViewById(R.id.toptext);
}
else
{
TextView tt = (TextView) row.findViewById(R.id.toptext);
}
Tweet o = tweets.get(position);
TextView tt = (TextView) row.findViewById(R.id.toptext);
String survers=o.content;
tt.setText(survers);
return row;
}
}
}
You simply need to use the appropriate id in your ListView:
android:id="#android:id/list"