get array position in OnClickListener
I am trying to pass the the incremented value of mPath[]
to my new intent using 开发者_高级运维bundle. But I am not sure how to increment it.
for (int i=0; i<jsonArray.length(); i++)
{
String path_name = jsonArray.getJSONArray(i).getString(3);
Toast.makeText(MainActivity.this, path_name, Toast.LENGTH_LONG).show();
mPath[i] = "http://www.mywebsite.com/tattoo/" + path_name;
}
list=(ListView)findViewById(R.id.list);
adapter=new LazyAdapter(this, mStrings, mImages);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3){
Bundle bundle = new Bundle();
bundle.putString("selection", mPath[INCREMENT]);
Intent myIntent = new Intent(MainActivity.this, ShowFullSize.class);
myIntent.putExtras(bundle);
startActivityForResult(myIntent, 0);
}
});
Why don't you make use of the position of item from listview and increment the value of array as you want.
`list=(ListView)findViewById(R.id.list);
adapter=new LazyAdapter(this, mStrings, mImages);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3){
Bundle bundle = new Bundle();
//instead of INCREMENT here use arg2 which gives you the position of the list item clciked.
bundle.putString("selection", mPath[arg2]);
Intent myIntent = new Intent(MainActivity.this, ShowFullSize.class);
myIntent.putExtras(bundle);
startActivityForResult(myIntent, 0);
}
});
`
if your mstrings/mImages size was same as the mPath then you can pass the 3rd parameter value that is index/position. for e.g.
bundle.putString("selection", mPath[arg2]);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3){
Intent myIntent = new Intent(MainActivity.this, ShowFullSize.class);
INCREMENT=INCREMENT+1;
myIntent.putExtras("selection", mPath[INCREMENT]);
startActivityForResult(myIntent, 0);
}
});
To retrieve in ShowFullSize Activity
Bundle bundle = getIntent().getExtras();
String selection=bundle.getString("selection");
精彩评论