0

我需要帮助将数据和位置从 OnClickListener 传递到新活动。下面是我的尝试。我似乎无法弄清楚。请协助。谢谢

主要的

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();

适配器类

    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;
    }
}

单项视图

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);
        }
4

2 回答 2

1

我无法弄清楚这部分intent.putExtra(MainActivity.RANK);它需要 2 个字符串并且还传递了位置

putExtra()接受两个参数,因为您必须传入一个值和一个名称。

您可以这样做来传递数据:

intent.putExtra("Name", value);

编辑:

你也是如何通过这个职位的?

什么职位?您的适配器仅为每个列表行提供视图。如果您想获取项目在 ListView(或其他)中的位置,则必须将 an 设置onItemClickListener为 ListView。

于 2013-05-05T17:56:17.223 回答
0

can you do this in onClick:

 intent.putExtra(MainActivity.RANK,rank.getText());
于 2013-05-05T17:58:16.707 回答