Custom Layout of ListFragment is not working - android

I tried to use a ListFragment with a custom layout: Simple static headline, below the ListContent given with the SimpleCursorAdapter. The list itself also has a custom layout. The SimpleCursorAdapter works (Query is fine, Custom Layout for List works). As long as I do not use custom layout for the Fragment itself, everything works fine.
If I add the layout for the Fragment, only the headline (Textview) works, the list keeps empty.
Custom Layout for the List:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp" >
</TextView>
<TextView
android:id="#+id/value"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_gravity="right"
android:textSize="20sp" >
</TextView>
Custom Layout for the Fragment:
<?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/last_update"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</TextView>
<ListView
android:id="#id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
<TextView android:id="#id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:text="No data">
</TextView>
I commented some stuff out - it was worth a try because i read that it helped - for me it was not helping.
Last bot not least the Code of the Fragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.viewfragment, container, false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
TableDataGateway tableGate = new TableDataGateway(getActivity());
tableGate.UpdateUIRecords();
TextView tv = (TextView) getView().findViewById(R.id.last_update);
tv.setText(tableGate.getLastUpdateDate());
Cursor cursor = tableGate.selectUIRecords();
dBadapter = new SimpleCursorAdapter(getActivity(), R.layout.viewlayout, cursor, new String[] { DB_COL_GUI_LABEL, DB_COL_LAST_KNOWN_VALUE }, new int[] { R.id.label, R.id.value }, 0);
super.setListAdapter(dBadapter);
}
Question is: Why is the ListAdapter not connecting with the fragment layout?
Regards
Flizz
CLARIFICATION OF THE PROBLEM
I have a similar problem like Android + ListFragment with a custom view hierarchy
But i do not have stacked the content over each other, i only see the TextView content in the top corner of the screen and below where the list should be is just a white background.
I was pretty sure to have done anything as Google told to do (http://developer.android.com/reference/android/app/ListFragment.html keyword Screen Layout)
Update:
I am unsure whether i have to change my main_activity.xml:
<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=".MainActivity" >
</RelativeLayout>
I have not considered the fragment in there, do not know whether this is needed. Yesterday i tried a few functions to understand my problem in detail. I used getView().isShown() and get the return FALSE - this seems wrong to my, but I have no clue what is wrong here ...

Your ListView id must be #android:id/list in a ListFragment.
Maybe you should also replace super.setListAdapter to just setListAdapter.

Hey Here is a snippet of correct usage of CustomAdapter:
private class MyCustomAdapter extends BaseAdapter {
private ArrayList<String> mData = new ArrayList<String>();
private LayoutInflater mInflater;
public MyCustomAdapter() {
mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void addItem(final String item) {
mData.add(item);
notifyDataSetChanged();
}
#Override
public int getItemViewType(int position) {
if(position < LIST_ITEM_TYPE_1_COUNT)
return LIST_ITEM_TYPE_1;
else
return LIST_ITEM_TYPE_2;
}
#Override
public int getViewTypeCount() {
return LIST_ITEM_TYPE_COUNT;
}
#Override
public int getCount() {
return mData.size();
}
#Override
public String getItem(int position) {
return mData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
int type = getItemViewType(position);
if (convertView == null) {
holder = new ViewHolder();
switch(type) {
case LIST_ITEM_TYPE_1:
convertView = mInflater.inflate(R.layout.list_item_type1, null);
holder.textView = (TextView)convertView.findViewById(R.id.list_item_type1_text_view);
break;
case LIST_ITEM_TYPE_2:
convertView = mInflater.inflate(R.layout.list_item_type2, null);
holder.textView = (TextView)convertView.findViewById(R.id.list_item_type2_button);
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
holder.textView.setText(mData.get(position));
return convertView;
}
}
public static class ViewHolder {
public TextView textView;
}
}

After reviewing
Blank ListView with SimpleCursorAdapter
http://developer.android.com/samples/CustomChoiceList/src/com.example.android.customchoicelist/MainActivity.html
http://developer.android.com/samples/CustomChoiceList/res/layout/sample_main.html
i found the solution - everything is right so far. I just messed up the layout file:
Both contents ListView as TextView had the value "match_parent" - so there was not enough layout room for the other component. The component which comes first in the layout file overwrites everything else.
I adjusted the layout file to this:
<?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="#android:id/list"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1">
</ListView>
<TextView android:id="#android:id/empty"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="#FF0000"
android:text="No data">
</TextView>
<TextView
android:id="#+id/last_update"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TextView>
</LinearLayout>
and everything works fine. Do NOT use the fragment layout for the activity - it should have its own layout, else it will lead to errors.
Thanks everybody for helping.
Rgds
Flizz

Related

Horizontal GridView with customAdaptor not displaying properly

I want to display a single row of 5 images which then I can click on, this is what I hope to get:
[1][2][3][4][5]
This is what I actually get:"____" is a large, long blank area
[1]________________________
[2]________________________
[3]________________________
[4]________________________
[5]________________________
At first I thought there was something up with the orientation so I tried both "horizontal" and "vertical" but nothing worked.
Here is the adaptor code:
public class CustomTopGridMenuAdaptor extends BaseAdapter {
private Context mContext;
private final int[] gridViewImageId;
private ImageView imageViewAndroid;
public CustomTopGridMenuAdaptor(Context context, int[] gridViewImageId) {
mContext = context;
this.gridViewImageId = gridViewImageId;}
#Override
public int getCount() {
return gridViewImageId.length;
}
#Override
public Object getItem(int i) {
return null;
}
public void setImageSource (int i,ImageView v){
imageViewAndroid.setImageResource(gridViewImageId[i]);
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View convertView, ViewGroup parent) {
View gridViewAndroid;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
gridViewAndroid = new View(mContext);
gridViewAndroid = inflater.inflate(R.layout.android_custom_gridview_layout_top, null);
imageViewAndroid = (ImageView)
gridViewAndroid.findViewById(R.id.android_gridview_image_top);
setImageSource(i,imageViewAndroid);
} else {
gridViewAndroid = (View) convertView;
}
return gridViewAndroid;
}
}
As far as it goes the adaptor is working just fine.
This is the xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/android_custom_gridview_layout_top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="vertical">
<ImageView
android:id="#+id/android_gridview_image_top"
android:layout_width="50dp"
android:layout_height="50dp"/>
</LinearLayout>
This is the xml segment in the activity layout, where the menu will be displayed:
<GridView
android:id="#+id/android_gridview_menu_top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffff"
android:orientation="horizontal"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginStart="196dp">
Can you tell me why it's not working?
Use the android:numColumns="2" in your GridView if you want 5 then change the android:numColumns="5"
<GridView
android:id="#+id/android_gridview_menu_top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffff"
android:numColumns="2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginStart="196dp">
I don't think that gridviews can be made horizontally scrollable. Android has provided HorizontalScrollView it will scroll your view horizontaly. try this.
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<GridView
android:id="#+id/gridView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</GridView>
</LinearLayout>
</HorizontalScrollView>
Happy coding!!

GridView inside other GridView - only one row is shown

I want to make custom calendar, and for this I'm using a GridView (with 12 cells for months) with embedded GridViews for every month (showing days of months). I did very simple layout and code just to check how it looks. The problem is that I get 12 cells, but each of them shows only 1 row of inner grid (of month). So it shows like "0 1 2 3 4 5 6" for every cell of 12 (months).
First, it was worse - it was showing just single row of outer grid with single rows of inner grids. After some experimenting, I solved this problem: the issue was with that my main activity had root element of some special class (this was auto-generated by Android studio), I have changed it to LinearLayout and it started showing all cells of outer grid, but still just 1 row of the inner. First, my inner layouts were wrapped in RelativeLayout, I changed them all to LinearLayout hoping this will fix it - but there is no change. I also experimented a bit with widths and heights with no luck, and I have no idea what to try next.
Here are my layouts:
activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/calendar_year"></include>
</LinearLayout>
calendar_year (outer grid)
<?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">
<GridView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/grid_year"
android:numColumns="3"
android:gravity="center"
android:stretchMode="columnWidth" />
</LinearLayout>
calendar_month (inner grid)
<?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">
<GridView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/grid_month"
android:numColumns="7"
android:gravity="center"
android:stretchMode="columnWidth" />
</LinearLayout>
calendar_day (cell content)
<?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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/text_day"
android:textColor="#android:color/black" />
</LinearLayout>
onCreate of MainActivity.java:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
((GridView) this.findViewById(R.id.grid_year)).setAdapter(new YearGridAdapter(getApplicationContext()));
}
Outer grid's adapter:
public class YearGridAdapter extends BaseAdapter {
public YearGridAdapter(Context context) {
this.context = context;
}
#Override
public int getCount() {
return 12;
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.calendar_month, parent, false);
}
GridView monthView = (GridView) convertView.findViewById(R.id.grid_month);
monthView.setAdapter(new MonthGridAdapter(context));
return convertView;
}
private Context context;
}
Inner grid's adapter:
public class MonthGridAdapter extends BaseAdapter {
public MonthGridAdapter(Context context) {
this.context = context;
}
#Override
public int getCount() {
return 30;
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.calendar_day, parent, false);
}
((TextView) convertView.findViewById(R.id.text_day)).setText(Integer.toString(position));
return convertView;
}
private Context context;
}
This is probably caused by having GridView inside of GridView. Since both layouts are scrollable this causes a lot of problems. You can see on this link Arun Antoney's answer which may solve your problem

