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"
Related
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'm new android..my english little bit:(
I want develop multi pane app.
Two fragments "Gridview" and "Full screen"
I know multi pane, but i dont know Gridview and Full screen in multi pane.
Because all samples for ListView.
help me please
this app good for me but too complex=
http://www.codeproject.com/Articles/779293/Building-Dynamic-UI-for-Android-Devices
Its a simple implementation if i understand your question correctly
------------- Creating GridView ------------
For GridView you need an adapter for your GridView Object and images for columns, an example using country list and flag images,
Create drawable folder inside res and add country flag images inside drawable folder
Create gridlayout.xml in res/layout
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#drawable/border"
android:padding="5dp">
<ImageView
android:id="#+id/gridimage"
android:layout_height="65dp"
android:layout_width="65dp"
android:src="#drawable/icon"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
</ImageView>
<TextView
android:id="#+id/gridtext"
android:text="TextView"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="#+id/imageView1"
android:layout_marginTop="2dp"
android:layout_centerHorizontal="true"
android:textSize="18sp"
android:ellipsize="marquee"></TextView>
</RelativeLayout>
Create gridrow.xml in res/Layout folder
<GridView
android:id="#+id/gridView1"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:numColumns="auto_fit"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp">
</GridView>
Create GridAdapter.java class
public class GridAdapter extends BaseAdapter {
private ArrayList<String> CountryList;
private ArrayList<Integer> CountryFlag;
private Activity activity;
public GridAdapter(Activity activity,ArrayList<String> CountryList, ArrayList<Integer> CountryFlag){
super();this.CountryList = CountryList;
this.CountryFlag = CountryFlag;
this.activity = activity;}
#Override
public int getCount() {
return CountryList.size();
}
#Override
public String getItem(int position) {
return CountryList.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
public static class ViewHolder
{
public ImageView imgViewFlag;
public TextView txtViewTitle;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder view;
LayoutInflater inflator = activity.getLayoutInflater();
if(convertView==null)
{
view = new ViewHolder();
convertView = inflator.inflate(R.layout.gridview_row, null);
view.txtViewTitle = (TextView) convertView.findViewById(R.id.gridtext);
view.imgViewFlag = (ImageView) convertView.findViewById(R.id.gridimage);
convertView.setTag(view);
}
else
{
view = (ViewHolder) convertView.getTag();
}
view.txtViewTitle.setText(CountryList.get(position));
view.imgViewFlag.setImageResource(CountryFlag.get(position));
return convertView;
}
}
Create Fragment GridViewActivty
public class GridViewActivty extends Fragment {
private ArrayList CountryList = new ArrayList();
private ArrayList CountryFlag = new ArrayList();
private GridView mygrid;
private GridAdapter adapter;
private Context context;
public GridViewActivty(){}
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState)
{ View rootView = inflater.inflate(R.layout.gridlayout.xml, container, false);
CountryList = new ArrayList<String>();
CountryList.add("South Africa");
CountryList.add("Spain");
CountryList.add("Canada");
CountryList.add("China");
CountryList.add("France");
CountryList.add("Germany");
CountryList.add("Iran");
CountryList.add("Italy");
CountryList.add("Japan");
CountryList.add("Korea");
CountryList.add("Mexico");
CountryList.add("Netherlands");
CountryFlag = new ArrayList<Integer>();
CountryFlag.add(R.drawable.southafrica);
CountryFlag.add(R.drawable.spain);
CountryFlag.add(R.drawable.canada);
CountryFlag.add(R.drawable.china);
CountryFlag.add(R.drawable.france);
CountryFlag.add(R.drawable.germany);
CountryFlag.add(R.drawable.iran);
CountryFlag.add(R.drawable.italy);
CountryFlag.add(R.drawable.japan);
CountryFlag.add(R.drawable.korea);
CountryFlag.add(R.drawable.mexico);
CountryFlag.add(R.drawable.netherlands);
adapter = new GridAdapter(this.getActivity(),CountryList, CountryFlag);
mygrid = (GridView)rootView.findViewById(R.id.gridview1);
mygrid.setAdapter(adapter);
mygrid.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
}
}
);
return rootView;
}
}
----------- Creating Full Screen --------
Create fullscreen.xml inside Layout Folder
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
>
Create FullScreen.java Fragment
public class Fullscreen extends Fragment{ #Override public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle
savedInstanceState)
{ View rootView = inflater.inflate(R.layout.fullscreen.xml, container, false);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
return convertView; }
I am trying to populate a ListView with data from a web service using Retrofit, but my ListView is somehow always empty. I am getting the data from the web service fine, it just isn't getting into the ListView. setListAdapter() doesn't seem to be doing anything because the listview's count is 0 after I call it. Why is my ListView empty? Here is some of my code:
public class PopularFragment extends ListFragment{
private final String NAME = "Popular";
RestClient restClient;
#Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
restClient = RestClientService.getService();
restClient.getApps(new Callback<List<App>>() {
#Override
public void success(List<App> apps, Response response) {
Log.v("test", apps.toString());
setListAdapter(new AppListAdapter(getActivity(), apps));
Log.v("test", Integer.toString(getListAdapter().getCount()));
Log.v("test", Integer.toString(getListView().getCount()));
}
#Override
public void failure(RetrofitError retrofitError) {
Log.v("test", "failure");
}
});
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_browse_apps, container, false);
return view;
}
The two log methods Log.v("test", Integer.toString(getListAdapter().getCount())); and
Log.v("test", Integer.toString(getListView().getCount())); both return 0.
Here is my custom adapter:
public class AppListAdapter extends ArrayAdapter<App>
{
private final Context context;
private List<App> apps;
public AppListAdapter(Context context, List<App> apps)
{
super(context, R.layout.app_list_item);
this.context = context;
this.apps = apps;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View view = convertView;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
view = inflater.inflate(R.layout.app_list_item, parent, false);
}
App app = this.apps.get(position);
TextView appName = (TextView) view.findViewById(R.id.appName);
appName.setText("TEST");
return view;
}
}
app_list_item.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:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="App Name"
android:id="#+id/appName" />
fragment_browse_apps.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.android.devon.appfrenzy.BrowseAppsActivity$PlaceholderFragment">
<TextView
android:id="#+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="HELLO" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#android:id/list"
android:layout_centerHorizontal="true" />
You should call the 3 variable constructor for the ListAdapter:
public AppListAdapter(Context context, List<App> apps)
{
super(context, R.layout.app_list_item,apps);
this.context = context;
}
Then you don't actually need to store apps off, it can be retrieved using getItem
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View view = convertView;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
view = inflater.inflate(R.layout.app_list_item, parent, false);
}
App app = getItem(position);
TextView appName = (TextView) view.findViewById(R.id.appName);
appName.setText("TEST");
return view;
}
I am new in android app dev and I am trying to create a ListFragment consisting of a TextView and a ListView. I want to try and populate the ListView with data from a custom class and thus created a custom ArrayAdapter. My app runs but doesn't show anything in the list and I can't figure out why not. I have logged the data and it is present in the holder and the text seems to be set but something is wrong.
row_layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip" >
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="fill_parent">
<TextView
android:id="#+id/TextViewStation"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="My Application" />
<TextView
android:id="#+id/TextViewType"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:singleLine="true"
android:ellipsize="marquee"
android:text="Simple application that shows how to use RelativeLayout" />
</LinearLayout>
</RelativeLayout>
list_layout.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="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="No items to display." />
<ListView
android:id="#+id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
SimpleArrayAdapter.java
public class SimpleArrayAdapter extends ArrayAdapter<Warning> {
private final Context context;
private final List<Warning> objects;
public SimpleArrayAdapter(Context context, int resource,
List<Warning> objects) {
super(context, resource);
this.context = context;
this.objects = objects;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = new ViewHolder();
if (row == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.row_layout, parent, false);
holder.v1 = (TextView) row.findViewById(R.id.TextViewStation);
holder.v2 = (TextView) row.findViewById(R.id.TextViewType);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
final Warning warning = objects.get(position);
if (warning != null) {
holder.v1.setText(warning.getStation());
holder.v2.setText(warning.getType());
// Log.d("Adapter", "holder.v1.getText(): " + holder.v1.getText());
}
return row;
}
public static class ViewHolder {
TextView v1; // view1
TextView v2; // view2
}
#Override
public int getCount() {
return objects.size();
}
#Override
public Warning getItem(int position) {
// TODO Auto-generated method stub
return objects.get(position);
}
}
List_View.java - the ListFragment
public class List_View extends ListFragment {
List<Warning> items = new ArrayList<Warning>();
Gson gson = new Gson();
private SimpleArrayAdapter adapter;
private ListView mListView;
// #Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ---- Get data!
JsonElement je = new JsonParser().parse(result);
JsonObject jo = je.getAsJsonObject();
JsonArray ja = jo.getAsJsonArray("warnings");
for (int i = 0; i < ja.size(); i++) {
Warning war = new Warning();
war = gson.fromJson(ja.get(i).getAsJsonObject(), Warning.class);
items.add(war);
}
// ----
}
// #Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater
.inflate(R.layout.list_layout, container, false);
// RESTManager manager = new RESTManager();
return rootView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.d("Adapter", "items.size(): "+ items.size());
adapter = new SimpleArrayAdapter(getActivity(), R.layout.row_layout,
items);
setListAdapter(adapter);
}
}
In SimpleArrayAdapter, me logging the result from holder.v1.getText() returns correct value but still nothing is showing in the ListView in the app.
Try this :
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
TextView station, type ;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.row_layout, parent, false);
station = (TextView) row.findViewById(R.id.TextViewStation);
type = (TextView) row.findViewById(R.id.TextViewType);
}
Warning warning = objects.get(position);
if (warning != null) {
station.setText(warning.getStation());
type.setText(warning.getType());
// Log.d("Adapter", "holder.v1.getText(): " + holder.v1.getText());
}
return row;
}
I found the problem. Stupidly for some reason I had android:layout_width="0dip" for the textviews........ changed to android:layout_width="fill_parent" and boom, there is my data!
I'm trying to understand how to work with listfragments and custom adapters. So I build this little example and I was wondering where I could set the divider of my listview to null.
I found different ways:
- android:dividerHeight="1dip"
- android:divider="#android:color/transparent"
But i dont have an XML layout with listview
I also saw something like listview.setDivider(null), but I dont know where I can use that method in my code because of the use of listfragments.
My code:
listview_item_row.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="#+id/ivCountry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"
android:layout_weight="1"/>
<TextView
android:id="#+id/tvCountry"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:text="TextView"
android:layout_weight="4" />
</LinearLayout>
CountryList class
public class CountryList extends ListFragment {
Country[] countries2 = new Country[]
{
new Country("Netherlands"),
new Country(R.drawable.bas,"Basque Country")
};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
CountryAdapter adapter = new CountryAdapter(inflater.getContext(),R.layout.listview_item_row,countries2);
setListAdapter(adapter);
getListView().setDivider(null);
return super.onCreateView(inflater, container, savedInstanceState);
}
My CountryAdapter
public class CountryAdapter extends ArrayAdapter<Country>{
Context context;
int layoutResourceId;
Country[] data = null;
public CountryAdapter(Context context, int layoutResourceId, Country[] data) {
super(context,layoutResourceId,data);
this.context = context;
this.layoutResourceId = layoutResourceId;
this.data = data;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row = convertView;
CountryHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent,false);
holder = new CountryHolder();
holder.imgIcon = (ImageView) row.findViewById(R.id.ivCountry);
holder.txtTitle = (TextView)row.findViewById(R.id.tvCountry);
row.setTag(holder);
} else {
holder = (CountryHolder) row.getTag();
}
Country country = data[position];
holder.txtTitle.setText(country.getTitle());
holder.imgIcon.setImageResource(country.getIcon());
return row;
}
static class CountryHolder
{
ImageView imgIcon;
TextView txtTitle;
}
You can always set a custom view for the ListFragment by returning a view on onCreateView() but you need to have a ListView in it with a "#id/android:list" like explained in the documentation.
In your xml you could do android:divider="#null" or in if you really want to do it in your ListFragment code getListView().setDivider(null); (in onActivityCreated() method).
Inside onCreateView use:
getListView().setDivider(null);