2011-07-06 82 views
1

其實我已經創建了一個customListview,在這裏我想動態地添加項目..但在我的情況下,第一個項目插入任何問題,但當我嘗試插入第二個項目..我給一個錯誤,我會發送在code..if有誰知道請建議吧..Listview問題

代碼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); 
    } 
} 

代碼定製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; 
} 
} 
+0

什麼錯誤....是否強制關閉? –

+0

什麼是錯誤? – Sarmad

+0

葉它強制關閉錯誤 – AndroidDev

回答

0

嗨,我覺得可以在下面的代碼中使用..

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); 
} 
+0

沒有它不工作相同的錯誤..「停止的應用程序」 – AndroidDev

+0

你可以發佈logcat的錯誤? – Nikhil

+0

實際上運行在設備中時發生錯誤..當我添加第一個日期它完美的工作,但是當我添加第二個日期它終止..錯誤是..「應用程序已經停止unexpextedly」 – AndroidDev

0

會有你的字符串數組strDisplay [iHistCount中只有一個元素]。因爲你每次都把價值放在最後的位置。用i替換iHistCount。

+0

你是我的唯一..它在代碼中的錯誤 – AndroidDev