Is there a way to display TableLayout in ListView - android

I have 10+ tables created in xml Tablelayout and i'm trying to
put them in ListView rather than in a ScrollView .
Tables are not editable and just for info... How to put them as
data source in Adapter or i need to Inflate or what? tnx and sorry for noobish q..

You need to create custom Adapter for the ListView(please search this). With the adapter you need to create ListView item layout, where you can define your table for each row in the List View.
Hope this helps. :)

You can do it by simply adding TableLayout to ListView row layout xml.
Example from my current application:
public class DistributorsListAdapter extends ArrayAdapter<String>{
private final Context context;
private final int rowResourceId;
private final JSONArray data;
public DistributorsListAdapter(Context context, int rowResourceId, String[] ids, JSONArray data) {
super(context, rowResourceId, ids);
this.context = context;
this.rowResourceId = rowResourceId;
this.data = data;
}
#Override
public View getView(int position, View v, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(rowResourceId, parent, false);
try {
JSONObject rowData = data.getJSONObject(position);
// Заполняем данными строку списка
setHTMLData(rowView, rowData, "name", R.id.distributor_table_title );
setHTMLData(rowView, rowData, "address", R.id.distributor_address );
setData(rowView, rowData, "website", R.id.distributor_website );
setData(rowView, rowData, "phone", R.id.distributor_phone );
setData(rowView, rowData, "fax", R.id.distributor_fax );
setData(rowView, rowData, "email", R.id.distributor_email );
} catch (JSONException e) {
e.printStackTrace();
}
return rowView;
}
private void setData(View v, JSONObject data, String key, int textViewId) throws JSONException{
TextView tv = (TextView) v.findViewById(textViewId);
tv.setText(data.getString(key));
}
private void setHTMLData(View v, JSONObject data, String key, int textViewId) throws JSONException{
TextView tv = (TextView) v.findViewById(textViewId);
tv.setText( Html.fromHtml( data.getString(key) ) );
}
}
XML layout of a ListView row:
<?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:paddingLeft="#dimen/activity_horizontal_padding"
android:paddingRight="#dimen/activity_horizontal_padding"
android:paddingBottom="#dimen/distributor_table_vertical_padding">
<TextView
android:id="#+id/distributor_table_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/distributor_table_vertical_padding"
android:gravity="center_horizontal"
android:textSize="#dimen/distributor_table_title_text_size"
android:textStyle="bold"/>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/distributor_address"
android:textSize="#dimen/distributor_table_text_size"/>
<TextView
android:id="#+id/distributor_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/distributor_table_text_size"
android:layout_marginLeft="#dimen/distributor_table_columns_margin"/>
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/distributor_website"
android:textSize="#dimen/distributor_table_text_size"/>
<TextView
android:id="#+id/distributor_website"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/distributor_table_text_size"
android:layout_marginLeft="#dimen/distributor_table_columns_margin"/>
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/distributor_phone"
android:textSize="#dimen/distributor_table_text_size"/>
<TextView
android:id="#+id/distributor_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/distributor_table_text_size"
android:layout_marginLeft="#dimen/distributor_table_columns_margin"/>
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/distributor_fax"
android:textSize="#dimen/distributor_table_text_size"/>
<TextView
android:id="#+id/distributor_fax"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/distributor_table_text_size"
android:layout_marginLeft="#dimen/distributor_table_columns_margin"/>
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/distributor_email"
android:textSize="#dimen/distributor_table_text_size"/>
<TextView
android:id="#+id/distributor_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/distributor_table_text_size"
android:layout_marginLeft="#dimen/distributor_table_columns_margin"/>
</TableRow>
</TableLayout>
</LinearLayout>

Related

How to put titles for each field of a ListView item in Android