Grid Items are changing when scrolling

Hi I am working with custom GridView.
The GridView placed in the following xml/layout file.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<View
android:id="#+id/music_home_ad_view"
android:layout_width="match_parent"
android:layout_height="50dp" />
<ScrollView
android:id="#+id/music_home_root_container"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ScrollView>
<FrameLayout
android:id="#+id/music_home_promo_frame"
android:layout_width="match_parent"
android:layout_height="#dimen/music_home_promo_height"
android:layout_marginTop="20dp" >
<android.support.v4.view.ViewPager
android:id="#+id/music_home_promo_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.raaga.home.CirclePageIndicator
android:id="#+id/music_home_page_indicator"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:layout_gravity="bottom"
android:padding="10dp" />
</FrameLayout>
<GridView
android:id="#+id/music_home_grid_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/music_home_promo_frame"
android:numColumns="3" >
</GridView>
</RelativeLayout>
The GridView contains the following views
music_grid_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent"
>
<ImageView
android:id="#+id/music_grid_image"
android:layout_width="60dp"
android:layout_height="70dp"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:layout_marginTop="10dp"
android:src="#drawable/new_releases"
/>
<TextView
android:id="#+id/music_grid_category_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/music_grid_image"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="New Releases"
android:textColor="#color/text_white"
/>
</RelativeLayout>
I have used the custom adapter class and followed this link
MusicCategoryAdapter.java
public class MusicCategoryAdapter extends BaseAdapter{
ArrayList<Integer> categoryImageList;
ArrayList<String> categoryNameList;
Context mContext;
public MusicCategoryAdapter(Context mContext, ArrayList<Integer> categoryImageList, ArrayList<String> categoryNameList) {
this.mContext= mContext;
this.categoryImageList = categoryImageList;
this.categoryNameList = categoryNameList;
}
#Override
public int getCount() {
//send the list length
return categoryImageList.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
GridHolder holder;
View grid = convertView;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = new View(mContext);
convertView = inflater.inflate(R.layout.music_grid_item, null);
holder = new GridHolder();
holder.categoryTitle = (TextView) convertView.findViewById(R.id.music_grid_category_name);
holder.categoryImage = (ImageView)convertView.findViewById(R.id.music_grid_image);
holder.categoryTitle.setText(categoryNameList.get(position));
holder.categoryImage.setImageResource(categoryImageList.get(position));
convertView.setTag(holder);
} else {
holder = (GridHolder) convertView.getTag();
}
return convertView;
}
public class GridHolder{
ImageView categoryImage;
TextView categoryTitle;
}
}
I am facing the problem that the items are repeating by changing the position when scrolling.
Could anyone help on this problem? What mistake I did in this?
Put the scrollview at the starting of relative layout and end the tag in the end of the relative layout.
This would make your entire layout scrollable and you will overcome the problem.

