to add a last row in listview
i have a custom listview i want to add one lat row to it which will calculate total of all the list when i scroll the last row gets added again and again
if(position==searchResult.size() && searchResult.size()!=1)
{
holder.checkImg.setVisibility(ImageView.GONE);
holder.fvtImg.setVisibility(ImageView.GONE);
holder.type.setVisibility(TextView.GONE);
holder.name.setVisibility(TextView.GONE);
holder.offer_price.setVisibility(TextView.GONE);
holder.real_price.setVisibility(TextView.GONE);
holder.total.setVisibility(TextView.VISIBLE);
holder.total_price.setVisibility(TextView.VISIBLE);
//DecimalFormat df = new DecimalFormat("0.00");
//holder.txt_distance.setText(df.format(mData.get(position).get("distance")).toString()+"...");
DecimalFormat twoDForm = new DecimalFormat("0.00");
String totalPrice = (twoDForm.format(mTotalPrice)+"").replace('.',',');
holder.total.setText("Total");
holder.total_price.setText("Kr. "+totalPrice);
}
else
{
holder.total.setVisibility(TextView.GONE);
holder.total_price.setVisibility(TextView.GONE);
if(isEdit){
/*LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.leftMargin=30;
holder.fvtImg.setLayoutParams(lp);*/
/* TranslateAnimation anim = new TranslateAnimation(0,35,0,0);
holder.fvtImg.setAnimation(anim);
anim.setFillAfter(true);*/
holder.fvtImg.setVisibility(ImageView.GONE);
holder.name.setPadding(10,0, 0, 0);
holder.type.setPadding(10,0, 0, 0);
holder.checkImg.setVisibility(ImageView.VISIBLE);
Layout file :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageView
android:id="@+id/list_background"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/price_list_background"
/>
<CheckBox android:id="@+id/checkBox"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:visibility="gone">
</CheckBox>
<ImageView
android:id="@+id/img_pol"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/pol_icon_tag"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
/>
<TextView
android:id="@+id/txt_name"
android:layout_width="wrap_content"
android:layout_height="15dp"
android:textSize="16dp"
android:textColor="#000000"
android:layout_marginTop="5dp"
android:layout_marginLeft="35dp"
/>
<TextView
android:id="@+id/txt_type"
android:layout_width="200dp"
android:layout_height="13dp"
android:textSize="12dp"
android:textColor="#464647"
android:layout_marginTop="35dp"
a开发者_如何学JAVAndroid:layout_marginLeft="35dp"
/>
<TextView
android:id="@+id/txt_real_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12dp"
android:textColor="#464647"
android:layout_marginTop="35dp"
android:layout_marginLeft="90dp"
/>
<TextView
android:id="@+id/txt_offer_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16dp"
android:textColor="#464647"
android:layout_marginTop="31dp"
android:layout_marginLeft="190dp"
/>
<TextView
android:id="@+id/txt_total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textColor="#464647"
android:layout_centerVertical="true"
android:layout_marginLeft="100dp"
android:text="Total"
android:visibility="gone"
/>
<TextView
android:id="@+id/txt_total_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16dp"
android:textColor="#464647"
android:layout_centerVertical="true"
android:layout_marginLeft="230dp"
android:text="kr. 222.22"
android:visibility="gone"
/>
</RelativeLayout>
Have you considered using a footer instead of placing the last item inside the ListView?
What you do is, before you set the adapter of the ListView, you inflate a footer layout (or perhaps just a TextView, if that's all you need). Store a reference to it, and then add it to the ListView:
TextView myFooter = new TextView( context );
myListView.addFooterView( myFooter );
Then when you add items to the list or when the calculation needs to occur, you just calculate and set the text of myFooter
.
myFooter.setText( myCalculatedValue );
Setting the text of the footer must ofcourse happen on the UI-thread so if you use a background thread or a AsyncTask to add to your listview, be sure you update stuff the right place.
精彩评论