Reputation: 1617
I need help passing data and position from OnClickListener to a new activity. 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
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);
}
Upvotes: 0
Views: 869
Reputation: 72543
I cant figure out this part
intent.putExtra(MainActivity.RANK);
it needs 2 strings and also passing the position
putExtra()
takes two arguments, because you have to pass in a value and a name.
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.
Upvotes: 1
Reputation: 62519
can you do this in onClick:
intent.putExtra(MainActivity.RANK,rank.getText());
Upvotes: 0