how to display data in listactivity??

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

how to display data in listactivity??

Postby mr_muskuraha » Tue Jan 19, 2010 1:24 pm

dear friends,

i have a scenario,

----------------------------------------
Main Heading and right aligned button
Short Text below both
----------------------------------------
Main Heading and right aligned button
Short Text below both
----------------------------------------
Main Heading and right aligned button
Short Text below both
--------------------------------------

how can i display data like above example in listview activity can any one guide me???

any help would be appriciated..
mr_muskuraha
Senior Developer
Senior Developer
 
Posts: 142
Joined: Wed Jan 13, 2010 8:49 am

Top

Postby serkan.dogantekin » Tue Jan 19, 2010 5:21 pm

ListView's are managed by ListAdapters and its possible to provide for a different layout for items in a ListView by using Adapters. So what you need to do is:

- define a layout for your design for list items
- define an adaptor class that extends BaseAdapter or a subclass of it (I prefer ArrayAdapter)
- the method important for your case is "getView(int position, View convertView, ViewGroup parent)", it returns a view to represent item (specified with input variable position) in the ListView
inside this method:
public View getView(int position, View convertView, ViewGroup parent) {
View tempView;

if(convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
tempView= layoutInflater.inflate(R.layout.<the layout you defined>, null);
} else {
tempView= convertView;
}
// use "findViewById" method to get views in your layout, and populate them
return tempView;
}


- dont forget to bind this adapter to the ListView by using ListView's "setAdapter" method
serkan.dogantekin
Developer
Developer
 
Posts: 27
Joined: Thu Jun 04, 2009 6:12 pm
Location: Istanbul/Turkey

thanks for reply

Postby mr_muskuraha » Wed Jan 20, 2010 7:37 am

could please elebrate 2nd and third step in detail i am actually new to android development and java need some sample code to make changes :(

any help would be appriciated..
mr_muskuraha
Senior Developer
Senior Developer
 
Posts: 142
Joined: Wed Jan 13, 2010 8:49 am

Postby serkan.dogantekin » Wed Jan 20, 2010 7:55 am

- assume I have a layout that has three textviews (t1, t2, t3) for displaying listview items

- design a adapter to manage list view (HelperClass is a dummy class to hold values of text values for each item):

public class XAdapter extends ArrayAdapter<HelperClass> {
private Context context;

public XAdapter (Context context, int textViewResourceId, HelperClass[] objects) {
super(context, textViewResourceId, objects);
this.context = context;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
HelperClass listItem= getItem(position);
View itemView;

if(convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
itemView = layoutInflater.inflate(R.layout.<your_layout>, null);
} else {
itemView = convertView;
}

TextView t1 = (TextView) itemView.findViewById(R.id.t1);
TextView t2 = (TextView) itemView.findViewById(R.id.t2);
TextView t3 = (TextView) itemView.findViewById(R.id.t3);



t1.setText(listItem.t1);
t2.setText(listItem.t2);
t3.setText(listItem.t3);

return itemView;
}
}

- bind adaptor with the list view:

HelperClass [] content = ....; // prepare data to publish in the list view
ListView targetList = ...; // get your list view
targetList.setAdapter(new XAdapter (this,R.layout.<your_layout>,content));

I hope this helps
serkan.dogantekin
Developer
Developer
 
Posts: 27
Joined: Thu Jun 04, 2009 6:12 pm
Location: Istanbul/Turkey

Postby nbagade » Wed Jan 20, 2010 4:40 pm

Hi,

i want to show the listview like,

------------------------------------
image text
------------------------------------
text image
------------------------------------
image text
------------------------------------

how to achieve this.?

thanks
nbagade
Experienced Developer
Experienced Developer
 
Posts: 58
Joined: Fri Nov 20, 2009 7:49 am
Location: India

Postby serkan.dogantekin » Thu Jan 21, 2010 1:40 pm

a sample layout will be:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
<ImageView
android:id="@+id/listImageTop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
/>
<TextView
android:id="@+id/listText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/listImageTop"
/>
<ImageView
android:id="@+id/listImageDown"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/listText"
/>
</RelativeLayout>
serkan.dogantekin
Developer
Developer
 
Posts: 27
Joined: Thu Jun 04, 2009 6:12 pm
Location: Istanbul/Turkey

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 5 guests