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.
精彩评论