如何使用putExtras在新的Activity中加载ListView中的图像?

时间:2017-04-17 20:22:34

标签: android image listview fullscreen

ListView中的图片很小,我希望在新Activity

中显示原始尺寸的点击图片

以下是我的代码,但如何使用putExtras获取Activity 2中的图片?

   listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View row, int position, long id) {

            ImageView imageview = (ImageView) row.findViewById(R.id.imageView1);

            int images = imageview.getId();
            Intent i = new Intent(getApplicationContext(),
            Activity_Full_Screen.class);
            i.putExtra("pos", position);
            startActivity(i);

        }

    });

活动2:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.Activity_Full_Screen);


    ImageView images = (ImageView) findViewById(R.id.imageview_msg_full_screen);

    Intent intent = getIntent();
    int pic =intent.getIntExtra("pos", 0);
    images.setImageResource(pic);


}

我收到此错误:

Unable to start activity ComponentInfo{xxxxxxx.Activity_Full_Screen}: android.content.res.Resources$NotFoundException: Resource ID #0x2

我的活动就像这张图片:Example

我尝试在Activity2中获取图像:

public class Activity_Full_Screen extends AppCompatActivity {
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_full_screen);


    ImageView images = (ImageView) findViewById(R.id.imageview_full_screen);

    Intent intent = getIntent();
    int pic =intent.getIntExtra("pos", 0);
    images.setImageResource(pic);


    }
    }

xml:

     <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity__full_screen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.testing.test1.Main">

<ImageView
    android:background="@drawable/img_empty"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageview_full_screen"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

如果我将getItem从id改为img,我可以轻松获得图像链接,但问题是我无法改变它,因为我也需要其他函数的id Solution 1 here but not OK

3 个答案:

答案 0 :(得分:0)

尝试使用

Intent中设置额外内容
Show_Full_Screen.putExtra("url", url);

然后使用

在新的Activity中进行检索
String url = getIntent().getStringExtra("url")

在getView中,您应该处理列表中的每个项目。

之类的东西
final Item item = listOfItems.get(position);
Picasso.with(Main.this).load(item.getUrl()).into(img);
img.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent Show_Full_Screen = new Intent(Main.this,   Activity_Full_Screen.class);
            Show_Full_Screen.putExtra("url", item.getUrl());
            startActivity(Show_Full_Screen);

        }
    });

答案 1 :(得分:0)

首先,创建dataType的ArrayList。为了您的目的,它可以命名为// Header: #include <string> #include <map> using std::string; using std::map; class Test { public: map< string, string > stuff; string & get_ref( const string key ); string get_value( const string key ) const; }; // Possible implementation 1: string Test::get_value( const string key ) const { return get_ref( key ); } string & Test::get_ref( const string key ) { return stuff.find( key )->second; } // Possible implementation 2 (obviously wrong, but here for the sake of pointing that out): string Test::get_value( const string key ) const { return stuff.find( key )->second; } string & Test::get_ref( const string key ) { return get_value( key ); } Data Data类包含的位置

List<Data> list = new ArrayList<>();

现在将您的值添加到private url imgUrl; private String textOne; private String textTwo; 。 数据清单

list

现在添加它们以列出它们。

String[] urlArray;
String[] stringArrayOne; 
String[] stringArrayTwo;

将适配器添加到for(int i=0; i<image_res.length(); i++){ Data.add(image_res[i], stringArrayOne[i], stringArrayTwo[i]); }

listView

现在将onClickListener添加到listView.setAdapter(objAdapter);

listView

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Data singleData = list.get(position); Intent intent = new Intent(getApplicationContext(), Activity_Full_Screen.class); intent.putExtra("url", singleData.singleData()); startActivity(intent); } }); 班级url获取getIntent().getStringExtra("url")值。

对于其他资源,您可以看到Here

答案 2 :(得分:0)

我尝试在Activity2中获取图像:

public class Activity_Full_Screen extends AppCompatActivity {
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_full_screen);


    ImageView images = (ImageView) findViewById(R.id.imageview_full_screen);

    Intent intent = getIntent();
    int pic =intent.getIntExtra("pos", 0);
    images.setImageResource(pic);


}

}

我现在已经解决了这个问题。我可以通过getView得到我需要的所有内容,之后我可以将字符串保存在类中或者在意图中发送它们:

@Override
    public View getView(final int position, View view, ViewGroup   viewGroup) {

 String img_link =ListItemForAdapter.get(position).img;
 String id =ListItemForAdapter.get(position).id;
// after that i can put the img-link in Activity2 into OnCreate, i get it from class or from intent.

由于