简体   繁体   English

Android OnClickListener进行新活动

[英]Android OnClickListener to new activity

I need help passing data and position from OnClickListener to a new activity. 我需要将数据和位置从OnClickListener传递到新活动的帮助。 Below are my attempt. 以下是我的尝试。 I cant seem to figure it out. 我似乎无法弄清楚。 Please assist. 请协助。 Thanks 谢谢

Main 主要

public class MainActivity extends Activity {

    JSONObject json;
    JSONArray jsonarray;
    ListView listview;
    ProgressDialog pDialog;
    ListViewAdapter adapter;
    ArrayList<HashMap<String, String>> arraylist;
    static String ID = "id";
    static String RANK = "rank";
    static String COUNTRY = "country";
    static String POPULATION = "population";
    static String FLAG = "flag";

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

        new DownloadJSON().execute();
    }

    // DownloadJSON AsyncTask
    private class DownloadJSON extends AsyncTask<Void, Void, Void> {

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

            pDialog = new ProgressDialog(MainActivity.this);

            pDialog.setMessage("Loading...");
            pDialog.setIndeterminate(false);

            pDialog.show();
        }

        @Override
        protected Void doInBackground(Void... params) {

            arraylist = new ArrayList<HashMap<String, String>>();

            json = JSONfunctions
                    .getJSONfromURL("http://www.site.com");

            try {

                jsonarray = json.getJSONArray("worldpopulation");

                for (int i = 0; i < jsonarray.length(); i++) {
                    HashMap<String, String> map = new HashMap<String, String>();
                    json = jsonarray.getJSONObject(i);
                    map.put("id", String.valueOf(i));
                    map.put("rank", json.getString("rank"));
                    map.put("country", json.getString("country"));
                    map.put("population", json.getString("population"));
                    map.put("flag", json.getString("flag"));
                    arraylist.add(map);
                }
            } catch (JSONException e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void args) {
            listview = (ListView) findViewById(R.id.listview);
            adapter = new ListViewAdapter(MainActivity.this, arraylist);
            listview.setAdapter(adapter);
            //setListAdapter(adapter);
            pDialog.dismiss();

Adapter Class 转接器类别

    public class ListViewAdapter extends BaseAdapter {

    // Declare Variables
    Context context;
    LayoutInflater inflater;
    ArrayList<HashMap<String, String>> data;
    DownloadImageTask mTask;
    ImageLoader imageLoader;

    public ListViewAdapter(Context context,
            ArrayList<HashMap<String, String>> arraylistview) {
        this.context = context;
        data = arraylistview;
        imageLoader = new ImageLoader(context);

    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        TextView rank;
        TextView country;
        TextView population;
        ImageView flag;

        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View itemView = inflater.inflate(R.layout.listview_item, parent, false);

        HashMap<String, String> result = new HashMap<String, String>();
        result = data.get(position);
        rank = (TextView) itemView.findViewById(R.id.rank); // title
        country = (TextView) itemView.findViewById(R.id.country); // title
        population = (TextView) itemView.findViewById(R.id.population); // artist
        flag = (ImageView) itemView.findViewById(R.id.flag); // artist

        rank.setText(result.get(MainActivity.RANK));
        country.setText(result.get(MainActivity.COUNTRY));
        population.setText(result.get(MainActivity.POPULATION));
        imageLoader.DisplayImage(result.get(MainActivity.FLAG), flag);

        itemView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(context, SingleItemView.class);
                intent.putExtra(MainActivity.RANK);
                intent.putExtra(COUNTRY, country);
                intent.putExtra(POPULATION, population);
                intent.putExtra(FLAG, flag);
                context.startActivity(intent);

            }
        });

        return itemView;
    }
}

SingleItemView SingleItemView

public class SingleItemView extends Activity {


    // Declare Variables
        TextView txtrank;
        TextView txtcountry;
        TextView txtpopulation;
        ImageView imgflag;
        String rank;
        String country;
        String population;
        String flag;
        int position;

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

            Intent i = getIntent();

            position = i.getExtras().getInt("position");

            rank = i.getStringExtra("rank");

            country = i.getStringExtra("country");

            population = i.getStringExtra("population");

            flag = i.getStringExtra("flag");


            txtrank = (TextView) findViewById(R.id.rank);
            txtcountry = (TextView) findViewById(R.id.country);
            txtpopulation = (TextView) findViewById(R.id.population);


            txtrank.setText(rank);
            txtcountry.setText(country);
            txtpopulation.setText(flag);

            //imgflag.setImageResource(flag);
        }

I cant figure out this part intent.putExtra(MainActivity.RANK); 我无法弄清楚这部分intent.putExtra(MainActivity.RANK); it needs 2 strings and also passing the position 它需要2个字符串,并且还要传递位置

putExtra() takes two arguments, because you have to pass in a value and a name. putExtra()有两个参数,因为您必须传递一个值和一个名称。

You can do this to pass data: 您可以这样做来传递数据:

intent.putExtra("Name", value);

Edit: 编辑:

How do you pass the position too? 您也如何通过该职位?

What position? 什么职位? Your adapter only provides the View for each list row. 您的适配器仅为每个列表行提供“视图”。 If you want to get the position of the item in a ListView (or whatever) you have to set an onItemClickListener to your ListView. 如果要获取项目在ListView(或其他内容)中的位置,则必须将onItemClickListener设置为ListView。

您可以在onClick中执行以下操作:

 intent.putExtra(MainActivity.RANK,rank.getText());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM