简体   繁体   English

如何通过其他Activity在Android中填充ListView?

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

I want built a Android App and use Eclipse for this. 我想构建一个Android应用程序并为此使用Eclipse。 It is my first App and I have read on http://developer.android.com how I can create a App and more. 这是我的第一个应用程序,我已经在http://developer.android.com上阅读了如何创建应用程序及更多内容。

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. 在我的第三个Activity上,我有一个ListView但它为空。 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. 如果我单击Button的第二个Activity,我希望将字符串发送到第三个Activity的ListView。

a other Question is how I can save Informations in a Android App. 另一个问题是如何在Android应用程序中保存信息。 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: 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 第三次活动: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, 在按钮onclick的第二个活动中,

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. 您可以将数据存储在数据库或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. 如果要将大量数据存储为大量人员详细信息,则可以使用SQLite存储在数据库中,或者如果要存储诸如登录详细信息之类的较小数据,则可以存储在SharedPreferences中。

Refer the link for Database and SharedPreferences as 将数据库和SharedPreferences的链接引用为

SQLite SQLite的

SharedPreferences 共享首选项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Android中:如何将OnPostExecute()的结果发送到其他活动? - In Android: How can i send the result of from OnPostExecute() to other activity? 我如何将整个ListView项目从一个Activity传递到android中的另一个项目 - How can I pass the entire ListView item from one Activity to another in android 如何从listview获取变量并传递给另一个活动? - How can i get variable from listview and pass to another activity? 如何从主Activity中刷新片段中的ListView? - How can I refresh a ListView in a Fragment from the main Activity? 如何杀死android中的其他前台活动? - How can I kill other foreground activity in android? 填写其他活动的文本视图 - Fill a textview from other activity Android-Studio:如何从一个线程(另一个活动中的startet)更新聊天(ListView) - Android-Studio: How to update a chat (ListView) from a thread, which is startet in an other Activity 如何从Android ListView进行其他活动 - How to go Another Activity from Android ListView 从列表视图活动中选择一个选项后,如何返回主活动 - How can I go back to main activity after selecting an option from my listview activity 如何从其他活动中获取字符串到数组列表? - How can I get a string to an array list from other activity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM