Archives for category: Uncategorized

Finally I have had some success with TwoLineListItem’s. Here is how I implemented my solution.

Firstly, I created a simple class with two Strings and the associated getters and setters. This class represents each item in the list I will be creating later.

Next, I created a customised ArrayAdapter where I override the getView method. Inside the getView method you need to use the LayoutInflater to get the view from our main.xml file. Then we set the two lines of text.

Lastly, we extend the Activity class to extend from ListActivity. In the onCreate method (which is overridden) we call setListAdapter with our new custom ArrayAdapter as created above. From here we generate some data in our data class, add to a list and pass this list as a parameter to setListAdapter.

Code is below: apologies for the readability of the code. Hopefully I will get the hand of this blogging and fix it up to look good.

item.java <My Data Type>

package com.itemman.main;

public class item {

private String itemName;

private String itemDesc;

public String getitemName() {

returnitemName;

}

publicvoid setitemName(String itemName) {

this.itemName = itemName;

}

public String getitemDesc() {

returnitemDesc;

}

publicvoid setitemDesc(String itemDesc) {

this.itemDesc = itemDesc;

}

}

/res/layout/main.xml <The main layout file>

<?xmlversion=“1.0”encoding=“utf-8”?>

<TwoLineListItemxmlns:android=http://schemas.android.com/apk/res/android&#8221;

  android:layout_width=“fill_parent”

  android:layout_height=“wrap_content”>

<TextViewandroid:id=“@android:id/text1”

android:layout_marginTop=“5dp”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:textSize=“15dp”

android:textStyle=“italic”/>

<TextViewandroid:id=“@android:id/text2”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_below=“@android:id/text1”

android:layout_alignLeft=“@android:id/text1”

android:paddingBottom=“10dp”

android:includeFontPadding=“false”

android:textSize=“10sp”

android:textStyle=“normal”/>

</TwoLineListItem>

itemArrayAdapter.java <My custom ArrayAdapter class>

package com.itemman.main;

import java.util.List;

import android.content.Context;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.ArrayAdapter;

import android.widget.TwoLineListItem;

public class itemArrayAdapter extends ArrayAdapter {

private final int resourceId;

public itemArrayAdapter(Context context, int textViewResourceId, List objects) {

super(context, textViewResourceId, objects);

resourceId = textViewResourceId;

}

@Override

public View getView(int position, View convertView, ViewGroup parent) {

item t = (item) getItem(position);

if (t == null) {

return null;

}

LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

TwoLineListItem view;

if (convertView == null) {

view = (TwoLineListItem) inflater.inflate(resourceId, parent, false);

} else {

view = (TwoLineListItem) convertView;

}

if (view.getText1() != null) {

view.getText1().setText(t.getitemName());

}

if (view.getText2() != null) {

view.getText2().setText(t.getitemDesc());

}

return view;

}

}

Advertisements

So, setting up a list is no big deal. But the complexity increased by a multitude when I tried my hand at a twolinelistitem. So, I ask of anyone who reads this, what is the most efficient way of setting up a list with twolinelistitems? I’m going to go away again and re-try my hand at this but if you think you’ve got this down in an elegant / efficient manner than please let me know.

For anyone who hasn’t used what i’m talking about, here is some documentation I’ve found so far:

http://developer.android.com/reference/android/widget/TwoLineListItem.html

I’ll be back soon (hopefully) with my solution of which I want to be critiqued heavily on. I really want to make sure that as I learn Android development I am implementing best practices.

Good day.

Seriously, how are you?

My name is Bernie and I am about to head down the rabbit hole that is Android development. I am an experienced programmer in languages such as C, Java, VB .Net, Perl, PHP and a few others but I’ve been itching to get into some Android development. So I am now going to learn, with the help of others reading this blog, how to build Android applications. I will post my solutions and my issues and hope that others can help me out and that I can help out other beginners. This page will hopefully serve as my reference as I get further and further ahead in my development progress. I’m going to start by setting up my development environment which will involve downloading and installing Eclipse 3.7.2(http://www.eclipse.org/downloads/)

Today is day one, where will it take me?