【问题标题】:How do I pass listView data to another activity?如何将 listView 数据传递给另一个活动?
【发布时间】:2016-03-11 07:19:44
【问题描述】:

我在 Activity A 中有一个listView,所有数据都是从SQLite 检索的。现在我想在单击列表时将数据传递给另一个新活动。我怎样才能做到这一点?

活动 A

Activity A 中有两个列表,假设第一个列表被点击,我希望它传递数据给 B。

  listViewUpdate.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> listView, View view,
                                    int position, long id) {
                // Get the cursor, positioned to the corresponding listview_item_row in the result set

                Intent intent=new Intent(getActivity(),B.class);
                startActivity(intent);


            }
        });

我的活动 B 的某些部分

public class Edit_Details extends AppCompatActivity {

    EditText Description,TimeIn,TimeOut;
    String description;
    SeekBar seekBar;
    TextView progressText;
    int progress=0;
    SQLiteDatabase database;
    MyDatabaseHelper dbHelper;
    Cursor cursor;
    private com.example.project.myapplication.API.WorkDetailsAPI WD;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit_details);
        Project=(Spinner)findViewById(R.id.SpinnerProject);
        final String ID=getIntent().getStringExtra("ID"); // should be position?
        WD = new com.example.project.myapplication.API.WorkDetailsAPI(getApplication());
        Description=(EditText)findViewById(R.id.editTextWorkDescription);
        TimeIn=(EditText)findViewById(R.id.TimeIn);
        TimeOut=(EditText)findViewById(R.id.TimeOut);
        seekBar=(SeekBar)findViewById(R.id.seekBarPercentage);
        progressText=(TextView)findViewById(R.id.textProgress);
        progressText.setText("Covered:" + "" + seekBar.getProgress() + "/" + seekBar.getMax());
        Log.e("ID", ID);
        RetrieveDetails(ID); // how to get the position ?

    }

    public void RetrieveDetails(long ID)
    {
        final long id=ID;
        database=dbHelper.getWritableDatabase();
        cursor=database.rawQuery("SELECT SubContractors, NumberOfPerson, NumberOfHours FROM " + MyDatabaseHelper.TABLE_WORKFORCE+ " WHERE _id= ? ",
                new String[]{String.valueOf(id)}, null);

        Details d=new Details();
        if(cursor!=null) {
            while (cursor.moveToNext())
            {
                description=cursor.getString(cursor.getColumnIndexOrThrow(MyDatabaseHelper.WorkDescription));
                d.setWorkDescription(description); 
                Description.setText(description);  // display learn java on editText

            }
        }
    }
}

我需要知道如何让新活动记住被点击的列表项,然后从 A 中提取所有信息并显示在新活动中。如果这有意义..谢谢!

例如。记住精益java并在B上展示

【问题讨论】:

  • 查看传递给onItemClick的最后一个参数
  • 对不起,我不明白
  • 换句话说参见AdapterView.OnItemClickListener文档,尤其是idonItemClick的参数
  • 我需要知道如何让新活动记住被点击的列表项,然后从 A 中提取所有信息并显示在新活动中。如果这有意义..谢谢!
  • 所以通过额外的传递'id':intent.putExtra(KEY_ID, id)

标签: android sqlite listview android-intent


【解决方案1】:

在 Activity A 中,像这样向 Intent 添加数据

Intent intent = new Intent(A.this, B.class);
intent.putextra("keyName","value");
startActivity(intent);

在 Activity B 中,像这样检索数据

String data = getIntent().getExtras().getString("keyName");

您可以添加多个键值对。

编辑:

如果您有一个对象,比如 Data,其中包含描述、进度等值,您可以使用 onItemClick() 中的 listViewUpdate.setOnItemClickListener(...) 中的以下代码来获取它。

Data data = (Data) listView.getAdapter().getItem(position);

并在意图中传递整个对象。

如果您不熟悉我们如何通过意图传递对象,此 SO post 可能会对您有所帮助。

【讨论】:

  • 我需要知道如何让新活动记住被点击的列表项,然后从 A 中提取所有信息并显示在新活动中。如果这有意义..谢谢!
  • 非常感谢..我会看看
猜你喜欢
  • 1970-01-01
  • 2011-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多