简体   繁体   中英

How I can fill a ListView in Android from a other Activity?

I want built a Android App and use Eclipse for this. It is my first App and I have read on http://developer.android.com how I can create a App and more.

To my Appliation:

I use three Activitys.

The First Activity ist the main. On the Second Activity can I input a string and have a Button. On my third Activity I have a ListView but its empty. If I click on the second Activity of the Button I want that the string will be send to the ListView on the Third Activity.

a other Question is how I can save Informations in a Android App. Can I use a database or what is the right way. I want save the listview and if I open the Activity from the Main Activity i want see the Listview with the Informations.

MyCode:

FirstActivity:

public class MainActivity extends Activity {

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


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    public void createPlan(View view)
    {
        Intent intent = new Intent(this,CreateActivity.class);

        startActivity(intent);

    }
}

Second Activity:

public class CreateActivity extends Activity {

    public final static String ListViewMessage = "de.linde.KSDILLPlan";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_create);

        FillSpinnerViews(); 
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.test, menu);
        return true;
    }

    public void FillSpinnerViews()
    {
        Spinner spinner = (Spinner)findViewById(R.id.spinner2);

        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.daysArray, android.R.layout.simple_spinner_item);

        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        spinner.setAdapter(adapter);


        Spinner Spinner2 = (Spinner)findViewById(R.id.spinner1);

        ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this, R.array.zeitArray, android.R.layout.simple_spinner_item);

        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        Spinner2.setAdapter(adapter2); 

    }

    public void createPlan(View view)
    {
        String PlanName;
        //String StundenZahl;
        //String Wochentag;
        //Boolean doppelstunde; 

        Intent intent = new Intent(this,OpenActivity.class);

        EditText planName = (EditText)findViewById(R.id.editText1);

        PlanName = planName.getText().toString();

        intent.putExtra(ListViewMessage, PlanName); 

        startActivity(intent);

    }

Third Activity: The ListView

public class OpenActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_open);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_open2, menu);
        return true;
    }
}

Use following code to get the string from intent's bundle.

public class OpenActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_open);
        String text = getIntent().getStringExtra( CreateActivity.ListViewMessage );
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_open2, menu);
        return true;
    }
}

Refer this link for persistent storage question.

You can pass the data from one activity to another using intent as follows,

In second activity in onclick of button,

Intent in=new Intent(currentclass.this,nextactivity.class);
in.putExtras("value_name",value);
startActivity(in);
finish();

In third activity,

If(getIntent().getExtras!=null)
{
   String msg=getIntent().getStringExtra("value_name"); // value_name is the attribute given in second activity
}

You can store the data in the database or sharedpreference. it depends upon your needs. If you want to store bulk data as large number of person details, you can store in database using SQLite or if you want to store small data like login details you can store in SharedPreferences.

Refer the link for Database and SharedPreferences as

SQLite

SharedPreferences

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM