I can't make a two listview [duplicate] - android

This question already has answers here:
How to display a two column ListView in Android?
(5 answers)
Closed 7 years ago.
Hi I'd like to made a 2 vertical listview to show some data from server.
But it's getting a lot of errors..
Please help me to fix it.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView android:id="#+id/data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/data view"
/>
<ListView
android:id="#+id/date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:stackFromBottom="true" >
</ListView>
<ListView
android:id="#+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:stackFromBottom="true" >
</ListView>
</LinearLayout>
What I'm after:

I will give you an answer that resembles what you want, but you are free to tweak it.
Just to note: RecyclerView is mostly used nowadays, but since you asked for a ListView example I will give you an example with a ListView.
First of all you need to specify one ListView in your main activity's layout xml file. You also need a second layout for each item in the ListView. This second layout is going to have two TexViews. The first for Date and the second for Result. And finally you need an adapter for that ListView, which is responsible for inflating each item's (of the list) layoout, keep your list data, etc.
Here is a sample activity_main.xml layout. I added two views to resemble the lines between TextViews:
<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:padding="16dp"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/relativeLayout" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Date"
android:id="#+id/textViewDate"
android:gravity="center"
android:layout_toLeftOf="#+id/view2"
android:layout_toStartOf="#+id/view2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#android:color/darker_gray"
android:id="#+id/view1"
android:layout_alignBottom="#+id/textViewDate" />
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#android:color/darker_gray"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="#+id/view2"
android:layout_alignBottom="#+id/textViewDate" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Result"
android:id="#+id/textViewResult"
android:gravity="center"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_toRightOf="#+id/view2"
android:layout_toEndOf="#+id/view2" />
</RelativeLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listView"
android:layout_below="#+id/relativeLayout">
</ListView>
</RelativeLayout>
The list_item.xml layout for each item in the list:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Date"
android:id="#+id/textViewDate"
android:gravity="center"
android:layout_toLeftOf="#+id/view"
android:layout_toStartOf="#+id/view"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#android:color/darker_gray"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="#+id/view"
android:layout_alignBottom="#+id/textViewDate" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Result"
android:id="#+id/textViewResult"
android:gravity="center"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_toRightOf="#+id/view"
android:layout_toEndOf="#+id/view"
android:layout_alignBottom="#+id/textViewDate" />
</RelativeLayout>
Then a class for your custom item that holds a date and a result.
public class MyItem {
public GregorianCalendar date;
public String result;
public MyItem(GregorianCalendar date, String result){
this.date = date;
this.result = result;
}
}
and the adapter (here ArrayAdapter):
public class MyAdapter extends ArrayAdapter<MyItem> {
private final Context mContext;
private MyItem[] mItems;
public MyAdapter(Context context, MyItem[] items) {
super(context, -1, items);
mContext = context;
mItems = items;
}
#Override
public View getView(int position,View view,ViewGroup parent) {
View rowView = view;
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.list_item, null, true);
}
TextView textViewDate = (TextView) rowView.findViewById(R.id.textViewDate);
TextView textViewResult = (TextView) rowView.findViewById(R.id.textViewResult);
textViewDate.setText(mItems[position].date.getTime().toString());
textViewResult.setText(mItems[position].result);
return rowView;
}
}
Finally in your activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyItem[] items = {new MyItem(new GregorianCalendar(2014, 1, 1), "Ok"),
new MyItem(new GregorianCalendar(2014, 2, 3), "Not ok"),
new MyItem(new GregorianCalendar(2015, 3, 3), "Ok"),
new MyItem(new GregorianCalendar(2015, 5, 7), "Warning"),
new MyItem(new GregorianCalendar(2016, 7, 1), "Ok")};
MyAdapter adapter = new MyAdapter(this, items);
ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
}

Related

custom listview shows large space between them when adding a new item by clicking on button in android studio

