notifyDataSetChanged()不起作用

时间:2012-05-01 10:29:44

标签: android listview

我在ListView中显示数据,我从2个不同的 ArrayList 获取数据,我已经转换为 String [] 我能够列出它们但是点击了Listview我正在从ListView中删除clicked元素并使用 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

1 个答案:

答案 0 :(得分:0)

有些时候notifyDataSetChanged()不起作用我仍然找到原因但是为了实现你的目标,你可以通过finish()和startActiviy()重新启动你的活动。

我曾经遇到同样的问题。