如何使用ListView在布局中设置标题?

时间:2012-06-26 14:29:43

标签: android-layout android-listview

我正在尝试使用标题实现ListView。但是,使用下面的代码,会发生以下情况:

标题出现在第一个列表元素上。 有一个EditText出现在顶部的其他人面前。

我的Android活动

public class LoanRepaymentActivity extends ListActivity implements View.OnClickListener  {

private Location lastKnownLocation;
private ArrayList<CurrentRepaymentInstallment> loansForRepayment;
@Override
public void onCreate(Bundle savedInstanceState){
    lastKnownLocation = Utility.getLastKnownLocation(this);
    super.onCreate(savedInstanceState);

    Intent intent = getIntent();

    RepaymentInfo repaymentInfo = (RepaymentInfo)intent.getSerializableExtra(Constants.CURRENT_REPAYMENT_INSTALLMENT);
    loansForRepayment = repaymentInfo.getLoansForRepayment();
    boolean repaymentsNotPresent = loansForRepayment == null || loansForRepayment.isEmpty(); 
    if(repaymentsNotPresent){
        Dialog dialog = Utility.getDialogWithText(LoanRepaymentActivity.this, getText(R.string.noLoansForRepayment).toString());
        Utility.getDialogButton(dialog, LoanRepaymentActivity.this, PartnerGroupListActivity.class, intent.getStringExtra(Constants.ACECSS_TOKEN_PARAM));
        dialog.show();
    }

    setContentView(R.layout.loan_repayments);

    ArrayAdapter<CurrentRepaymentInstallment> adapter = new LoanRepaymentListAdapter(this, loansForRepayment);

    View footer = getLayoutInflater().inflate(R.layout.loan_disbursement_footer, null);
    getListView().addFooterView(footer);

    setListAdapter(adapter);
         if(!repaymentsNotPresent)  {
        TextView textView = (TextView)findViewById(R.id.screenTitle);
        String currentInstallmentLabel = getText(R.string.currentInstallmentLabel).toString() + repaymentInfo.getCurrentGroupInstallment();
        textView.setText(currentInstallmentLabel);
    }

    Button button = (Button)findViewById(R.id.disburse);
    button.setOnClickListener(this);
}

XML

<?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="wrap_content"
android:orientation="vertical"
>

<TextView android:id="@+id/screenTitle" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" >
</TextView>

 <ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@+layout/loan_disbursement_footer"
     />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
    android:id="@+id/borrowerName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@+id/label"
    android:textSize="14sp"
     >
</TextView>

<TextView
    android:id="@+id/loanAmount"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@+id/label"
    android:textSize="14sp"
     >
</TextView>

<TextView
    android:id="@+id/installmentNumber"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@+id/label"
    android:textSize="12sp"
    >
</TextView>

<TextView
    android:id="@+id/estimatedTotal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@+id/label"
    android:textSize="12sp"
    >
</TextView>

</LinearLayout>
<EditText
    android:id="@+id/repaymentAmount"
    android:layout_width="100sp"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:inputType="numberDecimal"
    android:textSize="12sp"
     >
</EditText>

自定义阵列适配器

public class LoanRepaymentListAdapter extends ArrayAdapter<CurrentRepaymentInstallment> {

private final List<CurrentRepaymentInstallment> loansForRepayment;
private final Activity context;

public LoanRepaymentListAdapter(Activity context, List<CurrentRepaymentInstallment> loansForRepayment) {
    super(context, R.layout.loan_repayments, loansForRepayment);
    this.context = context;
    this.loansForRepayment = loansForRepayment;
}

static class ViewHolder {
    protected TextView borrowerName;
    protected TextView loanAmount;
    protected TextView installmentNumber;
    protected TextView estimatedTotal;
    protected EditText repaymentAmount;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View view = null;
    if (convertView == null) {
        LayoutInflater inflator = context.getLayoutInflater();
        view = inflator.inflate(R.layout.loan_repayments, null);
        final ViewHolder viewHolder = new ViewHolder();
        viewHolder.borrowerName = (TextView) view.findViewById(R.id.borrowerName);
        viewHolder.loanAmount = (TextView) view.findViewById(R.id.loanAmount);
        viewHolder.installmentNumber = (TextView) view.findViewById(R.id.installmentNumber);
        viewHolder.estimatedTotal = (TextView) view.findViewById(R.id.estimatedTotal);
        viewHolder.repaymentAmount = (EditText) view.findViewById(R.id.repaymentAmount);
//          viewHolder.repaymentAmount.setEditableFactory(Editable.Factory.getInstance());
        viewHolder.repaymentAmount.addTextChangedListener(new TextWatcher() {

            @Override
            public void afterTextChanged(Editable arg0) {

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start,
                    int count, int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String repaymentAmount =  s.toString();
                try {
                    loansForRepayment.get(position).setRepaymentAmount(Float.parseFloat(repaymentAmount));
                } catch (Exception ex){
                    loansForRepayment.get(position).setRepaymentAmount(0.0F);
                }
            }


        });
        view.setTag(viewHolder);
        viewHolder.repaymentAmount.setTag(loansForRepayment.get(position));
    } else {
        view = convertView;
        ((ViewHolder) view.getTag()).repaymentAmount.setTag(loansForRepayment.get(position));
    }
    ViewHolder holder = (ViewHolder) view.getTag();
    holder.borrowerName.setText(loansForRepayment.get(position).getLoanProfileBasicInfo().getBorrowerBasicInfo().getFirstName());
    holder.loanAmount.setText("Rs. " + Float.toString(loansForRepayment.get(position).getLoanProfileBasicInfo().getLoanAmountInPaisa()/100));
    holder.estimatedTotal.setText("Rs. " + Float.toString(loansForRepayment.get(position).getEstimatedTotalAmount()/100));
    holder.installmentNumber.setText("Inst no : " + Integer.toString(loansForRepayment.get(position).getInstallmentNumber()));
    float repaymentAmt = loansForRepayment.get(position).getRepaymentAmount();
    if(repaymentAmt != 0.0) holder.repaymentAmount.setText(Float.toString(repaymentAmt));
    return view;
       }
   }

如果有人能告诉我如何做到这一点会很棒。

1 个答案:

答案 0 :(得分:0)

为标题设置新布局,并将其作为标题添加到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="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentBottom="true"
    >

<TextView android:id="@+id/repaymentScreenTitle" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:text="@string/repaymentsTitle" >
</TextView>
</LinearLayout>

在活动

中将此块替换为它下面的块
     if(!repaymentsNotPresent)  {
    TextView textView = (TextView)findViewById(R.id.screenTitle);
    String currentInstallmentLabel = getText(R.string.currentInstallmentLabel).toString() + repaymentInfo.getCurrentGroupInstallment();
    textView.setText(currentInstallmentLabel);
}

    View headerView = getLayoutInflater().inflate(R.layout.loan_repayment_title, null);
    TextView titleView = (TextView) headerView.findViewById(R.id.repaymentScreenTitle);
    String titlePrefix = (String) getText(R.string.groupInstallmentLabel);
    titleView.setText(titlePrefix + " " + repaymentInfo.getCurrentGroupInstallment());
    getListView().addHeaderView(headerView);