I have a Listview with a custom adapter , which displays person's data like
Barak Obama 2008
But I want to put a header with Name Surname Date but properly alligned
A draft example:
ListView Items with titles
How Can I achieve this?
thank you
Custom ListView, All what you need, A xml layout for row with adapter and listview
check out this link explain Custom listview
https://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView
You can use TableLayout to show your Data as your requirement. I've replicated your demand. Here are the codes.
1) MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lstview=(ListView)findViewById(R.id.listView);
// Inflate header view
ViewGroup headerView = (ViewGroup)getLayoutInflater().inflate(R.layout.header_layout, lstview,false);
// Add header view to the ListView
lstview.addHeaderView(headerView);
// Get the string array defined in strings.xml file
String[] date = {"29-03-17", "29-03-17"};
String[] amount = {"5", "2"};
String[] dept = {"14", "4"};
// Create an adapter to bind data to the ListView
CustomListViewAdapter adapter=new CustomListViewAdapter(this,R.layout.row_listview, R.id.tvDate, date, amount, dept);
// Bind data to the ListView
lstview.setAdapter(adapter);
}
2) CustomListViewAdapter.java
public class CustomListViewAdapter extends ArrayAdapter<String> {
int groupid;
String[] date;
String[] amount;
String[] dept;
ArrayList<String> desc;
Context context;
public CustomListViewAdapter(Context context, int vg, int id, String[] date, String[] amount, String[] dept){
super(context,vg, id, date);
this.context=context;
groupid=vg;
this.date=date;
this.amount=amount;
this.dept=dept;
}
// Hold views of the ListView to improve its scrolling performance
static class ViewHolder {
public TextView tvDate;
public TextView tvAmount;
public TextView tvDept;
}
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
// Inflate the rowlayout.xml file if convertView is null
if(rowView==null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView= inflater.inflate(groupid, parent, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.tvDate= (TextView) rowView.findViewById(R.id.tvDate);
viewHolder.tvAmount= (TextView) rowView.findViewById(R.id.tvAmount);
viewHolder.tvDept= (TextView) rowView.findViewById(R.id.tvDept);
rowView.setTag(viewHolder);
}
// Set text to each TextView of ListView item
ViewHolder holder = (ViewHolder) rowView.getTag();
holder.tvDate.setText(date[position]);
holder.tvAmount.setText(amount[position]);
holder.tvDept.setText(dept[position]);
return rowView;
}
}
3) HeaderLayout (header_layout.xml)
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="*"
>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/tvDate"
android:layout_column="0"
android:padding="10sp"
android:gravity="left"
android:layout_span="1"
android:text="Date"
android:layout_gravity="center">
</TextView>
<TextView
android:id="#+id/tvAmount"
android:layout_column="1"
android:padding="10sp"
android:gravity="left"
android:layout_span="1"
android:text="Amount"
android:layout_gravity="center">
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvDept"
android:layout_column="2"
android:layout_span="1"
android:text="Dept"
android:layout_gravity="center" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvDelete"
android:layout_column="3"
android:layout_span="1"
android:text="Delete"
android:layout_gravity="center" />
</TableRow>
</TableLayout>
4) RowLayout for each ListView row (row_listview.xml)
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="*">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tvDate"
android:layout_column="0"
android:padding="10sp"
android:gravity="left"
android:layout_span="1"
android:text="Date"
android:layout_gravity="center">
</TextView>
<TextView
android:id="#+id/tvAmount"
android:layout_column="1"
android:padding="10sp"
android:gravity="left"
android:layout_span="1"
android:text="Amount"
android:layout_gravity="center">
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvDept"
android:layout_column="2"
android:layout_span="1"
android:text="Dept"
android:layout_gravity="center" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton"
android:layout_column="3"
android:layout_span="1"
android:src="#android:drawable/ic_delete" />
</TableRow>
</TableLayout>
5) Output:
Hope this helps.

How to get two textviews in a listview on the same line?