How can I change the font of customised Navigation Drawer?

This is the layout of my navigation drawer:
<?xml version="1.0" encoding="utf-8"?>
<!-- the root view is now a LinearLayout, all other Views are children of this -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#121314"
android:orientation="vertical">
<!-- a separate section to go above the list -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp">
<!-- your image, you can set it later (see NavDrawerFrag) -->
<ImageView
android:id="#+id/nav_image"
android:layout_width="150dp"
android:layout_height="150dp"
android:padding="15dp"
android:src="#android:drawable/ic_menu_myplaces"/>
<!-- a bit of test or a title to go with it
<TextView
android:id="#+id/nav_text"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="Default text"/>-->
</LinearLayout>
<!-- some divider thing
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:padding="20dp"
android:background="#000000"/>-->
<!-- your ListView is now a child View -->
<ListView
android:id="#+id/nav_listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:listSelector="#drawable/colors"/>
</LinearLayout>
I want a custom font in the ListView, but I've been busting my head for two days straight on this. I just can't seem to get it working.
This is the part where the Navigation Drawer is created:
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState)
{
// need site names for list
siteNames = getActivity().getResources().getStringArray(R.array.site_names);
Log.d(TAG, "number of sites loaded: " + siteNames.length);
// inflate the parent view (the entire layout)
View view = inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
// now grab the separate child views from inside it
mDrawerListView = (ListView) view.findViewById(R.id.nav_listView);
mDrawerImage = (ImageView) view.findViewById(R.id.nav_image);
//mDrawerText = (TextView) view.findViewById(R.id.nav_text);
// configure the Views
mDrawerImage.setImageResource(R.drawable.orange);
mDrawerListView.setOnItemClickListener(this);
mDrawerListView.setAdapter(new ArrayAdapter<String>(getActionBar().getThemedContext(),
android.R.layout.simple_list_item_1, android.R.id.text1, siteNames));
mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
// and return the inflated view up the stack
return view;
}
Instead of using the android.R.id.text1 as your textview resource, you should create your own textview xml layout. You could do something like this
<?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"
android:id= "#+id/listView >
<TextView
android:id="#+id/listItem"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TextView
</LinearLayout>
The following is the custom adapter you will need so you can set your custom styles to your individual items.
public class listAdapter extends BaseAdapter {
String[] siteNames;
Activity a;
public listAdapter(Activity a, String[] siteNames) {
this.a = a;
this.siteNames = siteNames;
}
public int getCount() {
return siteNames.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi = convertView;
vi = a.getLayoutInflater().inflate(R.layout.listView, null);
Typeface tf = Typeface.createFromAsset(a.getAssets(), "fonts/Raleway-Thin.otf");
TextView tv = (TextView) vi.findViewById(R.id.listItem);
tv.setTypeface(tf);
//whatever other changes you want to make to your list items.
return vi;
}
}
You then create a new adapter from this "listAdapter" class, or whatever you would like to name it. Then you can set your listview with this adapter and you should be good to go.

