单击时从 ListView 继续进行新活动
Posted
技术标签:
【中文标题】单击时从 ListView 继续进行新活动【英文标题】:Proceed to new activity from ListView when clicked 【发布时间】:2015-11-08 23:58:03 【问题描述】:我在 android 中创建了一个列表视图,但现在我试图让列表视图在传递参数时可以点击到新活动。列表视图的数据来自 api。代码如下。
private static String urlString;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_market_place);
urlString = "http://websiteforapi/api/market";
new ProcessJSON().execute(urlString);
private class ProcessJSON extends AsyncTask<String, Void, String>
protected String doInBackground(String...strings)
String stream = null;
String urlString = strings[0];
HTTPDataHandler hh = new HTTPDataHandler();
stream = hh.GetHTTPData(urlString);
// Return the data from specified url
return stream;
protected void onPostExecute(String stream)
//..........Process JSON DATA................
if(stream !=null)
try
// Get the full HTTP Data as JSONObject
JSONObject reader = new JSONObject(stream);
JSONArray businessArray = reader.getJSONArray("data");
ArrayList<String> businesses = new ArrayList<String>();
for(int i = 0; i < businessArray.length(); i++)
JSONObject business_object_0 = businessArray.getJSONObject(i);
final String businessname = business_object_0.getString("name");
final String business = business_object_0.getString("business");
businesses.add(businessname);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getListView().getContext(),android.R.layout.simple_list_item_1, businesses);
getListView().setAdapter(adapter);
catch(JSONException e)
e.printStackTrace();
// if statement end
// onPostExecute() end
// ProcessJSON class end
我在 API 中使用的 JSON 是这样的......
"data":["id":"3","name":"My Test Business 3","phone":"6785555555","email":"business5@email.com","address":"123 This Avenue","city":"Atlanta","state":"GA","zip":"30308","business":"Technology 33333","id":"4","name":"My Test Business 2 ","phone":"6783571004","email":"test100@test.com","address":"123 Some Street","city":"Lawrenceville","state":"GA","zip":"30043","business":"Website building","id":"5","name":"My Test Business 3","phone":"6785555555","email":"business2@email.com","address":"123 This Avenue","city":"Atlanta","state":"GA","zip":"30308","business":"Technology 3","id":"6","name":"My Test Business 5","phone":"6785555555","email":"business6@email.com","address":"123 This Avenue","city":"Atlanta","state":"GA","zip":"30308","business":"Technology 4"]
到目前为止,我可以让 ListView 显示,但到目前为止,我无法让它们可点击到新活动。
【问题讨论】:
【参考方案1】:根据您的要求试试这个...
yourListView.setOnItemClickListener(new OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
// Code for start new Activity
Intent intent = new Intent(MainActivity.this, NewActivity.class);
startActivity(intent);
);
但是你为什么不使用主细节!!!
【讨论】:
不确定您所说的主细节是什么意思。但是代码有效。有没有办法将所选数组列表项的参数传递给下一个活动?我尝试使用以下内容。 Intent intent = new Intent(MarketPlaceActivity.this, SelectedBusinessActivity.class); intent.putExtra(“名称”,企业);开始活动(意图); 看到这个传递数据link【参考方案2】:你可以使用OnItemClickListener:
getListView().setOnItemClickListener(new AdapterView.OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
startActivity(this, NewActivity.class);
);
当然 newActivity 需要在 manifest 中注册。
【讨论】:
以上是关于单击时从 ListView 继续进行新活动的主要内容,如果未能解决你的问题,请参考以下文章
单击 Listview 中的项目时,如何在新活动中从 Firebase 检索数据?
如何通过单击按钮将 Listview 中的数据传递到购物车列表(新活动)。我插入到 listview 的数据正在使用 SQLite