I have got a custom listview. I am trying to add items to the custom listview by clicking on the button. When I click the button, I want the items to show one under the other but it shows a big space between items. Data read from the database are listed in the listview at the same time.it works like chat applications. Thanks for your help
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="match_parent"
android:layout_height="wrap_content"
tools:context=".Mainactivity">
<ListView
android:id="#+id/listid"
android:layout_width="match_parent"
android:divider="#drawable/dessen"
android:layout_above="#+id/lineerid"
android:dividerHeight="3dp"
android:layout_height="wrap_content">
</ListView>
<LinearLayout
android:id="#+id/lineerid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_marginLeft="0dp"
android:layout_marginBottom="3dp"
android:background="#91f1f1f1"
android:orientation="horizontal"
android:paddingBottom="2dp">
<EditText
android:id="#+id/mesajid"
android:layout_width="252dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/gonderid"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="#+id/sendMessageButton"
android:layout_weight="0.72"
android:ems="10"
android:maxHeight="80dp" />
<Button
android:id="#+id/gonderid"
android:layout_width="wrap_content"
android:textColor="#ffffff"
android:layout_height="wrap_content"
android:background="#drawable/buttonsekil"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:text="Gönder" />
</LinearLayout>
</RelativeLayout>
custom _layout
<?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="wrap_content"
android:background="#ffffff"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/nickid"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:textColor="#352925"
android:textSize="18sp"
android:textStyle="bold">
</TextView>
<TextView
android:id="#+id/tarihid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:textColor="#352925"
android:textSize="18sp"
android:textStyle="bold">
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_marginTop="10dp"
android:layout_height="wrap_content">
<TextView
android:id="#+id/formesajid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#050505"
android:textColorLink="#9C27B0"
android:textSize="16sp"
android:textStyle="bold">
</TextView>
</LinearLayout>
</LinearLayout>
custom adapter
public class customadapter extends BaseAdapter {
private LayoutInflater layoutInflater;
private List<Model> modelList;
public customadapter(Activity activity,List<Model> modelList) {
layoutInflater= (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.modelList=modelList;
}
#Override
public int getCount() {
return modelList.size();
}
#Override
public Object getItem(int position) {
return modelList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
view=layoutInflater.inflate(R.layout.custom_layout,null);
TextView nick =view.findViewById(R.id.nickid);
TextView tarih=view.findViewById(R.id.tarihid);
TextView mesaj=view.findViewById(R.id.formesajid);
Model model=modelList.get(position);
nick.setText(model.getNickname());
tarih.setText(model.getTarih());
mesaj.setText(model.getMesaj());
return view;
}
}

GridView with incomplete row item does not scroll

I have a problem with my GridView when it receives incomplete row item number, it doesn't scroll anymore. My items are dynamic which are given by the server, I have 3 columns and if the number of GridView items are more than enough to enable scrolling and if the row item is 3, it works fine. For example (9 items):
But if the third row has only 1 or 2 item/s, it doesn't scroll anymore (7 or 8 items):
This has caused me headache more than excitement, my GridView in layout is:
<GridView
android:id="#+id/gridView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#color/white"
android:listSelector="#android:color/transparent"
android:numColumns="3"
android:stretchMode="columnWidth" >
</GridView>
In my activity nothing much, just declaration and setting its adapter. Did I miss anything? I tried adding verticalSpacing and padding separately but nothing's changed.
EDIT:
This is the setup in the whole layout:
Black is a RelativeLayout, FrameLayout and the last ImageView are aligned top and bottom respectively. Top TextView is below FrameLayout, bottom TextView is below top TextView. Now GridView is below bottom TextView and above ImageView, it should expand its height basing to the space left consumed between the first 3 elements and the bottom element. This is how it's setup in my layout.
I believe it's a layout problem more than in my code since I've done dozens of ListView and this has similar implementation.
EDIT2:
The layout:
<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:orientation="vertical"
tools:context=".SixthActivity" >
<FrameLayout
android:id="#+id/cover_frame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/cover_image"
android:layout_width="match_parent"
android:layout_height="180dp"
android:background="#color/gray"
android:scaleType="centerCrop" />
<ProgressBar
android:id="#+id/cover_image_progressbar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="180dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
android:layout_margin="10dp" />
<TextView
android:id="#+id/school_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginBottom="10dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:ellipsize="end"
android:maxLines="2"
android:textColor="#color/white"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:id="#+id/container_sns"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_margin="10dp"
android:gravity="right"
android:orientation="horizontal" >
<ImageView
android:id="#+id/btn_telephone"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:visibility="gone" />
<ImageView
android:id="#+id/btn_email"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:visibility="gone" />
<ImageView
android:id="#+id/btn_facebook"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:visibility="gone" />
<ImageView
android:id="#+id/btn_twitter"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:visibility="gone" />
<ImageView
android:id="#+id/btn_line"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:visibility="gone" />
</LinearLayout>
</RelativeLayout>
</FrameLayout>
<TextView
android:id="#+id/student_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/cover_frame"
android:background="#f8f7f3"
android:padding="10dp"
android:textColor="#color/brown"
android:textStyle="bold" />
<RelativeLayout
android:id="#+id/container_news"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/student_name"
android:background="#drawable/bg_news"
android:clickable="true"
android:orientation="horizontal"
android:visibility="gone" >
<TextView
android:id="#+id/news"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:layout_marginLeft="85dp"
android:layout_marginRight="10dp"
android:clickable="true"
android:ellipsize="end"
android:maxLines="2"
android:textColor="#color/brown"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/container_next_plan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/container_news"
android:background="#drawable/bg_next"
android:clickable="true"
android:orientation="horizontal"
android:visibility="gone" >
<TextView
android:id="#+id/next_plan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:layout_marginLeft="85dp"
android:layout_marginRight="10dp"
android:clickable="true"
android:ellipsize="end"
android:maxLines="2"
android:textColor="#color/brown"
android:textStyle="bold" />
</RelativeLayout>
<GridView
android:id="#+id/grid_view"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/mypage_footer"
android:layout_below="#id/container_next_plan"
android:background="#ACA899"
android:gravity="center"
android:numColumns="3" >
</GridView>
<ImageView
android:id="#+id/mypage_footer"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:scaleType="fitXY" />
</RelativeLayout>
Don't mind the ImageViews, I just took out their source. I needed to retain the setup from my actual layout. The code:
public class SixthActivity extends Activity {
private Integer[] gridViewItem = new Integer[] { 1, 2, 3, 4, 5, 6, 7, 8 };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sixth);
GridView gridView = (GridView) findViewById(R.id.grid_view);
CustomAdapter adapter = new CustomAdapter(this,
R.layout.item_module, gridViewItem);
gridView.setAdapter(adapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.sixth, menu);
return true;
}
}
The adapter:
public class CustomAdapter extends ArrayAdapter<Integer> {
private Context context;
private int resource;
private Integer[] moduleList;
public CustomAdapter(Context context, int resource,
Integer[] moduleList) {
super(context, resource, moduleList);
// TODO Auto-generated constructor stub
this.context = context;
this.resource = resource;
this.moduleList = moduleList;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(resource, parent, false);
holder = new ViewHolder();
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
holder.tvModuleName = (TextView) row.findViewById(R.id.module_name);
holder.tvModuleImage = (ImageView) row.findViewById(R.id.module_image);
holder.tvModuleName.setText("SAMPLESAMPLE");
return row;
}
public class ViewHolder {
public ImageView tvModuleImage;
public TextView tvModuleName;
}
}
The item layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/item_module"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/module_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/module_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:ellipsize="end"
android:singleLine="true" />
</LinearLayout>
Try to add numbers to gridViewItem to add items enough to trigger the scrolling just like in my screenshots, and try taking out 1 or 2 items at the bottom such that that row would be incomplete to replicate my problem. Thanks.

