如何启动新的活动表单ListView并为其赋予多个参数 [英] How to start a new activity form ListView and give it multiple parameters

查看:66
本文介绍了如何启动新的活动表单ListView并为其赋予多个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的代码,该代码在listview中显示数据,该数据是从json解析的.
当用户单击列表中的任何项目时,我都想开始新的活动.

Below is my code which displays data in listview which is parses from json.
I want to start new activity when the user clicks on any item in the list.

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

I followed this url http://www.androidhive.info/2012/01/android-json-parsing-tutorial/ and this is the json file http://api.androidhive.info/contacts/

当用户单击列表视图中的任何项目并将剩余的json值作为参数传递时,如何启动新活动?

How to start a new activity when a user clicks on any item in listview and pass the remaining json values as parameters?

现在,我的列表视图仅显示姓名,但我想传递其余项目,例如电子邮件,性别和移动到其他活动.

                           "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

这篇关于如何启动新的活动表单ListView并为其赋予多个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