关键词:
【中文标题】将活动之间的数据从列表视图传递到另一个活动【英文标题】:Passing data between activities from listview to another activity 【发布时间】:2013-08-14 12:19:05 【问题描述】:我正在尝试将数据从这个主要活动传递到另一个活动
我成功地在活动之间发送数据....就像通过 putExtra
和 GetExtra
方法将数据从 Edit-text 发送到下一个活动并作为意图传递
任何想法,
ativity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_ >
<ListView
android:id="@+id/listViewID"
android:layout_
android:layout_
android:layout_gravity="center">
</ListView>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity
// url to make request
private static String url = "http://54.218.73.244:7002/";
List<Item> yourData = new ArrayList<Item>();
ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Instantiating ProgressDialog with onCreate method
progressDialog=new ProgressDialog(MainActivity.this);
new ParsingAsync().execute();
private class ParsingAsync extends AsyncTask<Void, Void, Void>
@Override
protected void onPreExecute()
// TODO Auto-generated method stub
super.onPreExecute();
progressDialog=ProgressDialog.show(MainActivity.this, "", "Please Wait", true, false);
@Override
protected Void doInBackground(Void... params)
// TODO Auto-generated method stub
// Creating JSON Parser instance
JSONObjParser jParser = new JSONObjParser();
// getting JSON string from URL
JSONArray json = jParser.getJSONFromUrl(url);
try
for (int i = 0; i < json.length(); i++)
JSONObject c = json.getJSONObject(i);
// Storing each json item in variable
String NAME=c.getString("restaurantNAME");
yourData.add(new Item(NAME));
catch (JSONException e)
// TODO Auto-generated catch block
e.printStackTrace();
return null;
@Override
protected void onPostExecute(Void result)
// TODO Auto-generated method stub
super.onPostExecute(result);
progressDialog.dismiss();
ListView yourListView = (ListView) findViewById(R.id.listViewID);
ListAdapter customAdapter = new ListAdapter(MainActivity.this, R.layout.itemlistrow, yourData);
yourListView.setAdapter(customAdapter);
yourListView.setOnItemClickListener(new OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
// When clicked, show a toast with the TextView text
if(position == 0)
//code specific to first list item
Intent myIntent = new Intent(MainActivity.this,CopperChimneyDesc.class);
startActivity(myIntent);
else if(position == 1)
//Intent myIntent = new Intent(MainActivity.this,AroyDesc.class);
//startActivity(myIntent);
);
item.java
public class Item
private String Name;
public Item(String name)
this.Name = name;
public String getName()
return Name;
谢谢,
【问题讨论】:
所以您尝试传递您在列表视图中单击的餐厅名称 在onItemClick中,您可以使用:Item item = yourData.get(position);现在可以使用intent.putExtra来传递这个item了,顺便问下这个Item对象长什么样子 我已经更新了项目对象在问题中的样子......请看一下......我尝试了你的解决方案,我在 putextra 行中得到错误The method putExtra(String, boolean) in the type Intent is not applicable for the arguments (String, Item)
好的,所以你会这样做 String item = yourData.get(position).getName();然后您可以使用以下方法将字符串添加到意图:intent.putExtra("restaurent", item);
在另一端,如果它的文本视图你会 textView.setText(getIntent().getExtras().getString("restaurent"));我会纠正这个答案,如果我解决了你的问题,请标记它是正确的
【参考方案1】:
好的,你会这样做的
String item = yourData.get(position).getName();
然后您可以使用以下命令将string
添加到intent
:
intent.putExtra("restaurent", item);
另一方面,如果它的textview
你会这样做
textView.setText(getIntent().getExtras().getString("restaurent"));
【讨论】:
谢谢,您的解决方案很有魅力:)【参考方案2】:intent.putExtra("City Name", String.valueOf(lvCity.getSelectedItem()));
试试这个来投射。
【讨论】:
【参考方案3】:您可以使用 putxtera 方法打包数据并将其发送到其他活动。请参阅How to store values in onSaveInstanceState() and retrive?
【讨论】:
【参考方案4】:由于您在用户单击的列表视图中拥有项目的索引,因此您可以从适配器获取项目值:
String value = (String)customAdapter.getItem(position);
Intent myIntent = new Intent(MainActivity.this,CopperChimneyDesc.class);
intent.putExtra("restaurant_name", value);
startActivity(myIntent);
【讨论】:
我尝试了您的解决方案,但出现错误Cannot cast from item to string
....关于如何解决此问题的任何想法【参考方案5】:
您可以将数据保存在sharedPreferences
中,并可以从其他活动中访问它。只是一个想法:)
【讨论】:
但是如何从列表视图中获取数据(JSON)并将其存储在字符串中......有什么想法吗? 如果数据是静态的,比如session_key
,我认为SharePreference
是可行的方法。
mJsonString = downloadFileFromInternet(urls[0]); if(mJsonString == null /*|| mJsonString.isEmpty()*/) 返回 false; JSONObject jObject = null;尝试 jObject = new JSONObject(mJsonString); JSONArray jsonImageArray = jObject.getJSONArray("imageTarget");当我单击列表视图的操作项时,将文本从列表视图传递到另一个活动
...击列表视图的操作项时,将文本从列表视图传递到另一个活动【英文标题】:PassingTextFromListviewtoanotherActivitywhenIclickactionitemofthelistview【发布时间】:2013-03-1015:04:59【问题描述】:我的主要活动中有一个列表视图。在每一行中我... 查看详情
将数据从列表视图(从 Rest Api 生成)传递到另一个活动并将其保存到 SQLite 数据库中
】将数据从列表视图(从RestApi生成)传递到另一个活动并将其保存到SQLite数据库中【英文标题】:passDatafromaListView(generatedfromRestApi)toanotheractivityandsavethatintoSQLiteDatabase【发布时间】:2016-11-0115:24:11【问题描述】:我正在使用RESTap... 查看详情
将列表从一个活动传递到另一个活动
】将列表从一个活动传递到另一个活动【英文标题】:PassingaListfromoneActivitytoanother【发布时间】:2012-07-0515:32:45【问题描述】:如何通过Intent的putExtra方法将ArrayList等集合从一个Activity传递到另一个集合?谁能帮助我,因为我想... 查看详情
如何将 LatLng 的数组列表从一个活动传递到另一个活动
】如何将LatLng的数组列表从一个活动传递到另一个活动【英文标题】:HowtopassanarrylistofLatLngfromoneactivitytoanother【发布时间】:2015-09-0509:41:26【问题描述】:我想通过按钮单击将ArrayList的LatLng变量从一个Activity传递给另一个。String... 查看详情
如何将值从动态列表视图传递到活动?
】如何将值从动态列表视图传递到活动?【英文标题】:Howtopassvaluefromdynamiclistviewtoactivity?【发布时间】:2016-03-0117:53:59【问题描述】:我是android新手,我创建了一个简单的listview,它从mysql获取数据并且一切正常,但现在我想... 查看详情
我如何将数据从一个活动传递到另一个活动
】我如何将数据从一个活动传递到另一个活动【英文标题】:howipassdatafromoneactivitytoanotheractivity【发布时间】:2015-12-3120:26:37【问题描述】:packagecom.example.assignmentone;importjava.util.ArrayList;importandroid.R.integer;importandroid.app.Activity;impor... 查看详情
从以前的活动中获取文本并将其添加到另一个活动列表视图
】从以前的活动中获取文本并将其添加到另一个活动列表视图【英文标题】:Gettingtextfrompreviousactivityandaddingitanotheractivitieslistview【发布时间】:2015-06-3011:33:17【问题描述】:如何在单击按钮时将项目动态添加到列表中?文本被... 查看详情
使用捆绑将数据从一个活动传递到另一个活动 - 不在第二个活动中显示
】使用捆绑将数据从一个活动传递到另一个活动-不在第二个活动中显示【英文标题】:Passingdatafromoneactivitytoanotherusingbundle-notdisplayinginsecondactivity【发布时间】:2013-03-0422:07:16【问题描述】:我目前正在尝试获取通过RESTAPI调用获... 查看详情
将数据从片段列表视图项传递到活动字符串变量 onitemclicklistener
】将数据从片段列表视图项传递到活动字符串变量onitemclicklistener【英文标题】:PassingDatafromFragmentlistviewitemtoActivitystringvariableonitemclicklistener【发布时间】:2018-04-1422:10:12【问题描述】:我正在尝试将ListView项从片段传递到活动中... 查看详情
从活动传递数据到类扩展视图
】从活动传递数据到类扩展视图【英文标题】:PassDataMovingfromactivitytoClassextendsView【发布时间】:2015-07-0423:43:22【问题描述】:我正在尝试将MainActivity中的数据传递给我的类VistaJuego,该类绘制了一个在屏幕上移动的玩偶,但是当... 查看详情
如何将数据从main活动传递到另一个活动(代码片段)
我想将用户输入的名称和号码传递给我的Android应用程序中的FakeRinger类。我在putExtra()的意图中使用了fab.setOnClickListener方法。这是我在MainActivity中使用的代码publicclassMainActivityextendsAppCompatActivitypublicstaticfinalStringLOG_TAG="MainActivit 查看详情
不能将列表视图的位置传递给第二个活动?
】不能将列表视图的位置传递给第二个活动?【英文标题】:Can\'tPassthepostionofalistviewtosecondactivity?【发布时间】:2015-03-2116:41:24【问题描述】:您好,我创建了一个存储一些编辑文本的数据库。我有三个活动A、B和C。活动A在列... 查看详情
通过第二个活动从数据库传递列表视图的项目位置
】通过第二个活动从数据库传递列表视图的项目位置【英文标题】:Passingitempositionoflistviewfromdatabasethrough2ndactivity【发布时间】:2015-03-2712:22:08【问题描述】:您好,我创建了一个可以保存笔记的日记。我有3个活动,比如说A、B... 查看详情
将项目添加到另一个活动中的列表视图(仅添加 1 个条目)
】将项目添加到另一个活动中的列表视图(仅添加1个条目)【英文标题】:AddItemstoaListviewwhichisinanotheractivity(AddsOnly1entry)【发布时间】:2019-07-3118:02:18【问题描述】:我正在尝试制作一个应用程序,我想在其中将值从editText(MainAc... 查看详情
如何将整数从一个活动传递到另一个活动?
】如何将整数从一个活动传递到另一个活动?【英文标题】:HowtopassintegerfromoneActivitytoanother?【发布时间】:2011-10-2719:05:03【问题描述】:我想将一个整数的新值从一个活动传递到另一个活动。即:活动B包含一个integer[]pics=R.drawa... 查看详情
如何从自定义适配器加载活动 Yes No 对话将数据传递到列表视图?
】如何从自定义适配器加载活动YesNo对话将数据传递到列表视图?【英文标题】:HowtoloadactivityfromcustomadapterYesNoDialoguepassingdatatoalistview?【发布时间】:2021-08-1608:28:54【问题描述】:我有2个自定义列表视图。我使用一个通过编辑... 查看详情
将整个捆绑(对象)本身从某个活动的片段传递和接收到另一个活动
】将整个捆绑(对象)本身从某个活动的片段传递和接收到另一个活动【英文标题】:Passingandreceivinganentirebundle(object)itselffromfragmentofsomeactivitytoanotheractivity【发布时间】:2021-03-0508:37:29【问题描述】:我正在开发一个android应用... 查看详情
如何将用户定义的对象从一个活动传递到另一个活动[重复]
】如何将用户定义的对象从一个活动传递到另一个活动[重复]【英文标题】:howtopassauserdefinedobjectfromoneactivitytoanother[duplicate]【发布时间】:2012-10-1122:35:31【问题描述】:可能重复:HowtopassobjectfromoneactivitytoanotherinAndroid我想将一... 查看详情