Modify code to use ArrayList instead of SimpleCursor - android

Hi this is the code I'm looking at:
https://github.com/findup/Android_Sample_TodoApp/blob/master/src/jp/co/example/testapp/MainActivity.java
Around line 127, it chooses to use a database connection to fetch content from the database.
Instead of fetching data from the database, I'd like to use an ArrayList to hold the data. Could anyone help me figure out what I need to do? Thanks!

Not a one-for-one example based on your code, but this is will populate a list from a database table without a simple cursor:
public class MyListAdapter extends ListActivity {
List<ContactGroup> groups = new ArrayList<ContactGroup>();
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.groups = getGrpList();
ContactGroupAdapter cAdapter = new ContactGroupAdapter(this);
setListAdapter(cAdapter);
}
private List<ContactGroup> getGrpList(){
List<ContactGroup> grps = new ArrayList<ContactGroup>();
ContentResolver cr = getContentResolver();
Cursor groupCur = cr.query(Groups.CONTENT_URI, new String [] {Groups._ID, Groups.NAME}, null, null, Groups.NAME + " ASC");
if (groupCur.getCount() > 0) {
while (groupCur.moveToNext()) {
ContactGroup newGroup = new ContactGroup();
newGroup.Name = groupCur.getString(groupCur.getColumnIndex(Groups.NAME));
newGroup.Id = groupCur.getString(groupCur.getColumnIndex(Groups._ID));
grps.add(newGroup);
}
}
return grps;
}
public class ContactGroupAdapter extends BaseAdapter{
public ContactGroupAdapter(Context c) {
mContext = c;
}
public int getCount() {
return groups.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
LayoutInflater vi = LayoutInflater.from(this.mContext);
convertView = vi.inflate(R.layout.two_line_list_item, null);
holder = new ViewHolder();
convertView.setTag(holder);
}
else {
//Get view holder back
holder = (ViewHolder) convertView.getTag();
}
ContactGroup cg = groups.get(position);
if (cg != null) {
//Name
holder.toptext = (TextView) convertView.findViewById(R.id.text1);
holder.toptext.setText(cg.Name);
//ID
holder.bottomtext = (TextView) convertView.findViewById(R.id.text2);
holder.bottomtext.setText(cg.Id);
}
return convertView;
}
private Context mContext;
}
public static class ViewHolder {
TextView toptext;
TextView bottomtext;
}
public class ContactGroup{
public String Id;
public String Name;
}
}
Then there are XML files ...
two_line_list_item.xml:
<?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">
<TextView android:id="#+id/text1" android:textStyle="bold"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
<TextView android:id="#+id/text2" android:textStyle="bold"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
</LinearLayout>
and main.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:paddingLeft="8dp"
android:paddingRight="8dp">
<ListView android:id="#+id/android:list" android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TextView android:id="#id/android:empty" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:text="No Groups" />
</LinearLayout>

Related

Cannot resolve layout in adapter for custom list view

