Customized ListView with TextView, TextView, RadioGroup in Android - android

I'm developing an application with ListView for Student Marklist creation.
In this application, the List have 10 students. There are four grades provided for the exam which was conducted to those Students. One student can adapt only one grade from the four.
The teacher will assign the grade to the student in ListView.
My xml file Studentlist.xml is as follow:
<?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" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
and my row.xml file is as follow:
<?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="40dp"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/StudNo"
style="#style/text"
android:layout_width="80dp"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/StudName"
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/radio0"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:checked="true" />
<RadioButton
android:id="#+id/radio1"
android:layout_width="40dp"
android:layout_height="wrap_content" />
<RadioButton
android:id="#+id/radio2"
android:layout_width="40dp"
android:layout_height="wrap_content" />
<RadioButton
android:id="#+id/radio3"
android:layout_width="40dp"
android:layout_height="wrap_content" />
</RadioGroup>
</TableRow>
Now I'm trying for the output in the form of:
1 0001 AAAA <RadioButton1> <RadioButton2> <RadioButton3> <RadioButton4>
2 0002 BBBB <RadioButton1> <RadioButton2> <RadioButton3> <RadioButton4>
How can I use the Adapter functionalities?
ArrayAdapter<String,String,String,RadiGroup> studList=new ArrayAdapter<String,String,String,RadiGroup>();
Can I use like this, and how to develop the customized Adapter for the ListView?
Suggest me for the best solution!

This is a sample example of custom adapter for listview. Modify it as your need:
XML layout :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="#+id/main"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Header -->
<LinearLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/black" >
<TextView
android:id="#+id/item2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:height="30dip"
android:text="Latest News - Your Healing Place"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF"
android:width="100dip" />
<TextView
android:id="#+id/item1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:height="30dip"
android:width="20dip" />
</LinearLayout>
<View android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="?android:attr/listDivider" />
<LinearLayout android:id="#+id/layout"
android:layout_width="wrap_content"
android:layout_height="fill_parent">
<ListView
android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/black" >
</ListView>
</LinearLayout>
</LinearLayout>
news.java :
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
String[] from = new String[] {"details", "date"};
int[] to = new int[] { R.id.item1, R.id.item2};
ListView lv= (ListView)findViewById(R.id.listview);
HashMap<String, String> map = new HashMap<String, String>();
map.put("details", "This is a sample message");
map.put("date", "24-07-2003");
fillMaps.add(map);
SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.grid_item, from, to);
lv.setAdapter(adapter);

