adding item to listView after passing info through an intent in Android

前端 未结 2 805
旧时难觅i
旧时难觅i 2021-01-26 16:31

I am trying to add an item, first by using an add button, then going to a different activity, then coming back to the original one and adding it in a listview. I can\'t seem to

2条回答
  •  后悔当初
    2021-01-26 17:08

    Here is a tested prototype:

    TestResultActivity.java

    package com.arrdude.forumanswer;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.ListAdapter;
    import android.widget.ListView;
    import android.widget.SimpleAdapter;
    
    public class TestResultActivity extends Activity implements OnClickListener {
        private static final int REQUEST_LIST_ITEM = 235; //arbitrary request code
        static final String ADAPTER_VALUE_1 = "head";
        static final String ADAPTER_VALUE_2 = "sub";
    
        ArrayList> masterlist = null;
        ListAdapter adapter = null;
    
        ListView listview = null;
        Button addbutton = null;
    
        public ListAdapter getAdapter() {
            if(adapter == null){
                adapter = new SimpleAdapter(this, getMylist(), R.layout.listitem, new String[] {ADAPTER_VALUE_1, ADAPTER_VALUE_2}, new int[] {R.id.listheading, R.id.listsubheading});
            }
            return adapter;
        }
    
        public ListView getListview() {
            if(listview==null){
                listview = (ListView) findViewById(R.id.mainlistview);
                listview.setAdapter(getAdapter());
            }
            return listview;
        }
    
        public Button getAddbutton() {
            if(addbutton==null){
                addbutton = (Button) findViewById(R.id.mainaddbutton);
                addbutton.setClickable(true);
                addbutton.setOnClickListener(this);
            }
            return addbutton;
        }
    
        public ArrayList> getMylist() {
            if(masterlist==null){
                masterlist = new ArrayList>();
            }
            return masterlist;
        }
    
        public void addListItem(String head, String sub){
            HashMap addme=new HashMap();
            addme.put(ADAPTER_VALUE_1, head);
            addme.put(ADAPTER_VALUE_2, sub);
    
            masterlist.add(addme);
        }
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            //lazy init objects not the most efficient in Android but easier reading
            getListview();
            getAddbutton();
            addListItem("Test Head", "Test sub heading here below"); //an initial item for testing
        }
    
        @Override
        public void onClick(View v) {
            Intent launchadditemI = new Intent(this, AddItemActivity.class);
            startActivityForResult(launchadditemI, REQUEST_LIST_ITEM);
        }
    
        @Override
        public void onActivityResult(int req, int res, Intent data){
            if(req == REQUEST_LIST_ITEM && res == RESULT_OK){
                String thishead = data.getStringExtra(ADAPTER_VALUE_1);
                String thissub = data.getStringExtra(ADAPTER_VALUE_2);
                addListItem(thishead, thissub);
            }
        }
    }
    

    AddItemActivity.java

    package com.arrdude.forumanswer;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    
    public class AddItemActivity extends Activity implements OnClickListener {
        EditText headeredittext = null;
        EditText subheadedittext = null;
        Button donebutton = null;
        Button cancelbutton = null;
    
        public EditText getHeaderedittext() {
            if(headeredittext==null){
                headeredittext = (EditText) findViewById(R.id.headedittext);
            }
            return headeredittext;
        }
    
        public EditText getSubheadedittext() {
            if(subheadedittext==null){
                subheadedittext = (EditText) findViewById(R.id.subedittext);
            }
            return subheadedittext;
        }
    
        public Button getDonebutton() {
            if(donebutton==null){
                donebutton = (Button) findViewById(R.id.affffdonebutton);
                donebutton.setClickable(true);
                donebutton.setOnClickListener(this);
            }
            return donebutton;
        }
    
        public Button getCancelbutton() {
            if(cancelbutton==null){
                cancelbutton = (Button) findViewById(R.id.addcancelbutton);
                cancelbutton.setOnClickListener(this);
            }
            return cancelbutton;
        }
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.additem);
            //lazy init objects not the most efficient in Android but easier reading
            getHeaderedittext();
            getSubheadedittext();
            getDonebutton();
            getCancelbutton();
        }
    
        @Override
        public void onClick(View arg0) {
            switch (arg0.getId()) {
            case R.id.addcancelbutton:
                finish();
                break;
    
            case R.id.affffdonebutton:
                sendResult();
                break;
    
            default:
                break;
            }
        }
    
        private void sendResult() {
            Intent resultI = new Intent();
            resultI.putExtra(TestResultActivity.ADAPTER_VALUE_1, getHeaderedittext().getText().toString());
            resultI.putExtra(TestResultActivity.ADAPTER_VALUE_2, getSubheadedittext().getText().toString());
            setResult(RESULT_OK, resultI);
            finish();
        }
    }
    

    main.xml

    
    
    
    
    
    
    

    additem.xml

    
    
        
            
            
                
            
        
        
            
            
        
        
            
            
        
    
    
    

    listitem.xml

    
    
        
        
    
    

    strings.xml

    
    
        List Items:
        TestResultActivity
        Add New Item
    
    

    AndroidManifest.xml

    
    
        
    
        
            
                
                    
                    
                
            
            
    
        
    
    

    Happy coding!

提交回复
热议问题