intent方法(onActivityResult)擦除以前listview存储的数据

时间:2016-04-25 15:02:07

标签: java android listview android-intent

我正在尝试在CreateGroup.java中创建一组电话联系人。因此,为此目的,我使用ContactsView.java转到名为Intent的新活动。在那里我得到所有联系人的清单。当我单击该列表中的联系人时,它会获取该联系人项目的名称并将其返回到名为CreateGroup.java的上一个活动。但是当我尝试添加更多联系人时,它正在替换listview中之前添加的项目。我无法在列表中添加多个联系人。

请有人帮助我!!

CreateGroup.java 


    public class CreateGroup extends ActionBarActivity {
    TextView textView;
    ListView show;
    ArrayList<String> addArray=new ArrayList<String >();

public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.create_group);

 ImageButton next = (ImageButton)findViewById(R.id.imgButtonAddContacts);
            textView=(TextView)findViewById(R.id.textViewtst);
            show= (ListView) findViewById(R.id.listView2);
            next.setOnClickListener(new View.OnClickListener() {

public void onClick(View view) {
        Intent myIntent = new Intent(view.getContext(), ContactsView.class);
                    startActivityForResult(myIntent, 0);
                }

            });
        }


 @Override
 public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);

       switch(requestCode) {
                case (0) : {
                    if (resultCode == Activity.RESULT_OK) {
                        String newText = data.getStringExtra("CName");
                        addArray.add(newText);
                        ArrayAdapter<String>adapter=new ArrayAdapter<String> (CreateGroup.this,android.R.layout.simple_list_item_1,addArray);
                        show.setAdapter(adapter);
                    }
                   break;
                }
            }
       }
    }

这里是ContactsView.java

    public class ContactsView extends ActionBarActivity {
        String namecsv="";
        String phonecsv="";
        String namearray[];
        String phonearray[];
        ListView lv1;


        ArrayList<String> list_items= new ArrayList<String>();

        //declare a variable for counting of selected items.
        int count=0;

        /** Called when the activity is first created. */
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.contacts_view);


            lv1=(ListView)findViewById(R.id.listView);
            list_items.add("one");
         Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
            while (phones.moveToNext()){
String name= phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

                String phonenumber= phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                if(name!=null){
                    namecsv+=name + ",";
                    phonecsv+=phonenumber + ",";
                }
            }
            phones.close();
            namearray=  namecsv.split(",");
            phonearray=phonecsv.split(",");


            final ArrayAdapter <String> adapter = new ArrayAdapter <String> (this,android.R.layout.simple_list_item_1,android.R.id.text1,namearray);
            lv1.setAdapter(adapter);


            lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    String msgname = namearray[position];
                    String msgnum = phonearray[position];
                    Toast.makeText(getApplicationContext(), msgname + " " + msgnum, Toast.LENGTH_SHORT).show();

                    Intent resultIntent = new Intent();
                    resultIntent.putExtra("CName", msgname);
                    setResult(Activity.RESULT_OK, resultIntent);
                    finish();
                }
            });

        }
    }

我已根据提供的答案更新了我的代码。但仍面临同样的问题。它仅显示在列表视图中的最后一个添加项目上。上一项删除。

 @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch(requestCode) {
           case (0) : {
                if (resultCode == Activity.RESULT_OK) {
                    String newText = data.getStringExtra("CName");
                    textView.setText(newText);
                    addArray.add(newText);
                    adapter.notifyDataSetChanged();
                    show.setAdapter(adapter);
                }
                break;
            }
            default:
                super.onActivityResult(requestCode, resultCode, data);
        }
   }

1 个答案:

答案 0 :(得分:0)

您不应该首先致电super.onActivityResult(requestCode, resultCode, data);,但只有当您面对requestCode时,您的代码才会处理。换句话说,这应该添加到switch部分的default块中。另外一点是你不应该每次都设置新的适配器。所以这个:

addArray.add(newText);
ArrayAdapter<String>adapter=new ArrayAdapter<String> (CreateGroup.this,android.R.layout.simple_list_item_1,addArray);
show.setAdapter(adapter);

应替换为:

addArray.add(newText);
adapter.notifyDatasetChanged();

只是将数据添加到当前数组,然后通知适配器(当然还需要在onCreate()中创建)关于数据集中的更改。