2013-05-05 31 views
0

我需要幫助將數據和位置從OnClickListener傳遞到新的活動。以下是我的嘗試。我似乎無法弄清楚。請協助。感謝Android的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; 
    } 
} 

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); 
     } 
+0

出了什麼問題? – 2013-05-05 17:48:29

+0

我還沒有看到任何問題,請考慮發佈SingleItemView.class – j2emanue 2013-05-05 17:49:45

+0

我無法弄清楚這部分intent.putExtra(MainActivity.RANK);它需要2個字符串,並通過位置 – 2013-05-05 17:49:48

回答

1

我不能弄清楚該部分intent.putExtra(MainActivity.RANK);它 需要2個串,並且還使所述位置

putExtra()需要兩個參數,因爲您必須傳遞一個值和一個名稱。

你可以做到這一點來傳遞數據:

intent.putExtra("Name", value); 

編輯:

如何傳遞的位置嗎?

什麼位置?您的適配器僅爲每個列表行提供View。如果你想在ListView中獲得物品的位置(或其他),你必須設置一個onItemClickListener到你的ListView。

+0

你怎麼也通過這個職位? – 2013-05-05 17:57:52

+1

感謝您的幫助 – 2013-05-05 18:03:27

+0

不客氣。 – Ahmad 2013-05-05 18:03:57

0

可以在你的onClick做到這一點:

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