Making Listeners for buttons within a ListView

Copying other people's code that I only half understand, I have succeeded in making a listview, each element of which contains three TextViews and a CheckBox.
The code involves the following two xml files. First "customrowview.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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="142dp"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="fill_parent"/>
<TextView android:id="#+id/text2"
android:layout_width="match_parent"
android:layout_height="fill_parent"/>
<TextView android:id="#+id/text3"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right" >
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<Button
android:id="#+id/editbut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Edit" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Then "customlistview.xml":
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView android:id="#id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000fff"
android:layout_weight="2"
android:drawSelectorOnTop="false">
</ListView>
<TextView android:id="#id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFff00"
android:text="No data"
/>
</LinearLayout>
I get access to the list via:
listView = (ListView) findViewById(R.id.mylist);
The code also involves:
SimpleAdapter adapter = new SimpleAdapter(
this,
list,
R.layout.custom_row_view,
new String[] {"label","daysordate","time"},
new int[] {R.id.text1,R.id.text3, R.id.text2}
);
listView.setAdapter(adapter);
Now what I want to do is something like listView.setlistenerforbuttonwithinlist() !
But can not work out how to do it. I know there have been related questions on SO before, but I can not understand the answers :-(
EDIT: After seeing azgolfers answer... I made a custom adapter as follows:
public class myadapter extends SimpleAdapter
{
LayoutInflater mLayoutInflater;
public void myadapter(Context context)
{
mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View view = null;
if (convertView != null)
{
view = convertView;
}
else
{
view = mLayoutInflater.inflate(R.layout.custom_row_view, null);
}
Button buttonEdit = (Button) view.findViewById(R.id.editbut);
buttonEdit.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
Log.i("xx","Button pressed!");
}
});
return super.getView(position, convertView, parent);
}
public myadapter(Context context, List<? extends Map<String, ?>> data,int resource, String[] from, int[] to)
{
super(context, data, resource, from, to);
// TODO Auto-generated constructor stub
}
}
Unfortunatly this crashes at the line
view = mLayoutInflater.inflate(R.layout.custom_row_view, null);
With a null pointer exception... not sure why :-(
First, you need to extend your own Adapter, probably from an ArrayAdapter. ArrayAdapter has a method called getView that you will need to override and provide the UI for a listview row at a certain position. e.g.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView != null) {
view = convertView;
} else {
view = mLayoutInflater.inflate(R.layout.custom_row_view, null);
}
Button buttonEdit = (Button) view.findViewById(R.id.editbut);
buttonEdit.setOnClickListener(...);
}
In getView(), since you are building the UI of the row, you have a chance here to set the click handler on your button.
Also, you also need to add this to your Button's xml tag:
android:focusable="false"
android:focusableInTouchMode="false"
Without that, button inside a listview will not fire OnClick event when pressed.
try to сhange
view = mLayoutInflater.inflate(R.layout.custom_row_view, null);
to
view = mLayoutInflater.inflate(R.layout.custom_row_view, parent, false);
Good luck!

Categories

Resources