0

我在 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 发布在服务器上发布所有选中的项目。

4

1 回答 1

0

要在您的 ViewgetView(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()if条件和条件的button代码else

于 2012-12-01T19:20:24.557 回答