Android ListView Background repeats background after each entry - java

I figured it out I was using the same layout for my on create and to show the data so it was repeating everyting.
I have a list of data showing and for some reason the background I have set looks fine but after each two text views the background is squished into a 20px window overlaying the orginal background.
My xml for my view looks like so:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:background="#drawable/patriot_bg2"
android:cacheColorHint="#00000000"
android:id="#id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:drawSelectorOnTop="true"
/>
<TextView
android:id="#+id/item_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:background="#00000000"
android:padding="2dp"
android:textSize="20dp" />
<TextView
android:id="#+id/item_subtitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#00000000"
android:cacheColorHint="#00000000"
android:padding="2dp"
android:textSize="13dp" />
</LinearLayout>
Below is the Java that places the text into the text fields and adds it to the screen:
for (int i = 0; i < nodes.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element)nodes.item(i);
map.put("main_content", XMLfunctions.getValue(e, "content"));
map.put("name", XMLfunctions.getValue(e, "name"));
mylist.add(map);
}
//
ListAdapter adapter = new SimpleAdapter(ShowXMLPAR.this, mylist , R.layout.listplaceholder,
new String[] {"main_content", "name" },
new int[] { R.id.item_title, R.id.item_subtitle });
setListAdapter(adapter);
its almost as if there is another listview after each set of textfields but I am not sure how to get rid of it. Please help.

I had to use a seperate layout for the ListView items so only that would repeat and not the main layout.

Related

Android custom List SimpleAdapter - chose row icon

Layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#android:color/black">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/icon"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:id="#+id/line_a"
android:textSize="14dp"
android:textColor="#android:color/white"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold"/>
<TextView android:id="#+id/line_b"
android:textSize="10dp"
android:textColor="#android:color/darker_gray"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
Java:
SimpleAdapter sa;
ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
HashMap<String,String> item;
for(int i = 0; i < j; i++){
item = new HashMap<String,String>();
item.put( "line1", ListData[i][0]);
item.put( "line2", ListData[i][1]);
item.put( "type", ListData[i][2]);
list.add( item );
}
sa = new SimpleAdapter(this, list,
R.layout.two_lines_list,
new String[] { "line1","line2" },
new int[] {R.id.line_a, R.id.line_b});
// can you add the setImageView here? If yes, how?
setListAdapter( sa );
}
How can I make the icon to get an image from res/drawable... depending on the type (ListData[i][2] inserted in the HashMap)? eg. if the type is "1" set icon1.png at the beginning of the 2-line-row
You should override SimpleAdapter.getView(int position, View convertView, ViewGroup parent) method, and get the list item with the current position(the first param in that method), then check the value set set the icon you want.
NOTE: you' d better use ViewHolder pattern in the getView method for performance.

android - how to inflate a map fragment in an already inflated layout?

i am creating a Table Layout in my an activity, adding dynamically table rows in a loop.
What i want to achieve, is to add a Google map fragment in each dynamically created Table row.
This is my Table row Layout:
<?xml version="1.0" encoding="utf-8">
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rl_rowitem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="15dp">
<RelativeLayout
android:id="#+id/inflated_row_relative"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tv_poi_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/orange"
android:textColor="#ffffff"
android:text="Name"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="#+id/img_snapshot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/tv_poi_name"
android:adjustViewBounds="true"
android:src="#drawable/map_snapshot" />
<TextView
android:id="#+id/tv_poi_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address"
android:textColor="#fe8301"
android:layout_below="#+id/img_snapshot"
android:textAppearance="?android:attr/textAppearanceMedium" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/distance_layout"
android:layout_below="#+id/tv_poi_address" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="20dp"
android:layout_height="20dp"
android:src="#drawable/distance_icon" />
<TextView
android:id="#+id/tv_poi_distance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/imageView1"
android:text="Distance"
android:paddingLeft="10dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
</RelativeLayout>
</TableRow>
And this is the loop that i am creating the Table rows dynamically:
final TableLayout tblAddLayout = (TableLayout) findViewById(R.id.tl_menu_pois);
TableRow inflateRow = (TableRow) View.inflate(Menu_pois.this, R.layout.menu_pois_rowitem, null);
for (int i=0;i<3;i++){
View inflate = (TableRow) View.inflate(Menu_pois.this, R.layout.menu_pois_rowitem, >tl_menu_pois);
RelativeLayout inflated_row_relative = (RelativeLayout) >inflate.findViewById(R.id.inflated_row_relative);
inflated_row_relative.setTag(i);
TextView poi_name = (TextView) inflate.findViewById(R.id.tv_poi_name);
TextView poi_address = (TextView) inflate.findViewById(R.id.tv_poi_address);
poi_name.setText("Name");
poi_address.setText("Address");
//set tag for each TableRow
inflate.setTag(i);
//add TableRows to TableLayout
tblAddLayout.addView(inflate);
//set click listener for all TableRows
inflated_row_relative.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Object tag = v.getTag();
String string_tag = tag.toString();
}
});
}
I want to display a Google map in each Table row like the image bellow (instead of the sample image, having the map):
http://goo.gl/9erYB7
Is there a way to implement this?
Thank you very much in advance.
I found a solution for adding multiple map fragment on the same View, following this example, in case someone has the same problem:
http://saadroid.blogspot.gr/2013/06/multiple-maps-in-same-view.html

Can't Get TableLayout to Programatically Be Created

I've created what I want in xml but I need to do it programatically and I can't get it to work. It's a table layout with one row. In that row there is two more table layouts. The first of which has one row containing a picture and the second table layout has two rows containing two textviews and a picture. Is there some kind of xml to java converter? I've tried to write the code now for a couple of hours and can't get the layouts to work. I'd post a picture but I need 10 rep points first. The whole xml file is in a linear layout if it helps.
<TableLayout
android:id="#+id/tablemain"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:gravity="center_horizontal" >
<TableRow
android:id="#+id/TableRow1"
android:background="#drawable/border"
android:weightSum="100" >
<TableLayout
android:id="#+id/Tableout2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0"
android:background="#drawable/border"
android:gravity="center_horizontal" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imageView1"
android:adjustViewBounds="true"
android:maxHeight="100dp"
android:maxWidth="100dp"
android:padding="3dp"
android:src="#drawable/unknown" />
</TableRow>
</TableLayout>
<TableLayout
android:id="#+id/Tableout3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="100"
android:gravity="center_horizontal"
android:weightSum="100" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="50"
android:background="#drawable/border" >
<TextView
android:id="#+id/Judgename"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:padding="7dp"
android:text="Name"
android:textSize="20sp"
android:textStyle="bold" />
</TableRow>
<TableRow
android:id="#+id/tablerowctroom"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="50"
android:background="#drawable/border"
android:weightSum="100" >
<TextView
android:id="#+id/Courtroom"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="70"
android:padding="7dp"
android:text="Room"
android:textSize="15sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:layout_marginRight="10dp"
android:layout_weight="30"
android:src="#drawable/umid" />
</TableRow>
</TableLayout>
</TableRow>
</TableLayout>
This is the new code I'm trying to use based on Nathan Walter's advice. However, It'll set the first set text but the rest are just the default value. Although the layout looks right. How can I set the textfields for each individual view when the method is called.
public void Display(String name, String ctroom) {
ViewGroup parent = (ViewGroup) findViewById(R.id.container);
view = LayoutInflater.from(this).inflate(R.layout.tablebutton, null);
parent.addView(view);
TextView jn = (TextView) findViewById(R.id.JudgenameCourts);
jn.setText(name);
TextView ct = (TextView) findViewById(R.id.Courtroomnew);
ct.setText(ctroom);
view = LayoutInflater.from(this).inflate(R.layout.tablebutton, null);
parent.addView(view);
jn.setText("Name1");
ct.setText("Courtroom1");
}
I think the code you want looks something like this. Let me know if it makes sense.
// Create an array of things to create views from.
// You will probably be getting your array from somewhere else
YourObjectHere[] things = new YourObjectHere[2];
things[0] = new YourObjectHere();
things[1] = new YourObjectHere();
// Get a reference to a LinearLayout to hold your views
LinearLayout list = (LinearLayout) findViewById(R.id.list);
// Loop through your list of objects
for(int i = 0; i < things.length; i++) {
// Inflate an instance of your layout
View listItem = getLayoutInflater().inflate(R.layout.child, null);
// Set the TextViews to the appropriate things
((TextView) listItem.findViewById(R.id.name)).setText(things[i].getName());
((TextView) listItem.findViewById(R.id.type)).setText(things[i].getType());
// Add the inflated view to the list
item.addView(child);
}

