gpt4 book ai didi

android - 如何启动一个新的 Activity 表单ListView并给它多个参数

转载 作者:行者123 更新时间:2023-11-29 16:06:59 27 4
gpt4 key购买 nike

下面是我的代码,它在从 json 解析的 ListView 中显示数据。
当用户单击列表中的任何项目时,我想开始新的 Activity 。

我关注了这个网址 http://www.androidhive.info/2012/01/android-json-parsing-tutorial/这是 json 文件 http://api.androidhive.info/contacts/

当用户点击 ListView 中的任何项目并将剩余的 json 值作为参数传递时,如何启动一个新 Activity ?

现在我的 ListView 仅显示姓名,但我想将其余项目(例如电子邮件、性别和手机)传递给其他 Activity 。


                           "id": "c200",
"name": "Ravi Tamada",
"email": "ravi@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"






public class NewsRowAdapter extends ArrayAdapter<Item> {

private Activity activity;
private List<Item> items;
private Item objBean;
private int row;
Context context;
public NewsRowAdapter(Activity act, int resource, List<Item> arrayList) {
super(act, resource, arrayList);
this.activity = act;
this.row = resource;
this.items = arrayList;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder holder;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(row, null);

holder = new ViewHolder();
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}

if ((items == null) || ((position + 1) > items.size()))
return view;

objBean = items.get(position);

holder.tvName = (TextView) view.findViewById(R.id.txtText);


if (holder.tvName != null && null != objBean.getName()
&& objBean.getName().trim().length() > 0) {
holder.tvName.setText(Html.fromHtml(objBean.getName()));
Intent intent=new Intent(context,TodayLunch.class);
intent.putExtra("name", Html.fromHtml(objBean.getName()));
context.startService(intent);

}


return view;
}

public class ViewHolder {
public TextView tvName, tvCity, tvBDate, tvGender, tvAge;
}
}





package com.schoollunchapp;





public class SeletecDayofweek extends Activity implements OnItemClickListener {


private static final String rssFeed = "http://192.168.2.100/jsonparsing.txt";

private static final String ARRAY_NAME = "student";
private static final String ID = "id";
private static final String NAME = "name";
private static final String CITY = "dish";
private static final String GENDER = "Gender";
private static final String AGE = "age";
private static final String BIRTH_DATE = "birthdate";


ListView listMainMenu;
List<Item> arrayOfList;
//MainMenuAdapter mma;
NewsRowAdapter objAdapter;

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

listMainMenu = (ListView) findViewById(R.id.listMainMenu2);
listMainMenu.setOnItemClickListener(this);

arrayOfList = new ArrayList<Item>();

if (URLUtils.isNetworkAvailable(SeletecDayofweek.this)) {
new MyTask().execute(rssFeed);
} else {
showToast("No Network Connection!!!");
}

}

// My AsyncTask start...

class MyTask extends AsyncTask<String, Void, String> {

ProgressDialog pDialog;

@Override
protected void onPreExecute() {
super.onPreExecute();

pDialog = new ProgressDialog(SeletecDayofweek.this);
pDialog.setMessage("Loading...");
pDialog.setCancelable(false);
pDialog.show();
}

@Override
protected String doInBackground(String... params) {
return URLUtils.getJSONString(params[0]);
}

@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);

if (null != pDialog && pDialog.isShowing()) {
pDialog.dismiss();
}

if (null == result || result.length() == 0) {
showToast("No data found from web!!!");
SeletecDayofweek.this.finish();
} else {

try {
JSONObject mainJson = new JSONObject(result);
JSONArray jsonArray =
mainJson.getJSONArray(ARRAY_NAME);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject objJson =
jsonArray.getJSONObject(i);

Item objItem = new Item();

objItem.setId(objJson.getInt(ID));
objItem.setName(objJson.getString(NAME));
objItem.setCity(objJson.getString(CITY));

objItem.setGender(objJson.getString(GENDER));
objItem.setAge(objJson.getInt(AGE));

objItem.setBirthdate(objJson.getString(BIRTH_DATE));

arrayOfList.add(objItem);

}
} catch (JSONException e) {
e.printStackTrace();
}

setAdapterToListview();

}

}
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// showDeleteDialog(position);




}


public void setAdapterToListview() {
objAdapter = new NewsRowAdapter(SeletecDayofweek.this,
R.layout.main_menu_item,
arrayOfList);
listMainMenu.setAdapter(objAdapter);
}

public void showToast(String msg) {
Toast.makeText(SeletecDayofweek.this, msg, Toast.LENGTH_LONG).show();
}

}

最佳答案

像这样创建数组列表

    public ArrayList<String> Id = new ArrayList<String>();
public ArrayList<String> Name = new ArrayList<String>();
public ArrayList<String> Gender= new ArrayList<String>();


for (int i = 0; i < jsonArray.length(); i++) {
JSONObject objJson = jsonArray.getJSONObject(i);

// here you can get id,name,city...

Id.add(objJson.getInt("id"));

Name.add(objJson.getString("name"));
Gender.add(objJson.getString("Gender"));




//You need to use this code in the class where you have the view ,

// list item click
List_View.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub



Intent i = new Intent(this, abc.class);
// here arg2 is argument of onitemclick method
// this will pick the same item from array list that is clicked on list view
i.putExtra("key_name" , Id.get(arg2));
i.putExtra("key_name" , Name.get(arg2));
i.putExtra("key_name" , Gender.get(arg2));

startActivity(i);


}




});

也能看到这个

http://www.ezzylearning.com/tutorial.aspx?tid=1351248

http://www.bogotobogo.com/Android/android6ListViewSpinnerGridViewGallery.php

关于android - 如何启动一个新的 Activity 表单ListView并给它多个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17395060/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com