I am trying to make a custom ListView and a adapter for that list, I am inflating the list view layout but it is giving an error saying
Cannot find symbol variable lv_grpMsg
Can someone tell me how to correct this error?
Here is the adapter code:
public class GrpMsgAdapter extends BaseAdapter{
private Fragment fragment;
private ArrayList data;
private static LayoutInflater layoutInflater = null;
public Resources resources;
GrpMsgModel grpMsgModel = null;
int i = 0;
public GrpMsgAdapter(Fragment fragment, ArrayList data, Resources resources){
this.fragment = fragment;
this.data = data;
this.resources = resources;
layoutInflater = (LayoutInflater)this.fragment.getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
if(data.size() <= 0)
return 1;
return data.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
public static class ViewHolder{
public ImageView iv_grpMsgDp;
public TextView tv_grpMsgName, tv_grpMsgNoMem;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder viewHolder;
if(convertView == null) {
view = layoutInflater.inflate(android.R.layout.lv_grpmsg, null);
viewHolder = new ViewHolder();
viewHolder.iv_grpMsgDp = (ImageView) view.findViewById(R.id.iv_grpMsgDp);
viewHolder.tv_grpMsgName = (TextView) view.findViewById(R.id.tv_grpMsgName);
viewHolder.tv_grpMsgNoMem = (TextView) view.findViewById(R.id.tv_grpMsgNoMem);
view.setTag("holder");
}
else{
viewHolder = (ViewHolder)view.getTag();
}
if(data.size() <= 0){
viewHolder.tv_grpMsgName.setText("No data");
}
else{
grpMsgModel = null;
grpMsgModel = (GrpMsgModel)data.get(position);
viewHolder.tv_grpMsgName.setText(grpMsgModel.getGrpName());
viewHolder.tv_grpMsgNoMem.setText(grpMsgModel.getNoMem());
viewHolder.iv_grpMsgDp.setImageResource(resources.getIdentifier("com.example.nmss.coach" + grpMsgModel.getImageURL(), null, null));
}
return view;
}
}
My ListView layout code is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/relGrpMsg">
<ImageView
android:id="#+id/iv_grpMsgDp"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginStart="18dp"
app:srcCompat="#mipmap/ic_launcher_round"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginTop="12dp" />
<TextView
android:id="#+id/tv_grpMsgName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Group name"
android:layout_alignTop="#+id/iv_grpMsgDp"
android:layout_toEndOf="#+id/iv_grpMsgDp"
android:layout_marginStart="80dp" />
<TextView
android:id="#+id/tv_grpMsgNoMem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/iv_grpMsgDp"
android:layout_alignStart="#+id/tv_grpMsgName"
android:layout_marginBottom="15dp"
android:text="No. of members" />
<CheckBox
android:id="#+id/cb_grpMsgSel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/tv_grpMsgNoMem"
android:layout_alignParentEnd="true"
android:layout_marginEnd="24dp" />
</RelativeLayout>
It should be R.layout.lv_grpmsg not android.R.layout.lv_grpmsg
use R.layout.lv_grpmsg instead ofandroid.R.layout.lv_grpmsg
and other mistake is view.setTag("holder");
correct one is view.setTag(viewHolder);

ListView shows only One Row

I have simple list which suppose to display all the rows from DB table. However, it shows only one row in the list view.
Though, it receives all the rows from the DB (verified the size of configArrayList), but displays only the first row and discards others.
Here is the Activity and Custom Adapter.
Main Activity :
ArrayList<HashMap<String, String>> configArrayList = dbTools.getAllConfig(); // Returns 4 rows
ArrayList<ConfigEntity> configMenu = new ArrayList<>();
if (!(configArrayList.isEmpty()))
{
Iterator<HashMap<String, String>> iterator = configArrayList.iterator();
while (iterator.hasNext())
{
HashMap<String, String> configMap = iterator.next();
// ConfigEntity contains two members Name, ID and their setter, getter
ConfigEntity entity = new ConfigEntity();
entity.setID(configMap.get("ID"));
entity.setName(String.valueOf(configArrayList.size()));
// Set Name as Size to verify the list size is grater than 1. (Size is 4)
configMenu.add(entity);
}
}
if (!(configMenu.isEmpty()))
{
ListAdapter adapter = new ConfigListAdapter(this, configMenu);
ListView theListView = (ListView) findViewById(R.id.config_list_listView);
theListView.setAdapter(adapter);
}
Here is the Custom Adapter :
public class ConfigListAdapter extends BaseAdapter {
private ArrayList<ConfigEntity> entityList;
private LayoutInflater layoutInflater;
public ConfigListAdapter(Context context, ArrayList<ConfigEntity> entityList) {
this.entityList = entityList;
layoutInflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return entityList.size();
}
#Override
public Object getItem(int position) {
return entityList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.config_list_row_layout, null);
holder = new ViewHolder();
holder.nameTextView = (TextView) convertView.findViewById(R.id.config_list_row_textView);
holder.idTextView = (TextView) convertView.findViewById(R.id.config_list_row_id);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
ConfigEntity entity = entityList.get(position);
holder.nameTextView.setText(entity.getName());
holder.idTextView.setText(entity.getID());
return convertView;
}
static class ViewHolder {
TextView nameTextView;
TextView idTextView;
}
}
main_activity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add New"
android:id="#+id/config_list_addConfig_button"
android:layout_gravity="right"
android:onClick="addNewConfig" />
<ListView
android:id="#+id/config_list_listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:dividerHeight="5dp">
</ListView>
</LinearLayout>
</ScrollView>
config_list_listView.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="#dimen/margin_20dp">
android:layout_marginRight="#dimen/margin_20dp">
<TextView
android:id="#+id/config_list_row_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:layout_marginTop="#dimen/margin_10dp"
android:layout_marginLeft="#dimen/margin_20dp"
android:longClickable="true" />
<TextView
android:id="#+id/config_list_row_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
</LinearLayout>
Please try this.
Change getView Method as follows
#Override
public View getView(int position, View convertView, ViewGroup parent) {
view=convertView;
ViewHolder holder;
if (convertView == null) {
view = layoutInflater.inflate(R.layout.config_list_row_layout, null);
holder = new ViewHolder();
holder.nameTextView = (TextView) view.findViewById(R.id.config_list_row_textView);
holder.idTextView = (TextView) view.findViewById(R.id.config_list_row_id);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
ConfigEntity entity = entityList.get(position);
holder.nameTextView.setText(entity.getName());
holder.idTextView.setText(entity.getID());
return view;
}
and Declare View view; as class level variable;
Hope it helps Thanks. Refer here :-https://androidruler.wordpress.com/2016/02/21/android-custom-listview-example/

Android parse two array into ListView

my ListView extends from ListFragment and after define subClass for customAdapter extends from BaseAdapter could not parse two array to layout elements.
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/txt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"/>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/txt2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"/>
</LinearLayout>
setListAdapter function:
public class ResivedSMS extends ListFragment {
.
.
.
public ResivedSMS() {
testArray1 = new String[] {
"1111111111",
"2222222222",
"3333333333",
"4444444444",
"5555555555",
"6666666666",
};
testArray2 = new String[] {
"AAAAA",
"BBBBB",
"CCCCC",
"DDDDD",
"FFFFF",
"GGGGG",
};
}
.
.
.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ViewResivedSMSDetailes customListView = new ViewResivedSMSDetailes(getActivity(),testArray1,testArray2);
setListAdapter( customListView ); //call the method if listFragment
}
.
.
.
class ViewResivedSMSDetailes extends BaseAdapter
{
private LayoutInflater inflater;
private String[] values1;
private String[] values2;
private class ViewHolder {
TextView txt1;
TextView txt2;
}
public ViewResivedSMSDetailes(Context context,String[] values1,String[] values2)
{
this.values1=values1;
this.values2=values2;
inflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return values1.length;
}
#Override
public Object getItem(int index) {
return values1[index];
}
#Override
public long getItemId(int arg0) {
return arg0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder = null;
if(convertView ==null){
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.list_fragment, null);
holder.txt1 = (TextView)convertView.findViewById(R.id.txt1);
holder.txt2 = (TextView)convertView.findViewById(R.id.txt2);
convertView.setTag(holder);
}
holder = (ViewHolder) convertView.getTag();
holder.txt1.setText(values1[position]);
holder.txt2.setText(values2[position]);
return convertView;
}
}
UPDATED POST:
list_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/txt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"/>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/txt2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"/>
</LinearLayout>
logCat Result:
08-24 14:48:03.245 851-851/ir.tsms E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at ir.tsms.ResivedSMS$ViewResivedSMSDetailes.getView(ResivedSMS.java:105)
ResivedSMS.java:105 is:
holder.txt2.setText(values2[position]);
From what it looks when you are inflating the view in the getView() callback method you are not setting the ViewGroup parent.
Try changing the following line:
convertView = inflater.inflate(R.layout.list_fragment, null);
To:
convertView = inflater.inflate(R.layout.list_fragment, parent,false);
And I would suggest creating a shared class of both of the views, and have a single data set and not two data sets to avoid index out of bounds exceptions.