Setting background while using Custom ListView

I am working on a android application project in which I am using a customer simple adapter to repeat a layout called row.xml. Everything is going good except for the fact that I have a background color/image that I am using for my app and when I use this simple adapter method I get white space for the area that is not filled by the list.
Here is the code from my Java class
public class CustomerActivity extends ListActivity {
final static ArrayList<HashMap<String, ?>> arrayListData = new ArrayList<HashMap<String, ?>>();
ListView mCustomerList;
static {
HashMap<String, Object> CurrentRow = new HashMap<String, Object>();
CurrentRow.put("Icon", R.drawable.has_360_inspection_icon);
CurrentRow.put("Icon2", R.drawable.has_solid_fuel_inspection_icon);
CurrentRow.put("Name", "Xue Test");
arrayListData.add(CurrentRow);
CurrentRow = new HashMap<String, Object>();
CurrentRow.put("Icon", R.drawable.has_360_inspection_icon);
CurrentRow.put("Icon2", R.drawable.no_solid_fuel_inspection_icon);
CurrentRow.put("Name", "Ryan Test");
arrayListData.add(CurrentRow);
CurrentRow = new HashMap<String, Object>();
CurrentRow.put("Icon", R.drawable.no_360_inspection_icon);
CurrentRow.put("Icon2", R.drawable.has_solid_fuel_inspection_icon);
CurrentRow.put("Name", "Becca Test");
arrayListData.add(CurrentRow);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SimpleAdapter adapter = new SimpleAdapter(this, arrayListData, R.layout.row, new String[] { "Icon", "Icon2", "Name" },
new int[] { R.id.imageView1, R.id.imageView2, R.id.customer_name_textView });
setListAdapter(adapter);
}
and here is the code from row.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/lightBlue"
android:weightSum="6" >
<TextView
android:id="#+id/customer_name_textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="TextView" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/has_360_inspection_icon" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/has_solid_fuel_inspection_icon" />
<View
android:id="#+id/view1"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="2" />
</LinearLayout>
Thank you for the help and if you need any more information just let me know.
If you just want the entire list to have a light blue background, set it on the listview itself instead of on each row. That way if there's empty space in the list it will still be blue.
Have you use wrap_content or match_parent for Listview? If match_parent then replace to wrap_content

Using two listViews in one activity

I have made a list view containing two childs. Now i am trying to append a series of children to each of these two childs.
I am using following procedure.
super.onCreate(savedInstanceState);
setContentView(R.layout.report_main_layout);
CharSequence[] dummyChar = {"a","b"};
listView = (ListView) findViewById(R.id.UIMainAccountListView);
adapter = new CustomArrayAdapterOfMainList(this, dummyChar);
listView.setAdapter(adapter);
In the custom adapter i am using following code.
if (position == 0)
{
viewHolder.listView = (ListView) view.findViewById(R.id.UIReportTrialBalanceListView);
adapter = new CustomArrayAdapterForChildList(context, debitAccounts,sumDebit);
viewHolder.listView.setAdapter(adapter);
}
else
{
viewHolder.listView = (ListView) view.findViewById(R.id.UIReportTrialBalanceListView);
adapter = new CustomArrayAdapterForChildList(context, creditAccounts, sumCredit);
viewHolder.listView.setAdapter(adapter);
}
All i am getting is like this, I want to extend it so that all of the accounts show, List view should wrap content but it is not wrapping content
This will stack two listviews and force them to take up half of their parent each.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="2">
<ListView
android:id="#+id/list1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<ListView
android:id="#+id/list2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
</LinearLayout>
You can remove the cache color by setting android:cacheColorHint="#android:color/transparent" for each listview
<?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="fill_parent"
android:orientation="vertical"
android:paddingBottom="20sp" >
<ListView
android:id="#+id/child1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ListView
android:id="#+id/child2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/child1" />
</RelativeLayout>
This layout for first child1 listview above the second child2 listview
Thanks

Categories

Resources