Button inside ListView not clickable

I want to be able to click on a button inside an item of a ListView. It should have a different effect from clicking the whole item. I realize there are several questions asked on stackoverflow, but none of the suggestions works for me.
The ListView is inside a Fragment.
Layout of the fragment:
<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=".EventFragment" >
<ListView
android:id="#+id/event_list"
android:background="#C0FFFFFF"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp" />
</RelativeLayout>
Layout of each list item:
<?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="wrap_content"
android:orientation="vertical"
android:background="#C0101010">
<TextView
android:id="#+id/event_list_separator"
style="?android:attr/listSeparatorTextViewStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="separator"
android:textColor="#android:color/white" />
<LinearLayout
android:id="#+id/event_list_element"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFFFF"
android:padding="6dip" >
<ImageView
android:id="#+id/event_list_element_icon"
android:layout_width="26dip"
android:layout_height="60dip"
android:layout_marginRight="6dip"
android:contentDescription="TODO" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/event_list_element_firstLine"
android:layout_width="match_parent"
android:layout_height="25dip"
android:text="item_header"
android:textSize="18sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="35dip"
android:orientation="horizontal" >
<TextView
android:id="#+id/event_list_element_secondLine"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:ellipsize="marquee"
android:singleLine="true"
android:text="Description"
android:textSize="14sp" />
<Button
android:id="#+id/event_list_element_button_1"
android:layout_width="132dip"
android:layout_height="match_parent"
android:drawableLeft="#drawable/ic_button1"
android:text="Participate"
android:textStyle="bold"
android:textSize="14sp"
/>
<Button
android:id="#+id/event_list_element_button_2"
android:layout_width="110dip"
android:layout_height="match_parent"
android:ellipsize="marquee"
android:drawableLeft="#drawable/ic_button2"
android:singleLine="true"
android:text="No thanks"
android:textStyle="bold"
android:gravity="center_vertical"
android:textSize="14sp"
/>
<TextView
android:id="#+id/event_list_element_additional_text"
android:layout_width="100dip"
android:layout_height="match_parent"
android:ellipsize="marquee"
android:singleLine="true"
android:gravity="center_vertical"
android:text="sample"
android:textStyle="bold"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
My list adapter is:
public class EventAdapter extends ArrayAdapter<Event> {
static class ViewHolder {
TextView separator;
LinearLayout relativeLayout;
TextView eventHeader;
TextView eventDescription;
ImageView blueDot;
Button button1;
Button button2;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater)
_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.event_list_item, parent, false);
viewHolder = new ViewHolder();
viewHolder.relativeLayout = (LinearLayout) convertView.findViewById(R.id.event_list_element);
viewHolder.blueDot = (ImageView) convertView.findViewById(R.id.event_list_element_icon);
viewHolder.eventHeader = (TextView) convertView.findViewById(R.id.event_list_element_firstLine);
viewHolder.eventDescription = (TextView) convertView.findViewById(R.id.event_list_element_secondLine);
viewHolder.button1 = (Button) convertView.findViewById(R.id.event_list_element_button1);
viewHolder.button2 = (Button) convertView.findViewById(R.id.event_list_element_button2);
viewHolder.separator = (TextView) convertView.findViewById(R.id.event_list_separator);
convertView.setTag(viewHolder);
} else{
viewHolder = (ViewHolder) convertView.getTag();
}
final Event item = getItem(position);
if (item != null) {
OnClickListener listener = new OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(_context, "boo!", Toast.LENGTH_SHORT).show();
}
};
viewHolder.button1.setOnClickListener(listener);
}
return convertView;
}
}
The problem is that the two buttons are not clickable. What I tried to far:
ListView listView = (ListView) rootView.findViewById(R.id.event_list);
listView.setItemsCanFocus(true);
I also tried setting on the button:
android:focusable="true"
android:clickable="true"
I also experimented with android:descendantFocusability.
None of my tries made the buttons clickable.
Insert the attribute android:descendantFocusability="blocksDescendants" in the Parent Layout declaration of each list item.
The xml should be as follows:
<?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="wrap_content"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants"
android:background="#C0101010">
<TextView
android:id="#+id/event_list_separator"
style="?android:attr/listSeparatorTextViewStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="separator"
android:textColor="#android:color/white" />
<LinearLayout
android:id="#+id/event_list_element"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFFFF"
android:padding="6dip" >
<ImageView
android:id="#+id/event_list_element_icon"
android:layout_width="26dip"
android:layout_height="60dip"
android:layout_marginRight="6dip"
android:contentDescription="TODO" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/event_list_element_firstLine"
android:layout_width="match_parent"
android:layout_height="25dip"
android:text="item_header"
android:textSize="18sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="35dip"
android:orientation="horizontal" >
<TextView
android:id="#+id/event_list_element_secondLine"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:ellipsize="marquee"
android:singleLine="true"
android:text="Description"
android:textSize="14sp" />
<Button
android:id="#+id/event_list_element_button_1"
android:layout_width="132dip"
android:layout_height="match_parent"
android:drawableLeft="#drawable/ic_button1"
android:text="Participate"
android:textStyle="bold"
android:textSize="14sp"
/>
<Button
android:id="#+id/event_list_element_button_2"
android:layout_width="110dip"
android:layout_height="match_parent"
android:ellipsize="marquee"
android:drawableLeft="#drawable/ic_button2"
android:singleLine="true"
android:text="No thanks"
android:textStyle="bold"
android:gravity="center_vertical"
android:textSize="14sp"
/>
<TextView
android:id="#+id/event_list_element_additional_text"
android:layout_width="100dip"
android:layout_height="match_parent"
android:ellipsize="marquee"
android:singleLine="true"
android:gravity="center_vertical"
android:text="sample"
android:textStyle="bold"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
I found the solution! I wanted to update the list of events periodically (right now it's a thread running every x milliseconds, later I want to switch that to only update the event list when there is a change). Anyway, the code was (inside my main activity):
private BroadcastReceiver _bReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(RECEIVE_EVENT)) {
Bundle bundle = intent.getExtras();
Event event = (Event) bundle.get("event");
showEvent(event);
}
}
};
private void showEvent(final Event event){
final Context context = this;
runOnUiThread(new Runnable() {
public void run() {
final ListView listview = (ListView) findViewById(R.id.event_list);
EventAdapter adapter = new EventAdapter(context, id.event_list, getEventList());
listview.setAdapter(adapter);
}
});
}
Setting the adapter each time is certainly not the right approach. Once I changed that to only set the adapter once, it worked like suggested using android:descendantFocusability="blocksDescendants"

