在OnClickListener中获取数组位置

时间:2011-08-09 06:20:48

标签: java android arrays

我正在尝试使用bundle将mPath[]的递增值传递给我的新意图。但我不知道如何增加它。

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

3 个答案:

答案 0 :(得分:1)

如果你的腿筋/图像大小与mPath相同,那么你可以传递第三个参数值,即索引/位置。例如。

bundle.putString("selection", mPath[arg2]);

答案 1 :(得分:1)

为什么不利用listview中项目的位置并根据需要增加数组的值。

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

`

答案 2 :(得分:0)

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


                        }
                    });

在ShowFullSize活动中检索

Bundle bundle = getIntent().getExtras();
    String selection=bundle.getString("selection");