0

我在 ListView 中显示数据,我从 2 个不同的ArrayList中获取数据,我已经转换为String[]我能够列出它们,但是 Listview 的 onClick 我正在从 ListView 中删除单击的元素并使用notifyDataSetChanged()所以 ListView 得到更新新元素,但在这里,当我单击 ListView 时它不会踢元素,但它仍然存在,但是当我打开 AlertDialog 框时,它会更新并单击自动删除元素,我正在使用以下代码。

import java.util.ArrayList; 
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.CallLog;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class Incoming_call extends Activity {
     Button phonebook,log,manuall;
     Intent intents;
     ListView incoming_list;
     ArrayList<String> array ;
     ArrayList<String> mobileNumbers;
     String  name ,editext;
     ArrayList<String> array_ed ;
     ArrayAdapter<String> ad;
     ArrayAdapter<String> adapter;
     public static final int PICK_CONTACT = 1;
    @Override
     public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.incoming);

            phonebook = (Button)findViewById(R.id.phonebook1);
            log = (Button)findViewById(R.id.call_log1);
            manuall = (Button)findViewById(R.id.manually1);
            incoming_list = (ListView)findViewById(R.id.list1);
            array = new ArrayList<String>();


            phonebook.setOnClickListener(new Button.OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                    // TODO Auto-generated method stub
                                      intents = new Intent(Intent.ACTION_PICK);
                                      intents.setType(ContactsContract.Contacts.CONTENT_TYPE);
                                      startActivityForResult(intents, PICK_CONTACT);

                            }
                    });
            log.setOnClickListener(new Button.OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                    // TODO Auto-generated method stub
                                    Intent i  = new Intent(Incoming_call.this,ListView2Activity.class);
                                    startActivity(i);
                            }
                    });
            manuall.setOnClickListener(new Button.OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                    // TODO Auto-generated method stub
                                    AlertDialog.Builder builder = new AlertDialog.Builder(Incoming_call.this);
                                builder.setCancelable(true);
                                builder.setTitle("My Title");
                                builder.setInverseBackgroundForced(true);
                                builder.setIcon(R.drawable.ic_launcher);
                                final EditText ed = new EditText(Incoming_call.this);
                                ed.setInputType(InputType.TYPE_CLASS_PHONE);
                                builder.setView(ed);
                                builder.setPositiveButton("Yes",
                                        new DialogInterface.OnClickListener() {
                                                        @Override
                                                        public void onClick(DialogInterface dialog, int which)
                                                        {
                                                            Log.d("log",ed.getText().toString());
                                                            array.add(ed.getText().toString());
                                                            ad = new ArrayAdapter<String>(Incoming_call.this,android.R.layout.simple_list_item_1,array);
                                                            incoming_list.setAdapter(ad);
                                                            ad.notifyDataSetChanged();
                                                            dialog.dismiss();
                                                         }
                                                   });
                                            builder.setNegativeButton("No",
                                                    new DialogInterface.OnClickListener() {
                                                        @Override
                                                        public void onClick(DialogInterface dialog,int which)
                                                        {
                                                            dialog.dismiss();
                                                        }
                                                    });
                                            AlertDialog alert = builder.create();
                                            alert.show();


                            }
                    });

            incoming_list.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,
                        final int arg2, long arg3) {
                    // TODO Auto-generated method stub

                    AlertDialog.Builder adb=new AlertDialog.Builder(Incoming_call.this);
                    adb.setMessage("Selected Item is = "+incoming_list.getItemAtPosition(arg2));
                    adb.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub
                            array.remove(arg2); 



                        }
                    });
                    adb.show();                                 }
            });                   
     }
      @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
              Log.d("log", "requestCode="+requestCode+", resultCode="+resultCode+", data="+data);
             super.onActivityResult(requestCode, resultCode, data);

   if(resultCode ==  Activity.RESULT_OK)
    {        
     switch(requestCode)
     {
             case  PICK_CONTACT:
             {      

                     Log.d("log","pick");
                 Cursor cursor =  managedQuery(data.getData(), null, null, null, null);
                 cursor.moveToNext();
                   name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
                 String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                 Log.d("log","name = "+name );
                 cursor.close();
                 ArrayList<String> homeNumbers = new ArrayList<String>();
                 mobileNumbers = new ArrayList<String>();
                 ArrayList<String> workNumbers = new ArrayList<String>();
                 Log.d("log","array");
                 ContentResolver cr = getContentResolver();
                 Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                         ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);
                 Log.d("log","cursor phone");
                 if(phones != null && phones.moveToFirst()) {
                     Log.d("log","if");
                         while (phones.isAfterLast() == false) {
                             Log.d("log","while");
                                 String number = phones.getString(phones.getColumnIndex(Phone.NUMBER));
                                 int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));

                                 switch (type) {
                                 case Phone.TYPE_HOME:
                                         // do something with the Home number here...
                                         Log.d("log","home");
                                     //    array.add("Name is :" +name + "Number :"+ number);
                                         homeNumbers.add(number);
                                         break;
                                 case Phone.TYPE_MOBILE:
                                         // do something with the Mobile number here...
                                             Log.d("log","mobile");
                                         Toast.makeText(this, "Contect LIST  =  "+name  +number, Toast.LENGTH_LONG).show();
                                      //   array.add("Name is :" +name + "Number :"+ number);
                                         mobileNumbers.add(number);
                                         break;
                                 case Phone.TYPE_WORK:
                                         // do something with the Work number here...
                                    //         array.add("Name is :" +name + "Number :"+ number);
                                                    Log.d("log","work");
                                         workNumbers.add(number);
                                         break;
                                 }  //switch end

                                 phones.moveToNext();
                                 phones.close();
                         } // while end
                 }  // if end

                Log.d("lmt", "Name :"+name  + " Number : " + mobileNumbers);
                if(mobileNumbers.equals(null) || mobileNumbers.equals(""))
                {
                    Toast.makeText(getApplicationContext(), "Dont have mobile Numbers", Toast.LENGTH_LONG).show();
                }
                else
                {
                    array.add(name + "    " +mobileNumbers);
                }

                    adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,array);
                    incoming_list.setAdapter(adapter);
                    adapter.notifyDataSetChanged();
                    Toast.makeText(Incoming_call.this, name +mobileNumbers, Toast.LENGTH_LONG).show();
             } // case end
     } // switch end
    } // if end

        } // onActivityResult End

      }  // All End
4

1 回答 1

0

有时 notifyDataSetChanged() 不起作用我仍在寻找原因,但要实现您的目标,您可以做一件事,通过 finish() 和 startActiviy() 重新启动您的活动。

我曾经面临同样的问题。

于 2012-05-01T10:32:51.170 回答