You can use a class inheriting from ArrayAdapter, and overriding its getView() method.
public class StudentAdapter extends ArrayAdapter<Student> {
protected LayoutInflater inflater;
public StudentAdapter(final Context context) {
super(context, 0);
inflater = (LayoutInflater) ((Context) context)
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public View getView(int position, View convertView, ViewGroup parent) {
// Note: You should optimize here with re-using convertView
View rowView = inflater.inflate(R.layout.user_address_row_layout,
parent, false);
TextView sNo = (TextView) rowView.findViewById(R.id.sNo);
sNo.setText(getItem(position).number);
// same for every field of the row
// ...
return rowView;
}
}

Related

How to get the width of each child contained in the list_item using ArrayAdapter

I am creating a ListView which has some some static headers as shown in the following picture:
I want that the header columns (Date, Status, Latitude, and Longitude) adapts their width according to the width of their according column.
What i am trying is to get the Layout of the list item associated with the adapter so that i can find the width of its childs (but it seems to be impossible) in my ListActivity.
then i have tried in my custom ArrayAdapter's getView method to retrieve the width of each TextView associated with the list item as shown in the following code: (But it returns null, I think it can not return width before redering the textView.)
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
DispatchHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new DispatchHolder();
holder.tvcurrentDateTime = (TextView) row
.findViewById(R.id.tvCurrentDateTimeLog);
holder.tvAction = (TextView) row.findViewById(R.id.tvActionLog);
holder.tvLatitude = (TextView) row.findViewById(R.id.tvlatitudeLog);
holder.tvLongitude = (TextView) row
.findViewById(R.id.tvlogitudeLog);
row.setTag(holder);
} else {
holder = (DispatchHolder) row.getTag();
}
holder.tvcurrentDateTime.setText(data.get(position).getTime());
int i = holder.tvcurrentDateTime.getWidth() <------- Here it is return 0
holder.tvAction.setText(data.get(position).getAction());
holder.tvLatitude.setText(data.get(position).getLatitude());
holder.tvLongitude.setText(data.get(position).getLogitude());
return row;
}
I have searched on it but i could not find a way to do so.
Here is the related code:
My ListActivity:
public class Activity_Log extends ListActivity {
PickupStatusDao puDao;
LogAdapter adapter;
List<LogModel> listOfDispatch;
private Object mItem;
private View childView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_log_xml);
puDao = new PickupStatusDao(this);
List<LogModel> lstResult = puDao.readAllLogData();
adapter = new LogAdapter(this, R.layout.log_item_row, lstResult);
setListAdapter(adapter);
}
}
My list_item (R.layout.log_item_row):
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
android:id="#+id/BodyTable"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="#+id/tableRow2"
style="#style/BodyRow"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/tvCurrentDateTimeLog"
style="#style/BodyText"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="this time"
android:textSize="20sp" />
<TextView
android:id="#+id/tvActionLog"
style="#style/BodyText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="this is action"
android:textSize="20sp" />
<TextView
android:id="#+id/tvlogitudeLog"
style="#style/BodyText"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="logitude"
android:textSize="20sp" />
<TextView
android:id="#+id/tvlatitudeLog"
style="#style/BodyText"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="latuide"
android:textSize="20sp" />
</TableRow>
</TableLayout>
List Activity Layout (R.layout.activity_log_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" >
<TableLayout
android:id="#+id/HeaderTable"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow style="#style/HeaderRow" >
<TextView
style="#style/HeaderText"
android:layout_width="239dp"
android:text="Date" />
<TextView
style="#style/HeaderText"
android:layout_width="156dp"
android:text="Status" />
<TextView
style="#style/HeaderText"
android:layout_width="121dp"
android:text="Latitude" />
<TextView
style="#style/HeaderText"
android:layout_width="121dp"
android:text="Longitude" />
</TableRow>
</TableLayout>
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</ListView>
</LinearLayout>
Unfortunately, this just isn't something you can do efficiently with an AdapterView. Since they tie together an arbitrarily long data source, but only display a small subset of the data at any one time, there is no efficient way to determine what the maximum width of the data will be without measuring a view for each piece of data.
I would suggest maybe doing some work when initializing the data source to find the average length of the text that will be displayed, and setting each column to some value that makes sense for the data. Or, just use the same values for each column (be that some combination of layout_weights or some hardcoded values).
I have achieved this by XML android:layout_weight property, it works on all android devices with different densities, Here is the updated UI picture:
the updated XML of ListActivity (activity_log_xml.xml) and for list_item (log_item_row.xml) are as follows respectively:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
android:id="#+id/HeaderTable"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
style="#style/HeaderRow"
android:weightSum="10" >
<TextView
android:id="#+id/dateHeaderTV"
style="#style/HeaderText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="Date" />
<TextView
android:id="#+id/statusHeaderTV"
style="#style/HeaderText"
android:layout_width="0dp"
android:layout_weight="4"
android:text="Status" />
<TextView
android:id="#+id/latitudeHeaderTV"
style="#style/HeaderText"
android:layout_width="0dp"
android:layout_weight="1.5"
android:text="Latitude" />
<TextView
android:id="#+id/longitudeHeaderTV"
style="#style/HeaderText"
android:layout_width="0dp"
android:layout_weight="1.5"
android:text="Longitude" />
</TableRow>
</TableLayout>
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</ListView>
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
android:id="#+id/BodyTable"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="#+id/tableRow2"
style="#style/BodyRow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="10" >
<TextView
android:id="#+id/tvCurrentDateTimeLog"
style="#style/BodyText"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:text="this time" />
<TextView
android:id="#+id/tvActionLog"
style="#style/BodyText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:text="this is action" />
<TextView
android:id="#+id/tvlogitudeLog"
style="#style/BodyText"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.5"
android:text="logitude" />
<TextView
android:id="#+id/tvlatitudeLog"
style="#style/BodyText"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.5"
android:text="latuide" />
</TableRow>
</TableLayout>

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"

Two ListViews inside a Linear Layout

PLease help me with layout of the two list views placed vertically in the Linear Layout. Problem is that if list1 had more data than list2 is not visible and it not scrollable, using scollview is not solution.
<?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:background="#drawable/backrepeat"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/shape_calendar_top"
android:orientation="horizontal" >
<ImageView
android:id="#+id/prevMonth"
android:layout_width="50dp"
android:layout_height="55dp"
android:src="#drawable/button_events_previous" >
</ImageView>
<TextView
android:id="#+id/currentMonth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="0.6"
android:gravity="center_vertical|center_horizontal"
android:text="Janauary"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#d458b1"
android:textStyle="bold" >
</TextView>
<ImageView
android:id="#+id/nextMonth"
android:layout_width="50dp"
android:layout_height="55dp"
android:src="#drawable/button_events_next" />
</LinearLayout>
<TextView
android:id="#+id/direction_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_marginTop="10dp"
android:background="#DA81F5"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:text="PRESS RELEASES"
android:textColor="#FFFFFF"
android:textSize="13dip" />
<ListView
android:id="#+id/eventsList"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/direction_label"
android:background="#ffffff" >
</ListView>
<TextView
android:id="#+id/direction_label1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#DA81F5"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:text="SPEECHES"
android:textColor="#FFFFFF"
android:textSize="13dip" />
<ListView
android:id="#+id/eventsList1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/direction_label1"
android:background="#ffffff" >
</ListView>
<TextView
android:id="#+id/empty_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
If I use the scrollview then it is wrapping up the listview for only first row.
Please suggest something.
EDIT :
<?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:background="#drawable/backrepeat"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/shape_calendar_top"
android:orientation="horizontal" >
<ImageView
android:id="#+id/prevMonth"
android:layout_width="50dp"
android:layout_height="55dp"
android:src="#drawable/button_events_previous" >
</ImageView>
<TextView
android:id="#+id/currentMonth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="0.6"
android:gravity="center_vertical|center_horizontal"
android:text="Janauary"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#d458b1"
android:textStyle="bold" >
</TextView>
<ImageView
android:id="#+id/nextMonth"
android:layout_width="50dp"
android:layout_height="55dp"
android:src="#drawable/button_events_next" />
</LinearLayout>
<TextView
android:id="#+id/direction_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_marginTop="10dp"
android:background="#DA81F5"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:text="PRESS RELEASES"
android:textColor="#FFFFFF"
android:textSize="13dip" />
<ListView
android:id="#+id/eventsList"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_below="#+id/direction_label"
android:background="#ffffff" >
</ListView>
<TextView
android:id="#+id/direction_label1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#DA81F5"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:text="SPEECHES"
android:textColor="#FFFFFF"
android:textSize="13dip" />
<ListView
android:id="#+id/eventsList1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_below="#+id/direction_label1"
android:background="#ffffff" >
</ListView>
<TextView
android:id="#+id/empty_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
If I use this, nothing is shown :(
Asmi
EDIT : CODE FOR THE TWO LISTVIEWS:
{
arrayofWebData.add(cn);
listAdapter = new SelectArralAdapter(getActivity(),
arrayofWebData);
List_events.setAdapter(listAdapter);
}
For the Second ListView it is as follows :
if ((year == event_year) && (event_month == month))
{
arrayofWebDataPress.add(cn);
listAdapter_press = new SelectArralAdapter_Press(getActivity(),arrayofWebDataPress);
List_events_press.setAdapter(listAdapter_press);
i++;
}
Array Adapter for the two ListViews :
class SelectArralAdapter_Press extends ArrayAdapter<PressDB> {
private LayoutInflater inflater;
public SelectArralAdapter_Press(Context context,
ArrayList<PressDB> arrayofWebDataPress) {
super(context, R.layout.speech_list_item, R.id.event_title,
arrayofWebDataPress);
inflater = LayoutInflater.from(context);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder_Press holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.speech_list_item, null);
holder = new ViewHolder_Press(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder_Press) convertView.getTag();
}
holder.populateFrom(arrayofWebDataPress.get(position));
return (convertView);
}
}
class ViewHolder_Press{
public TextView event_name = null;
public TextView event_date = null;
public ViewHolder_Press(View row) {
event_name = (TextView) row.findViewById(R.id.event_title);
event_date = (TextView) row.findViewById(R.id.event_date_time);
}
void populateFrom(PressDB eventsMainDB) {
event_name.setText(eventsMainDB.press_name);
event_date.setText(eventsMainDB.press_date + " ");
}
}
For the second Adaper :
class SelectArralAdapter extends ArrayAdapter<SpeechDB> {
private LayoutInflater inflater;
public SelectArralAdapter(Context context,
ArrayList<SpeechDB> arrayofWebData) {
super(context, R.layout.speech_list_item, R.id.event_title,
arrayofWebData);
inflater = LayoutInflater.from(context);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.speech_list_item, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.populateFrom(arrayofWebData.get(position));
return (convertView);
}
}
class ViewHolder {
public TextView event_name = null;
public TextView event_date = null;
public ViewHolder(View row) {
event_name = (TextView) row.findViewById(R.id.event_title);
event_date = (TextView) row.findViewById(R.id.event_date_time);
}
void populateFrom(SpeechDB eventsMainDB) {
event_name.setText(eventsMainDB.speech_name);
event_date.setText(eventsMainDB.speech_date + " ");
}
}
Try this.
If you want to go with the xml design then you have to give the fix size, either by using the weight (this is most preferable) or by giving the fix size in dp.
<?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:background="#drawable/backrepeat"
android:weightSum="1"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/shape_calendar_top"
android:orientation="horizontal" >
<ImageView
android:id="#+id/prevMonth"
android:layout_width="50dp"
android:layout_height="55dp"
android:src="#drawable/button_events_previous" >
</ImageView>
<TextView
android:id="#+id/currentMonth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="0.6"
android:gravity="center_vertical|center_horizontal"
android:text="Janauary"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#d458b1"
android:textStyle="bold" >
</TextView>
<ImageView
android:id="#+id/nextMonth"
android:layout_width="50dp"
android:layout_height="55dp"
android:src="#drawable/button_events_next" />
</LinearLayout>
<TextView
android:id="#+id/direction_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_marginTop="10dp"
android:background="#DA81F5"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:text="PRESS RELEASES"
android:textColor="#FFFFFF"
android:textSize="13dip" />
<ListView
android:id="#+id/eventsList"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:layout_below="#+id/direction_label"
android:background="#ffffff" >
</ListView>
<TextView
android:id="#+id/direction_label1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#DA81F5"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:text="SPEECHES"
android:textColor="#FFFFFF"
android:textSize="13dip" />
<ListView
android:id="#+id/eventsList1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:layout_below="#+id/direction_label1"
android:background="#ffffff" >
</ListView>
<TextView
android:id="#+id/empty_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
i am getting this screen at design time.
i have used like:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFF">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
// first linearlayout which is holding one textview and a listview
<LinearLayout
">
<TextView
/>
<ListView
android:id="#+id/failed_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="5dp"/>
</LinearLayout>
// second linearlayout which is holding one textview and a listview
<LinearLayout
">
<TextView
/>
<ListView
android:id="#+id/failed_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="5dp"/>
</LinearLayout>
// third linearlayout which is holding one textview and a listview
<LinearLayout
">
<TextView
/>
<ListView
android:id="#+id/failed_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="5dp"/>
</LinearLayout>
// and finally i have added a button in the parent linearlayout
<Button
/>
</LinearLayout>
</ScrollView>
you can try this.
Try out as below :
Here is the solution to more than one ListView within the same layout file. You can set the layout files as the below layout by providing the equal weights to each layout inside the LinearLayout.
<LinearLayout android:layout_weight="1"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<ListView android:id="#+id/list1"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
</ListView>
</LinearLayout>
<LinearLayout android:layout_weight="1"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<ListView android:id="#+id/list2"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
</ListView>
</LinearLayout>
Java Code
public class MainActivity extends Activity {
private ListView lv1 = null;
private ListView lv2 = null;
private String s1[] = {"a", "b", "c", "d", "e", "f"};
private String s2[] = {"r", "s", "t", "u", "v", "w", "x"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv1 = (ListView) findViewById (R.id.list1);
lv2 = (ListView) findViewById (R.id.list2);
lv1.setAdapter(new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, s1));
lv2.setAdapter(new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, s2));
}
}
For more details you can refer the Link which have given the solution for same.
I hope it will help you.
Thanks.
You can take something like this.
<LinearLayout>
<ScrollView>
<LinearLayout>
{Your All code of controls}
</LinearLayout>
</ScrollView>
</LinearLayout>
OR uo can take <ScrollView> as parent tag..
You can do something like this. as per my Knowledge...
I solved the issue by using section headers and combining the two listviews into one listview and categorizing them by help of Section headers.