Can't find TextView in recursive custom Listview adapter

I created this class called GeoArea, which is suppose to store "Geographical Area" that have children Geographical Areas, this is fairly strait foward:
public class GeoArea {
public String id;
public String name;
public List<GeoArea> subGeoAreas;
public GeoArea parentGeoArea;
public GeoArea(String id) {
this.id = id;
name = id;
subGeoAreas = new LinkedList<GeoArea>();
}
#Override
public String toString() {
return name;
}
}
I have created the following Layout to render it on Android, the idea here is to for each GeoArea to recursively render it self and then it's children GeoArea in a listView:
//layout_geo_area.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/txtGeoAreaName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="left"
android:text="Geo Area Name"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ListView
android:id="#+id/listViewChildGeoAreas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/txtGeoAreaName"
android:gravity="left" >
</ListView>
</RelativeLayout>
This is my adapter I created for GeoArea to be displayed in a listView:
public class AdapterGeoArea extends ArrayAdapter<GeoArea>{
private ArrayList<GeoArea> _myGeoArea;
private Context _myContext;
LayoutInflater _inflater;
public AdapterGeoArea(Context context, ArrayList<GeoArea> myGeoArea) {
super(context, 0, myGeoArea);
_myGeoArea = myGeoArea;
_inflater = LayoutInflater.from(context);
_myContext = context;
}
public int getCount() {
return _myGeoArea.size();
}
public View getView(int position, View convertView, ViewGroup parent) {
GeoAreaLayoutHolder holder;
if (convertView == null) {
convertView = _inflater.inflate(R.layout.layout_geo_area,parent,false);
holder = new GeoAreaLayoutHolder();
holder.txtGeoAreaName = (TextView)convertView.findViewById(R.id.txtGeoAreaName);
holder.txtGeoAreaName.setTag(convertView);
holder.listViewChildGeoAreas = (ListView)convertView.findViewById(R.id.listViewChildGeoAreas);
holder.listViewChildGeoAreas.setTag(convertView);
} else {
holder = (GeoAreaLayoutHolder) convertView.getTag();
}
GeoArea curGeoArea = _myGeoArea.get(position);
holder.txtGeoAreaName.setText(curGeoArea.name);
if(curGeoArea.subGeoAreas.size()>0){
ArrayList<GeoArea> testList = new ArrayList<GeoArea>();
AdapterGeoArea adapter = new AdapterGeoArea(_myContext, testList);
for(GeoArea childGeoArea:curGeoArea.subGeoAreas){
testList.add(childGeoArea);
}
holder.listViewChildGeoAreas.setAdapter(adapter);
}
return convertView;
}
static class GeoAreaLayoutHolder {
public TextView txtGeoAreaName;
public ListView listViewChildGeoAreas;
}
}
And here is my Activity that I am using to set it all up:
public class ActivityGeoAreas extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_geo_area);
GeoArea.searchTerm = "Bar & Grill";
GeoArea torontoArea = new GeoArea("cityOfToronto");
ArrayList<GeoArea> testList = new ArrayList<GeoArea>();
testList.add(torontoArea);
AdapterGeoArea adapter = new AdapterGeoArea(this, testList);
ListView lv = (ListView) findViewById(R.id.listViewChildGeoAreas);
lv.setAdapter(adapter);
}
}
When I try to run it, I get the error nullPointerException on the line:
holder.txtGeoAreaName.setText(curGeoArea.name);
What am I doing wrong?
You may want to check ExpandableListView may suit your needs better
http://developer.android.com/reference/android/widget/ExpandableListView.html
An example # http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/
Continuing from my previous answer to your question ( i though that solved your problem)
To display just name in your listview
list_row.xml // this is the layout with textview to be inflated in getView.
Each row will have textview
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/textGeoArea"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:text="Choose Area"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
layout_geo_area.xml // with only listview no textview
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="#+id/listViewChildGeoAreas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:gravity="left" >
</ListView>
</RelativeLayout>
Now your adapter class
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_row,parent,false);
// inflate list_row.xml with textview
holder = new ViewHolder();
holder.tv = (TextView)convertView.findViewById(R.id.textGeoArea);
holder.setTag(convertView);
}
else {
holder = (ViewHolder) convertView.getTag();
}
GeoArea curGeoArea = _myGeoArea.get(position);
holder.tv.setText(curGeoArea.name);
return convertView;
}
static class ViewHolder // use a view holder for smooth scrolling an performance
{
TextView tv;
}
You have a custom adapter.
public class AdapterGeoArea extends ArrayAdapter<GeoArea>{
Now you set the adapter to listview like below
AdapterGeoArea adapter = new AdapterGeoArea(this, testList);
ListView lv = (ListView) findViewById(R.id.listViewChildGeoAreas);
lv.setAdapter(adapter);
So why do you need the below. remove these
if(curGeoArea.subGeoAreas.size()>0){
ArrayList<GeoArea> testList = new ArrayList<GeoArea>();
AdapterGeoArea adapter = new AdapterGeoArea(_myContext, testList);
for(GeoArea childGeoArea:curGeoArea.subGeoAreas){
testList.add(childGeoArea);
}
holder.listViewChildGeoAreas.setAdapter(adapter);

Custom ListView error when inserting many Views

Actually I have created a custom ListView. I want to add items to this custom ListView dynamically. But in my case, the first item is inserted without any problem but when I try to insert the second item it gives an error. If anyone knows how to solve this, please suggest it to me.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ListView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="#+id/historyFrmLstView2"
android:layout_alignParentLeft="true"
android:layout_below="#+id/historyFrmLstView1"
android:layout_marginTop="20dip"
android:layout_marginBottom="10dip">
</ListView>
</RelativeLayout>
ListView.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="5dip"
android:background="#00000000">
<TextView
android:textSize="18dp"
android:layout_height="wrap_content"
android:textColor="#000000"
android:background="#drawable/history_scroll_title_label_bg"
android:layout_width="wrap_content"
android:id="#+id/txtHistoryDisplay"
android:layout_below="#+id/txtViewHeading"
android:layout_marginTop="0dip"
android:layout_marginLeft="2dip">
</TextView>
</RelativeLayout>
main.java
public void displayHistory()
{
String strDisplay[] = {" "};
historyFrmLstView2 = (ListView) findViewById(R.id.historyFrmLstView2);
int iHistCount = CycleManager.getSingletonObject().getHistoryCount();
int i;
SimpleDateFormat sdFormatter = new SimpleDateFormat("dd-MMMM-yyyy");
for (i=0; i<iHistCount; i++)
{
strDisplay[i] = "";
Date dtHist = CycleManager.getSingletonObject().getHistoryDate(iHistCount);
strDisplay[i]=sdFormatter.format(dtHist.getTime());
aHistFrmAdpter2 = new HistoryFrmCustomAdapter2(this,strDisplay);
historyFrmLstView2.setAdapter(aHistFrmAdpter2);
}
}
custom Adapter.java
public class HistoryFrmCustomAdapter2 extends BaseAdapter
{
public String heading1[];
public Activity context;
public LayoutInflater inflater;
public HistoryFrmCustomAdapter2(Activity context,String[] heading1)
{
super();
this.context = context;
this.heading1=heading1;
this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount()
{
return heading1.length;
}
public Object getItem(int position)
{
return null;
}
public long getItemId(int position)
{
return 0;
}
public static class ViewHolder
{
TextView txtHistoryDisplay;
}
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
if(convertView==null)
{
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.histryfrm_listview2, null);
holder.txtHistoryDisplay =(TextView) convertView.findViewById(R.id.txtHistoryDisplay);
convertView.setTag(holder);
}
else
holder=(ViewHolder)convertView.getTag();
holder.txtHistoryDisplay.setText(heading1[position]);
return convertView;
}
}
Hi i think that may be used below code..
public void displayHistory()
{
String strDisplay[] = {" "};
historyFrmLstView2 = (ListView) findViewById(R.id.historyFrmLstView2);
int iHistCount = CycleManager.getSingletonObject().getHistoryCount();
int i;
SimpleDateFormat sdFormatter = new SimpleDateFormat("dd-MMMM-yyyy");
for (i=0; i<iHistCount; i++)
{
strDisplay[i] = "";
Date dtHist = CycleManager.getSingletonObject().getHistoryDate(iHistCount);
strDisplay[i]=sdFormatter.format(dtHist.getTime());
}
aHistFrmAdpter2 = new HistoryFrmCustomAdapter2(this,strDisplay);
historyFrmLstView2.setAdapter(aHistFrmAdpter2);
}
there will be only one element in your string array strDisplay[iHistCount]. since you r putting value at the last position each time. replace iHistCount with i.

Categories

Resources