I have two TextViews tvA and tvB which appear in a ListView lvA.
tvA and tvB display data pulled from a Sqlite Database using Cursor.
Cursor cursor = myDb.getAllRows();
String[] from = new String[]
{DBAdapter.KEY_A,DBAdapter.KEY_B};
int[] to = new int[]
{R.id.tvA,R.id.tvB};
SimpleCursorAdapter myCursorAdapter =new SimpleCursorAdapter(this,R.layout.lvA, cursor,from,to);
myList = (ListView) findViewById(R.id.lvA);
myList.setAdapter(myCursorAdapter);
registerForContextMenu(myList);
The issue is that tvA and tvB doesn't appear as one item in lvA (i.e. they don't appear on the same line) and appear as two different items.
Below is the XML:
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="5dip"
>
<TextView
android:id="#+id/tvA"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"/>
<TextView
android:id="#+id/tvB"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:layout_toRightOf="#id/tvA"/>
</RelativeLayout>
If you want both the TextViews in the same align then you can use LinearLayout with android:layout_weight attribute.
To have equal width TextView, give same weight value.
You can use TableLayout
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TableRow
android:id="#+id/tableRow1"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<TextView android:id="#+id/tvA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
</TextView>
<TextView android:id="#+id/tvB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
</TextView>
</TableRow></TableLayout >
or LinearLayout with android:orientation="horizontal"
and TextView with android:layout_weight="1"
Here's how I do it with three items on the same row = This is my item list filled from a cursor
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/TextView_StudentName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:maxWidth="75dp"
android:layout_gravity="left"
android:text="#string/student_id"
android:padding="5dp"></TextView>
<TextView
android:id="#+id/TextView_StudentLocation"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:maxWidth="75dp"
android:gravity="center"
android:text="#string/student_Location"
android:padding="5dp"></TextView>
<TextView
android:id="#+id/TextView_StudentClass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:maxWidth="75dp"
android:layout_alignParentRight="true"
android:text="#string/student_class" >
</TextView>
</RelativeLayout>`
I'm in the process of replacing the hard sizes with info from the dimen file, but so far this has worked.
TRY this :
Main Java:
//DECLARE:
private Map<String, Map<String, String>> mMenuView = new HashMap<String, Map<String, String>>();
//IN onCreateView
Adapter mAdapter = new NewAdapter(getActivity().getApplicationContext(), mMenuView);
mListView.setAdapter(mAdapter);
// ADD onActivityCreated or onStart save data from your DB and store in mMenuView
Map<String, String> tempMap = new HashMap<String, String>();
tempMap.put("TEXTVALE_A", datafromdb_A);
tempMap.put("TEXTVALE_B", datafromdb_B);
mMenuView.put(Integer.toString(mMenuView.size()), tempMap);
}
//NEWADAPTER
private class NewAdapter extends BaseAdapter {
private Context mContext;
private TextView mTitle1;
private TextView mTitle2;
private Map<String, Map<String, String>> mData = new HashMap<String, Map<String, String>>();
public NewAdapter(Context applicationContext,Map<String, Map<String, String>> mMenuValue) {
mContext = applicationContext;
mData = mMenuValue;
}
#Override
public int getCount() {
return mData.size();
}
#Override
public Map<String, String> getItem(int position) {
return mData.get(Integer.toString(position));
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final Map<String, String> data = mData.get(Integer.toString(position));
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(mContext.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.linear_layout, null);
mTitle1 = (TextView) convertView.findViewById(R.id.t1A);
mTitle2 = (TextView) convertView.findViewById(R.id.t1B);
}
if (data != null) {
mTitle1.setText(data.get("TEXTVALE_A"));
mTitle2.setText(data.get("TEXTVALE_B"));
}
return convertView;
}
}
linear_layout.xml
<LinearLayout
android:id="#+id/content_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:baselineAligned="false">
<LinearLayout
android:id="#+id/tab1_wrapper"
android:layout_width="0dip"
android:layout_height="50dp"
android:layout_weight="1"
android:orientation="vertical"
android:layout_gravity="center|center_horizontal"
>
<TextView
android:id="#+id/t1A"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:textSize="13sp"
android:gravity="center|center_horizontal"
android:textColor="#color/text_color3x"/>
</LinearLayout>
<LinearLayout
android:id="#+id/tab2_wrapper"
android:layout_width="0dip"
android:layout_height="50dp"
android:layout_weight="0.5"
android:orientation="vertical"
android:gravity="left|center_vertical|center_horizontal"
>
<TextView
android:id="#+id/A"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:textSize="13sp"
android:gravity="center|center_horizontal"
/>
</LinearLayout>
</LinearLayout>

Populating a multicolumn listview without a hashmap

I have been reading about multicolumn listviews today. They seem to be what I need, but can't info info about how to populate them not using a hash map. All examples I have seen use a hash map to build the listview data adapter but this is too heavy when lot of data is implied. Is there anyway to populate a list view without a hash list?
All examples I have seen use something like this:
http://saigeethamn.blogspot.com.es/2010/04/custom-listview-android-developer.html
Yes it is possible. please Check out the code.
public class List_view_ArrayAdapter extends ArrayAdapter<String> {
private Context context;
private String[] name;
private String[] address;
private String[] rating;
private TextView nameTextView;
private TextView addressTextView;
private ImageView lisiconImageView;
private AppManagers appManagers;
public List_view_ArrayAdapter(Context context, String[] name,
String[] address, String[] rating) {
super(context, R.layout.list_layout, name);
this.context = context;
this.name = name;
this.address = address;
this.rating = rating;
appManagers = new AppManagers();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_layout, parent, false);
nameTextView = (TextView) rowView.findViewById(R.id.nameTextView);
addressTextView = (TextView) rowView.findViewById(R.id.addressTextView);
lisiconImageView = (ImageView) rowView
.findViewById(R.id.listiconImageView);
nameTextView.setText(name[position]);
addressTextView.setText(address[position]);
lisiconImageView.setImageDrawable(appManagers
.ImageOperations(rating[position]));
// imageView.setImageResource(imageid[position]);
return rowView;
}
}
Here is list_layout for this line "R.layout.list_layout"
<?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="wrap_content"
android:orientation="horizontal" android:background="#d9d9d9">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" android:layout_marginBottom="5dp" android:layout_marginTop="5dp">
<ImageView
android:id="#+id/listiconImageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" android:layout_marginLeft="5dp" android:gravity="center_vertical" android:layout_marginBottom="5dp" android:layout_marginTop="5dp">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name : "
android:textColor="#2a170e" />
<TextView
android:id="#+id/nameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="#2a170e" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address : "
android:textColor="#2a170e" />
<TextView
android:id="#+id/addressTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="#2a170e" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Finaly you call it from your main Activity.
new List_view_ArrayAdapter arrayAdapter= new List_view_ArrayAdapter(context, appManagers.getNameArray(cafeandbarsList), name, address, rating);
listView.setAdapter(arrayAdapter);
I think it help you.
Thanks

Custom SimpleCursorAdapter not displaying data on screen

I've read and tried a dozen solutions from this forum but none of them seem to work - I cannot display data on my lists - view.findViewById(resourceid) returns null each time in bindView - for example when using TextView t = (TextView) view.findViewById(R.id.leftCell). It is strange behaviour considering i have no errors in Logcat, and the view itself isn't null. What am I doing wrong?
I've divided my layout as follows: two LinearLayouts each wrapping a ListView. I've defined a custom row structure for both lists, found in database_item.xml and list_item.xml.
Here's my code.
Main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:id="#+id/topFrame">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Devices loaded from Database: "></TextView>
<TableLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
<TableRow>
<TextView android:text="Name"
android:padding="3dip"
android:gravity="center"/>
<TextView android:text="Remote Address"
android:padding="3dip"
android:gravity="center"/>
<TextView android:text="Type"
android:padding="3dip"
android:gravity="center"/>
</TableRow>
</TableLayout>
<ListView android:id="#+id/databaseList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
</ListView>
</LinearLayout>
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1.2"
android:id="#+id/bottomFrame">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Detected Xbee nodes via 'introduce' command:"></TextView>
<TableLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
<TableRow>
<TextView android:text="Address"
android:padding="3dip"
android:gravity="center"/>
<TextView android:text="Type"
android:padding="3dip"
android:gravity="center"/>
</TableRow>
</TableLayout>
<ListView android:id="#+id/detectedNodesList"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</ListView>
</LinearLayout>
database_item.xml:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
<TableRow>
<CheckBox android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible"
android:id="#+id/checkBox"></CheckBox>
<TextView android:id="#+id/topLeftCell"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:gravity="center"></TextView>
<TextView android:id="#+id/topCenterCell"
android:padding="3dip"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
<TextView android:id="#+id/topRightCell"
android:gravity="center"
android:padding="3dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
</TableRow>
</TableLayout>
list_item.xml:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1">
<TableRow>
<TextView android:id="#+id/leftCell"
android:padding="3dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
<TextView android:id="#+id/rightCell"
android:gravity="left"
android:padding="3dip"
android:text="TESTING"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
</TableRow>
Relevant code from onCreate:
ListView topList = (ListView) findViewById(R.id.databaseList);
ListView bottomList = (ListView) findViewById(R.id.detectedNodesList);
String[] topColumns = new String[] {"name", "my", "type"};
String[] bottomColumns = new String[] {"my","type"};
int[] toTop = new int[] { R.id.topLeftCell, R.id.topCenterCell, R.id.topRightCell};
int[] toBottom = new int[] { R.id.leftCell, R.id.rightCell};
DbCursorAdapter e = new DbCursorAdapter(this, R.layout.database_item, cursor, topColumns, toTop);
DbCursorAdapter d = new DbCursorAdapter(this, R.layout.list_item, cursor, bottomColumns, toBottom);
topList.setAdapter(e);
bottomList.setAdapter(d);
and DbCursorAdapter.java:
public class DbCursorAdapter extends SimpleCursorAdapter {
private Context context;
private int layout;
private final String TAG = "DbCursorAdapter";
LayoutInflater inflater;
public DbCursorAdapter (Context context, int layout, Cursor c, String[] from, int[] to) {
super(context,layout,c,from,to);
this.context = context;
this.layout = layout;
inflater = LayoutInflater.from(context);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v = inflater.inflate(layout, parent, false);
return v;
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
Cursor c = cursor;
if (layout == R.layout.list_item) {
int addressCol = c.getColumnIndex("my");
int typeCol = c.getColumnIndex("type");
int address = c.getInt(addressCol);
int type = c.getInt(typeCol);
TextView t = (TextView) view.findViewById(R.id.leftCell);
TextView t2 = (TextView) view.findViewById(R.id.rightCell);
if (t != null) {
t.setText(Integer.toString(address));
}
if (t2 != null) {
t2.setText(Integer.toString(type));
}
}
if (layout == R.layout.database_item) {
int nameCol = c.getColumnIndex("name");
int addressCol = c.getColumnIndex("my");
int typeCol = c.getColumnIndex("type");
String name = c.getString(nameCol);
int my = c.getInt(addressCol);
int type = c.getInt(typeCol);
CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkBox);
checkBox.setVisibility(CheckBox.VISIBLE);
TextView t = (TextView) view.findViewById(R.id.topLeftCell);
TextView t2 = (TextView) view.findViewById(R.id.topCenterCell);
TextView t3 = (TextView) view.findViewById(R.id.topRightCell);
if (t != null) {
t.setText(name);
}
if (t2 != null) {
t2.setText(Integer.toString(my));
}
if (t3 != null) {
t3.setText(Integer.toString(type));
}
}
}
if (layout == R.layout.list_item)
The right way to access a resource id is
int list_item_id = context.getResources.getIdentifier(list_item,"layout", getPackageName());
if (layout == list_item_id)
this will return you the resource id of R.layout.list_item . I wanted to just let you know, but i don't know if this is the exact problem.
Much like Yashwanth, my intuition is telling me something is up with if(layout==R.layout.list_item). Have you considered just using separate adapters for each list?
Solved. The problem seemed to be in main.xml:
<TableLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
<TableRow>
<TextView android:text="Name"
android:padding="3dip"
android:gravity="center"/>
<TextView android:text="Remote Address"
android:padding="3dip"
android:gravity="center"/>
<TextView android:text="Type"
android:padding="3dip"
android:gravity="center"/>
</TableRow>
</TableLayout>
Somehow covered the ListView that was supposed to be below it for both the top frame and the bottom frame. Removing TableLayout shows both lists just fine.
Would anybody be able to explain why?

Proper usage of twoLineListItem Android

I am new to development on android and trying to create a list that has a bold header
and a smaller description for each item.
Like the one shown here (sorry can't post images yet):
http://i.stack.imgur.com/UVqzq.png
This is the XML that I have to start with:
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TwoLineListItem android:layout_width="fill_parent"
android:layout_height="wrap_content" android:mode="twoLine">
<TextView android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#android:id/text1" android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView android:layout_below="#android:id/text1"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_alignLeft="#android:id/text1" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="#android:id/text2" />
</TwoLineListItem>
</LinearLayout>
Is there a way to populate the list from an array in another xml file? If not how would I populate a list like this from the Java code? Like I said I'm new to development, so I'm probably way off. Thanks for any input!
You use a listview http://developer.android.com/reference/android/widget/ListView.html
In the case of an Array of Objects, you just have to implement your own adapter.
It can work for a TwoLineListItem or any other custom layout.
For example, if I have a list of items of the following type :
public class ProfileItem {
public String host, name, tag;
public ProfileItem(String host, String name, String tag) {
this.host = host;
this.name = name;
this.tag = tag;
}
#Override
public String toString() {
return name+"#"+tag;
}
}
Then I create the following adapter :
public class ProfileItemAdapter extends ArrayAdapter<ProfileItem> {
private Context context;
private int layoutResourceId;
private List<ProfileItem> objects = null;
public ProfileItemAdapter(Context context, int layoutResourceId, List<ProfileItem> objects) {
super(context, layoutResourceId, objects);
this.context = context;
this.layoutResourceId = layoutResourceId;
this.objects = objects;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if(v == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
v = inflater.inflate(layoutResourceId, parent, false);
}
ProfileItem item = objects.get(position);
TextView titletext = (TextView)v.findViewById(android.R.id.text1);
titletext.setText(item.toString());
TextView mainsmalltext = (TextView)v.findViewById(android.R.id.text2);
mainsmalltext.setText(item.host);
return v;
}
}
Then everything is in place, in my activity (or fragment), I just have to set this adapter in the onCreate method :
setListAdapter(new ProfileItemAdapter(getActivity(),
android.R.layout.two_line_list_item, // or my own custom layout
ProfileListContent.ITEMS));
I have a similar implementation, but instead of a 2 line listview, I have 3 lines.
This is the XML:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="#+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#111111"
android:textSize="17sp"
android:textStyle="bold"
android:text="PROJECT NAME"
android:typeface="monospace"
android:paddingBottom="2dp"
android:paddingTop="2dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:gravity="left|center" >
</TextView>
<TextView
android:id="#+id/text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#111111"
android:text="Selected Type"
android:textSize="16sp"
android:paddingBottom="1dp"
android:paddingTop="1dp"
android:paddingLeft="5dp"
android:paddingRight="5dp" >
</TextView>
<TextView
android:id="#+id/text3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#111111"
android:textSize="16sp"
android:paddingTop="1dp"
android:paddingBottom="1dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text="Project Description" >
</TextView>
</LinearLayout>
And this is the JAVA code for filling the lines with data returned from a DB. This code is in the onCreate() method:
String[] fields = new String[] { db.TABLE_PRJ_NAME, db.TABLE_PRJ_TYPE, db.TABLE_PRJ_DESC };
int[] views = new int[] { R.id.text1, R.id.text2, R.id.text3 };
c = db.getAllProjects();
startManagingCursor(c);
// Set the ListView
SimpleCursorAdapter prjName = new SimpleCursorAdapter(
this,
R.layout.project_list_rows,
//android.R.layout.simple_list_item_1,
c, fields, views);
setListAdapter(prjName);

Categories

Resources