List with multiple radio buttons in android

I'm developing an application with the list of member who participating in different events.
A group of people participating in a group of Games. But one person can participate in only one game. So i have chosen the RadioButton. Four Games namely A,B,C and D. The list of People have listed in a list with four RadioButton, we need to select one among them for each people.
I Have tried GridView with customized row data as follow...
<GridView
android:id="#+id/gridViewMarkList"
android:layout_width="match_parent"
android:layout_height="326dp"
android:numColumns="1" >
</GridView>
And My Grid List is a Single column with the following data in row.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="500dp"
android:layout_height="fill_parent"
android:padding="10dp" >
<TableRow>
<TextView
android:id="#+id/SNo"
style="#style/text"
android:layout_width="100dp"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/Name"
style="#style/text"
android:layout_width="180dp"
android:layout_height="wrap_content" />
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/A"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:checked="true" />
<RadioButton
android:id="#+id/B"
android:layout_width="40dp"
android:layout_height="wrap_content" />
<RadioButton
android:id="#+id/C"
android:layout_width="40dp"
android:layout_height="wrap_content" />
<RadioButton
android:id="#+id/D"
android:layout_width="40dp"
android:layout_height="wrap_content" />
</RadioGroup>
</TableRow>
Now i couldn't retrieve the Data in the Activity..
Give me any suggestion for doing this Process..!
You will need a custom ListView for this case.
make your code like below to achieve it..
row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout>
<TextView
android:id="#+id/SNo"
style="#style/text"
android:layout_width="100dp"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/Name"
style="#style/text"
android:layout_width="180dp"
android:layout_height="wrap_content" />
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/A"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:checked="true" />
<RadioButton
android:id="#+id/B"
android:layout_width="40dp"
android:layout_height="wrap_content" />
<RadioButton
android:id="#+id/C"
android:layout_width="40dp"
android:layout_height="wrap_content" />
<RadioButton
android:id="#+id/D"
android:layout_width="40dp"
android:layout_height="wrap_content" />
</RadioGroup>
CustomListAdapter.java
public class CustomListAdapter extends ArrayAdapter<String> {
private Context mContext = null;
private int id;
private List<String> list = null;
public CustomListAdapter(Context context, int textViewResourceId, List<String> list) {
super(context, textViewResourceId, patientNameList);
mContext = context;
id = textViewResourceId;
this.list = list;
}
#Override
public View getView(int position, View v, ViewGroup parent) {
View mView = v;
if (mView == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mView = inflater.inflate(id, null);
}
/*
here you go with your views which you gonna display
ex. : RadioButton gameOption = (RadioButton) mView.findViewById(R.id.gameOptionRadioBtn);
*/
return mView;
}
}
InYourActivityClass
ArrayAdapter<String> adapter = new CustomListAdapter(this, R.layout.row, list);
listView.setAdapter(adapter);
Hope this will help:)
Use this in your RadioGroup to make clickable
android:focusable="false"
android:focusableInTouchMode="false"

android -> relative-layout changes button text alignment on click

This is my main.xml:`
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:addStatesFromChildren="false"
android:layout_height="wrap_content">
<TableLayout
android:id="#+id/header"
android:layout_marginRight="2dp"
android:layout_marginLeft="2dp"
android:layout_height="wrap_content"
android:background="#android:color/black"
android:layout_width="fill_parent"
android:layout_alignParentTop="true">
<TableRow>
<TextView
android:id="#+id/leftHeader"
android:textColor="#android:color/black"
android:layout_margin="1dp"
android:gravity="center_horizontal"
android:layout_weight="1"
android:background="#ffcccccc"
android:text="textview1"
android:layout_width="30dip" />
<TextView
android:id="#+id/rightHeader"
android:textColor="#android:color/black"
android:gravity="center_horizontal"
android:layout_weight="1"
android:layout_margin="1dp"
android:background="#android:color/white"
android:text="textview2"
android:layout_width="70dip" />
</TableRow>
</TableLayout>
<ScrollView
android:id="#+id/table_scroll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="2dp"
android:layout_marginLeft="2dp"
android:layout_below="#+id/header"
android:layout_above="#+id/buttonTable">
<TableLayout
android:id="#+id/maintable"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/black">
<TableRow
android:id="#+id/maintableRow">
<TextView
android:id="#+id/maintableLeft"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginRight="1dp"
android:layout_marginLeft="1dp"
android:text="textview1"
android:layout_width="30dip" />
<TextView
android:id="#+id/maintableRight"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:text="textview2"
android:layout_width="70dip" />
</TableRow>
</TableLayout>
</ScrollView>
<TableLayout
android:id="#+id/buttonTable"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/black"
android:layout_alignParentBottom="true">
<TableRow>
<Button
android:id="#+id/button_ResetData"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="Reset Data"
android:layout_gravity="center"
android:layout_weight="1" />
<Button
android:id="#+id/button_Refresh"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="Refresh"
android:gravity="center"
android:layout_weight="1" />
</TableRow>
</TableLayout>
`
After I press one of the buttons its text alignment goes to the left, how can I make it stay in center?
something similar happens with the first table (header) (textview1, textview2) but here it also reduses the textsize the amount of the text in the view...
EDIT: I managed to find the code that causes the change but I don't know how to fix this.
Maybe some one can explain me why does this happen and how to fix it?
private void fillTableView(List<BatteryInfo> list) {
TextView leftHeader = (TextView) findViewById(R.id.leftHeader);
TextView rightHeader = (TextView) findViewById(R.id.rightHeader);
LayoutParams leftHeaderLayoutParams = leftHeader.getLayoutParams();
LayoutParams rightHeaderLayoutParams = rightHeader.getLayoutParams();
TableLayout contentTable = (TableLayout) findViewById(R.id.maintable);
contentTable.removeAllViews();
for (int i = list.size() - 1; i >= 0; i--) {
TextView tv1 = new TextView(this);
tv1.setTextColor(Color.BLACK);
tv1.setGravity(Gravity.CENTER);
tv1.setBackgroundColor(Color.LTGRAY);
tv1.setLayoutParams(leftHeaderLayoutParams);
tv1.setText(String.valueOf(list.get(i).getLevel()));
TextView tv2 = new TextView(this);
tv2.setTextColor(Color.BLACK);
tv2.setGravity(Gravity.CENTER);
tv2.setBackgroundColor(Color.WHITE);
tv2.setLayoutParams(rightHeaderLayoutParams);
tv2.setText(list.get(i).getDurationTimeInString());
TableRow tblRow = new TableRow(this);
tblRow.addView(tv1);
tblRow.addView(tv2);
contentTable.addView(tblRow);
}
}
After I use removeAllViews or any addView function the layout of the top most table changes as I explained before. Why does this happen?
You can fix this by changing the android:layout_width="30dip" and android:layout_width="70dip" in leftHeader and rightHeader to android:layout_width="fill_parent". I have to admit that I'm not entirely certain why that is, though.
Instead of clearing and re-filling a TableLayout every time, though, you might want to consider using a ListAdapter. It's much more suited to what you're doing (and will probably perform better, too).
UPDATE NUMBER 2:
Notice the change in the main.xml and the change in the onCreate method. This will make your header static while letting your listview scroll, and changing from TableLayout to LinearLayout for your buttons will fix the alignment issue.
Here's how to do this with a custom adapter instead of TableLayout (which isn't really meant for dynamic data like this):
Change you main.xml to this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:addStatesFromChildren="false" android:layout_height="wrap_content">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_alignParentTop="true" android:id="#+id/header">
<TextView android:id="#+id/leftHeader" android:textColor="#android:color/black"
android:layout_margin="1dp" android:gravity="center_horizontal"
android:layout_weight="1" android:background="#ffcccccc" android:text="textview1"
android:layout_width="fill_parent" android:layout_height="wrap_content"/>
<TextView android:id="#+id/rightHeader" android:textColor="#android:color/black"
android:gravity="center_horizontal" android:layout_weight="1"
android:layout_margin="1dp" android:background="#android:color/white"
android:text="textview2" android:layout_width="fill_parent" android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout android:id="#+id/buttonTable" android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:background="#android:color/black"
android:layout_alignParentBottom="true" >
<Button android:id="#+id/button_ResetData"
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:text="Reset Data" android:layout_gravity="center"
android:layout_weight="1" />
<Button android:id="#+id/button_Refresh"
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:text="Refresh" android:gravity="center"
android:layout_weight="1" />
</LinearLayout>
<ListView android:id="#+id/listView" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_weight="1"
android:fadingEdge="none"
android:layout_below="#id/header" android:layout_above="#id/buttonTable">
</ListView>
Add XML layout files for your rows (row.xml):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView android:id="#+id/maintableLeft"
android:layout_height="wrap_content" android:layout_weight="1"
android:layout_marginRight="1dp" android:layout_marginLeft="1dp"
android:gravity="center_horizontal" android:background="#ffcccccc"
android:textColor="#android:color/black" android:layout_width="fill_parent" />
<TextView android:id="#+id/maintableRight"
android:layout_height="wrap_content" android:background="#ffcccccc"
android:textColor="#android:color/black" android:layout_weight="1"
android:layout_marginLeft="1dp" android:layout_marginRight="1dp"
android:gravity="center_horizontal" android:layout_width="fill_parent" />
</LinearLayout>
Now everything is in place to create a custom class from BaseAdapter to display your information (and make your layout work better):
class CustomAdapter extends BaseAdapter {
private List<BatteryInfo> info;
private LayoutInflater inflater;
public CustomAdapter(Context context, List<BatteryInfo> x) {
this.info = x;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return info.size();
}
public Object getItem(int position) {
return info.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v;
v = inflater.inflate(R.layout.row, parent, false);
BatteryInfo b = (BatteryInfo) getItem(position);
((TextView) v.findViewById(R.id.maintableLeft)).setText(b
.getDurationTimeInString());
((TextView) v.findViewById(R.id.maintableRight)).setText(b
.getLevel());
return v;
}
}
Modify your onCreate method to inflate the header, add it, and then bind your data to your ListView using the new CustomAdapter (I've created a fake BatteryInfo class for this example, you'll need to modify things slightly to get this working):
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ListView lv = (ListView) this.findViewById(R.id.listView);
final Context context = this;
Button refresh = (Button) this.findViewById(R.id.button_Refresh);
refresh.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
List<BatteryInfo> x = new ArrayList<BatteryInfo>();
x.add(new BatteryInfo());
x.add(new BatteryInfo());
x.add(new BatteryInfo());
x.add(new BatteryInfo());
lv.setAdapter(new CustomAdapter(context, x));
}
});
}

Categories

Resources