【问题标题】:How I can fill my ListView in Android如何在 Android 中填写我的 ListView
【发布时间】:2012-10-10 12:36:09
【问题描述】:

我是 Android 编程新手,我想建立一个数字学校计划。为此,我使用了一些活动。我想创建一个计划并将它们列出在 ListView 中,用户可以在此控件上的 ListView 中单击并查看所有作业等。

这是我的想法:

我为 CreateMain 构建了一个 Activity:

 public void createPlan(View view)
    {
        String PlanName;

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

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

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

        intent.putExtra(ListViewMessage, PlanName); 

        startActivity(intent);

    }

还有一个用于在 ListView 中显示创建数据的 Activity

import android.os.Bundle;
import android.view.Menu;
import java.util.List;
import android.app.ListActivity;
import android.widget.ArrayAdapter;

public class ListenActivity extends ListActivity {

    private CommentsDataSource datasource;

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

        datasource = new CommentsDataSource(this);
        datasource.open();

        List<Comment> values = datasource.getAllComments();

        // Use the SimpleCursorAdapter to show the
        // elements in a ListView
        ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(this,
            android.R.layout.simple_list_item_1, values);
        setListAdapter(adapter);

        Comment comment = null;

          String[] comments = new String[] { "t1", "t2", "t3" };

          comment = datasource.createComment(comments[1]); 

          adapter.add(comment);

        adapter.notifyDataSetChanged();

    }

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

但我不知道如何在我的 ListView 中将之前的活动数据用于此活动。我想使用 SQLite 数据库,但它不起作用:(

【问题讨论】:

  • 让我看看我是否理解你。您想使用 SQLite 在您的活动中传递数据吗?
  • 是的,我希望我可以添加一个 ListView 行

标签: android sqlite listview android-intent android-arrayadapter


【解决方案1】:

通过活动传递数据的方法很少。由于您将信息存储在Bundle 中,我将按照相同的逻辑进行回答。

如果您没有注意到,您将您的信息存储在您的createPlan 方法中的Intent 的Extra Bundle 中。在那之后,你开始了Intent

根据官方文档: "...extras -- 这是任何附加信息的 Bundle。这可用于向组件提供扩展信息。例如,如果我们有发送电子邮件消息的操作,我们还可以在此处包含额外的数据,以提供主题、正文等..."

因此,要获取该信息,您可以在 ListenActivity 中使用 getIntent().getExtras() 收回您的 Bundle,然后使用 getIntent().getExtras().getString(your_key) 传递用于存储计划名称的密钥。

例如:

public class ListenActivity extends ListActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
   //...your stuff

   String planName = getIntent().getExtras().getString(ListViewMessage);

   //...your stuff
}

}

我不推荐使用 SQLite 来存储少量数据。如果你真的想用,有很好的例子here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多