1

我正在制作一个联系人管理器应用程序,并有一个联系人列表,每个联系人旁边都有复选框(使用 ListView)。运行应用程序时,我可以检查和取消选择每一个。我想删除已检查的联系人(按下删除按钮时),但我不确定如何执行此操作。每个联系人都与自己的唯一 ID 相关联,我的计划是,一旦用户选择删除按钮,将所有已检查的联系人 ID 存储在 ArrayList 中,然后遍历列表并从我的数据库中删除这些联系人。但是,我不确定如何实现这一点。

Here is my code for Delete Contacts:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.SparseBooleanArray;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

public class DeleteContacts extends ListActivity {

    // Initializing the listView and the contactList. The contactList is made public so that it is
    // accessible from any class.

    private ListView lv;
    private TextView contactId;
    private String sortOrder;
    ListAdapter adapter;

    public static final String LAST = "lastName";
    public static final String FIRST = "firstName";
    public static final String MOBILE = "mobileNumber";

    private ArrayList<HashMap<String, String>> listOfContacts;


    //The object that allows me to manipulate the Database
    DBTools dbTools = new DBTools(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getData(FIRST);



        // Checking to make sure that there are contacts to display.
        // Then setting up the view of the Home Screen
        if(listOfContacts.size()!=0){
            setupListView(listOfContacts);
        }

    }

    private void getData(String order) {
        listOfContacts = dbTools.getAllContacts(order);
    }

    private void setupListView(ArrayList<HashMap<String, String>> listOfContacts){

        lv = getListView();

//      listView.setOnItemClickListener(new OnItemClickListener(){
//
//          // Once any contact is clicked, the details are passed as extra information for
//          // the ViewContactsDetails class (using a Bundle). The application is then taken to
//          // the ViewContactDetails screen.
//          @Override
//          public void onItemClick(AdapterView<?> parentView, View clickedView, int clickedViewPosition, long id){
//
//              //When an item is clicked get the TextView with matching checkId
//
//              contactId = (TextView) clickedView.findViewById(R.id.contactId);
//
//              //Convert that contactId into a String
//
//              String contactIdValue = contactId.getText().toString();
//
//              // Intention to go to the ViewContactDetails class/screen
//
//              Intent theIntent = new Intent(getApplication(), ViewContactDetails.class);
//              // Put additional data in for EditContact to use
//
//              theIntent.putExtra("contactId", contactIdValue);
//
//              // calls for ViewContactDetails
//
//              startActivity(theIntent);
//
//          }
//
//      });

        adapter = new SimpleAdapter(DeleteContacts.this, listOfContacts, R.layout.activity_delete_contacts, new String[] {"contactId", "lastName", "firstName"}, new int[] {R.id.contactId, R.id.lastName, R.id.firstName});

        setListAdapter(adapter);


    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()) {
        // Once the add contact button is pressed, the application is taken to the AddContact screen.
        case R.id.addImageButton:
            Intent intent = new Intent(this, AddContact.class);
            this.startActivity(intent);
            break;

        case R.id.deleteButton:




            break;

        case R.id.sort_lastName:
            sortOrder = LAST;
            getData(sortOrder);
            setupListView(listOfContacts);
            break;

        case R.id.sort_firstName:
            sortOrder = FIRST;
            getData(sortOrder);
            setupListView(listOfContacts);
            break;  

        case R.id.sort_mobileNumber:
            sortOrder = MOBILE;
            getData(sortOrder);
            setupListView(listOfContacts);
            break;


        default:
            return super.onOptionsItemSelected(item);
        }

        return true;
    }

}

Here is my XML File for each entry:

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/contactImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/contactsmall" />

     <TextView
        android:id="@+id/contactId"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:visibility="gone" />
    <TextView
        android:id="@+id/firstName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="@dimen/padding_5dp"
        android:textColor="#444444"
        android:textSize="20sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/lastName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="@dimen/padding_5dp"
        android:textColor="#444444"
        android:textSize="20sp"
        android:textStyle="bold" />

</TableRow>
4

2 回答 2

0

要处理列表视图单元格上的视图,您必须在列表适配器的 getView() 中编写代码。以下是我的代码片段。SugarBean 是我的数组,其中包含列表视图单元格的信息。

public class ListAdapter  extends ArrayAdapter<SugarBean> {

    protected SugarBean[] items;
    protected int listViewCellId;

    public ListAdapter(ListviewHelper helper, int listViewCellId, SugarBean[] items) {
        super(helper.getActivity(), listViewCellId, items);

        this.items = items;
        this.listViewCellId= listViewCellId;

    }

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

        final SugarBean beanObject = items[position];// list view cell 

        View v = convertView;

        //load the custom cell
        if (v == null) {
            LayoutInflater vi = (LayoutInflater)              helper.getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(listViewCellId, null);
        } 

                RelativeLayout row = (RelativeLayout) v.findViewById(R.id.tableRow);
                // handle all views here

}
于 2013-10-23T06:53:53.547 回答
0

您需要做的是在使用复选框单击时,在数组中添加 id(这将在 onItemClickListener 下发生)。当你按下删除按钮时,检查 id。如果数组中存在相同的ID,则将其删除,否则将其保留。之后,一个新的项目数组传递给适配器或 notifyData 以重新生成列表视图。

于 2013-10-23T07:09:10.520 回答