与充气机布局的android相对布局问题

时间:2012-12-01 19:10:59

标签: android android-layout

我在android中有代码,它在列表中包含联系人,其中包含每个列表项的复选框。现在我想在屏幕底部添加一个按钮来执行某些操作。但是当我添加按钮时,它会显示每个列表项。我只希望它在底部一次。 这是我的活动。

package com.exercise.me;

import java.util.ArrayList;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;


public class ContactList extends ListActivity {
    /** Called when the activity is first created. */

    private ArrayList<contact> contact_list = null;
    private ProgressDialog mProgressDialog = null;
    private contactAdapter mContactAdapter = null;
    private Runnable mViewcontacts = null;
     //final Button btnPost = (Button)this.findViewById(R.id.btnPost);

    private ArrayList<contact> items;
    boolean[] isChecked;
    Cursor mCursor;
    ListView lv;
    CheckBox cb;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview);

        contact_list = new ArrayList<contact>();
        lv = getListView();

        mViewcontacts = new Runnable() {
            public void run() {
                getContacts();
            }
        };

        Thread thread = new Thread(null, mViewcontacts, "ContactReadBackground");
        thread.start();
        mProgressDialog = ProgressDialog.show(ContactList.this, "Please Wait...", "Retriving Contacts...", true);


    }

    @SuppressWarnings("unused")
    private void getContacts() {

        try {

            String[] projection = new String[] {
                    ContactsContract.Contacts.DISPLAY_NAME,
                    ContactsContract.Contacts.HAS_PHONE_NUMBER,
                    ContactsContract.Contacts._ID };

            mCursor = managedQuery(ContactsContract.Contacts.CONTENT_URI,
                    projection, ContactsContract.Contacts.HAS_PHONE_NUMBER + "=?", new String[] { "1" },
                    ContactsContract.Contacts.DISPLAY_NAME);

            while (mCursor.moveToNext()) {
                contact contact = new contact();

                String contactId = mCursor.getString(mCursor.getColumnIndex(ContactsContract.Contacts._ID));
                contact.setContactName(mCursor.getString(mCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
                contact_list.add(contact);
            }
            isChecked = new boolean[mCursor.getCount()];

            for (int i = 0; i < isChecked.length; i++) {
                isChecked[i] = false;
            }

            this.mContactAdapter = new contactAdapter(this, R.layout.listview, contact_list);
            lv.setAdapter(this.mContactAdapter);
            mCursor.close();

            runOnUiThread(returnRes);

        } catch (Exception e) {
            Log.d("getContacts", e.getMessage());
        }
    }

    public class contactAdapter extends ArrayAdapter<contact> {

        public contactAdapter(Context context, int textViewResourceId, ArrayList<contact> items1) {
            super(context, textViewResourceId, items1);
            items = items1;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = vi.inflate(R.layout.listview, parent, false);
            cb = (CheckBox) convertView.findViewById(R.id.checkBox);



            if (isChecked[position] == true)
                cb.setChecked(true);
            else
                cb.setChecked(false);
            cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                public void onCheckedChanged(CompoundButton buttonView, boolean ischecked) {
                    if (buttonView.isChecked())
                        isChecked[position] = true;
                    else
                        isChecked[position] = false;
                }
            });

            contact contacts = items.get(position);
            if (contacts != null) {
                if (cb != null) {
                    cb.setText(contacts.getContactName());
                }
            }

            return convertView;
        }
    }


    private Runnable returnRes = new Runnable() {

        public void run() {
            if (mProgressDialog.isShowing())
                mProgressDialog.dismiss();
            mContactAdapter.notifyDataSetChanged();
        }
    };

}

这是它的xml

<?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="fill_parent"
    android:orientation="horizontal" >

      <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="14dp"
        android:textSize="25sp" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >

    </ListView>



</RelativeLayout>

请告诉我如何在屏幕底部添加单曲。通过单击该单个按钮,我将使用HTTP帖子在服务器上发布所有选中的项目。

1 个答案:

答案 0 :(得分:0)

要在View getView(final int position, View convertView, ViewGroup parent)函数中实现此功能,您必须检查最后一个索引,然后膨胀第二个布局,其中只包含一个按钮。简单:)

<强>更新

这是您当前的getView()充气代码

 LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 convertView = vi.inflate(R.layout.listview, parent, false);

现在对此代码添加if条件并在第二个布局中膨胀,如果当前索引是您为ListAdabper

设置的列表的最后一个索引
 LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         if(position != AdapterList.size()){

         convertView = vi.inflate(R.layout.listview, parent, false);}
       else{

         convertView = vi.inflate(R.layout.your_button_layout, parent, false);
    }

同时将getView()条件中的所有ifbutton条件中的else代码