如何将listView数据传递给另一个活动? [英] How do I pass listView data to another activity?
问题描述
我在活动A 中有一个listView
,其中所有数据均从SQLite
中检索.现在,我想在单击列表时将数据传递给另一个新活动.我该如何实现?
I have a listView
in Activity A, where all the data are retrieved from SQLite
. Now I want to pass the data to another new activity when list is clicked. How can I achieve this ?
活动A
活动A中有两个列表,假设单击了第一个列表,我希望它将数据传递给B.
There are two list in Activity A, assume first list is clicked, I want it pass data to 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上显示
Eg. Remember lean java and display on B
推荐答案
在活动A中,将数据添加到意图中
In Activity A, add data to intent like this
Intent intent = new Intent(A.this, B.class);
intent.putextra("keyName","value");
startActivity(intent);
在活动B中,像这样检索数据
In Activity B, retrieve data like this
String data = getIntent().getExtras().getString("keyName");
您可以添加多个键值对.
You can add multiple key-value pairs.
如果您有一个对象Data
,其中包含描述,进度...的值,则可以使用listViewUpdate.setOnItemClickListener(...)
的onItemClick()
中的以下代码来获取它.
If you have an Object say Data
which holds values of description, progress,..., you could get it using below code inside onItemClick()
of listViewUpdate.setOnItemClickListener(...)
.
Data data = (Data) listView.getAdapter().getItem(position);
并按意图传递整个对象.
and pass the whole object in the intent.
If you are not familiar as to how we can pass an object in intent, this SO post might help you.
这篇关于如何将listView数据传递给另一个活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!