How to separate header/footer and data in listview?

my app needs to display data in a list, I walked through some tutorials and figured out to use a header.xml and row.xml. It works fine but now I would like to add more stuff on my header.xml and I found out that the entire header.xml is scrolling with the row.xml as well, which I dont really want.
Any solution that doesn't require me to rewrite and change my code style completely?
Activity:
public class HistoryActivity extends ListActivity
{
private static final String TAG = "HistoryActivity";
ListView lv;
SimpleAdapter sd;
RecordDAO dao = new RecordDAO(HistoryActivity.this);
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
lv = getListView();
lv.addHeaderView(getLayoutInflater().inflate(
R.layout.header, null, false));
}
#Override
protected void onResume()
{
super.onResume();
ArrayList<Record> Records = new ArrayList<Record>();
Records = (ArrayList<Record>) dao.findAll();
ArrayList<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
for (int x = Records.size()-1; x >=0; x--)
{
map = new HashMap<String, String>();
map.put(....// all my data
aList.add(map);
}
sd = new SimpleAdapter(this, aList, R.layout.row,
new String[]
{ "date", "name", "time",
"rating" }, new int[]
{ R.id.date, R.id.name, R.id.time,
R.id.rating});
lv.setAdapter(sd);
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View view, int arg2,
long arg3)
{
TextView tx = (TextView) view.findViewById(R.id.date);
String s = tx.getText().toString();
Intent intent = new Intent(HistoryActivity.this, EditRecordActivity.class);
intent.putExtra("date", s);
startActivity(intent);
}
});
}
private void insertNewRecord()
{
dao.add(newRecord);
}
}
header.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:orientation="horizontal"
android:paddingBottom="6dip"
android:paddingTop="4dip" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Date"
android:textColor="#FFFFFF"
android:textSize="16dp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="name"
android:textColor="#FFFFFF"
android:textSize="16dp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Time"
android:textColor="#FFFFFF"
android:textSize="16dp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Rating"
android:textColor="#FFFFFF"
android:textSize="16dp" />
</LinearLayout>
</LinearLayout>
row.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:paddingBottom="6dip"
android:paddingTop="4dip" >
<TextView
android:id="#+id/date"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#FFFFFF" />
<TextView
android:id="#+id/name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_weight="1"
android:textColor="#FFFFFF" />
<TextView
android:id="#+id/time"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#FFFFFF" />
<TextView
android:id="#+id/Rating"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#FFFFFF" />
</LinearLayout>
Don't use ListActivity. I think it is a bad practice. Use a regular Activity. Just insert a ListView with an id "#android:id/list" into header.xml.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:orientation="horizontal"
android:paddingBottom="6dip"
android:paddingTop="4dip" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Date"
android:textColor="#FFFFFF"
android:textSize="16dp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="name"
android:textColor="#FFFFFF"
android:textSize="16dp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Time"
android:textColor="#FFFFFF"
android:textSize="16dp" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Rating"
android:textColor="#FFFFFF"
android:textSize="16dp" />
</LinearLayout>
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
And you can get the list reference by doing:
ListView lv = (ListView)findViewById(R.id.list);
Setting the Header of a ListView means that it will be the first item of your list, but still will scroll with the list itself.
In your case, you can create some kind of layout (similar to header.xml) and place it above the ListView.
Instead of ListActivity, extend from Activity
Create a main_layout.xml that define some views and a ListView.
main_layout
<include header.xml/>
<listview>
If you don't want your header and footer views to scroll with your ListView, then make sure that you use the addHeaderView() and addFooterView() BEFORE you call your setAdapter() method. I would recommend moving the setAdapter() method to your onCreate() method.

ListView won't show up anymore

I use the following main.xml for my app.
<?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:id="#+id/toplinear">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/linear">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Previous"
android:id="#+id/previous"
android:layout_weight="1"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Next"
android:id="#+id/next"
android:layout_toRightOf="#id/previous"
android:layout_weight="1"
/>
</LinearLayout>
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/android:list"
android:layout_weight="10"
/>
</LinearLayout>
I want to have 2 buttons at the top, previous and next and my listview with custom adapter below it. Few days ago I had only my ListView showing, going over the buttons. Now I seem to be stuck on the other side, where my listview doesn't show while my buttons do.
My activity extends on ListActivity and uses this code to fill it with a customadapter.
ListView lv = getListView();
lv.setAdapter(new MyCustomAdapter(this, R.layout.custom_list, nameResults));
public class MyCustomAdapter extends ArrayAdapter<String> {
private String[] nameResults;
private Context context;
public MyCustomAdapter(Context context, int textViewResourceId,
String[] names) {
super(context, textViewResourceId, names);
this.context = context;
nameResults = names;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.custom_list, parent, false);
}
TextView label = (TextView) row.findViewById(R.id.Name);
label.setText(nameResults[position]);
TextView descr = (TextView) row.findViewById(R.id.Description);
descr.setText(linkedResults.get(nameResults[position]));
return row;
}
}
I tried using "lv.addHeaderView(previous);" but that only gave me vague ClassCastExceptions about LayoutParams.
I think my problem is currently in my main.xml, as the Layout tab in Eclipse of it won't show the ListView either. It knows its there as it shows up in the outline tab, but the visual representation doesn't give it a red outline when I select it.
For completeness, my custom_list (aka row) layout 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"
android:background="#ffffff"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/Name"
android:textColor="#000000"
android:textStyle="bold"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/Description"
android:textColor="#000000"
/>
</LinearLayout>
I'm modified your main.xml a bit - added orientation and took off the layout_weight for the listview - hopefully this fixes that problem.
<?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"
android:id="#+id/toplinear">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/linear">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Previous"
android:id="#+id/previous"
android:layout_weight="1"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Next"
android:id="#+id/next"
android:layout_toRightOf="#id/previous"
android:layout_weight="1"
/>
</LinearLayout>
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/android:list"
/>
</LinearLayout>

Categories

Resources