如何在下一个活动中显示文本和图像

时间:2018-08-01 08:14:49

标签: android android-layout android-studio android-fragments gridview

我创建了一个像gallery这样的android应用程序,其中包含一些使用gridview片段的图像。

功能正常,图像也显示在片段gridview和Book活动中,但文本未显示(未设置文本)

这是我的片段代码:

GridView grid;
    String[] web = {
            "Google",
            "Github",
            "Instagram",
            "Facebook",
            "Flickr",
            "Pinterest",
            "Quora",
            "Twitter",
            "Vimeo",
            "WordPress",
            "Youtube",
            "Stumbleupon",
            "SoundCloud",
            "Reddit",
            "Blogger"

    } ;
    int[] imageId = {
            R.drawable.person7,
            R.drawable.person1,
            R.drawable.person2,
            R.drawable.person7,
            R.drawable.person7,
            R.drawable.person7,
            R.drawable.person7,
            R.drawable.person7,
            R.drawable.person7,
            R.drawable.person7,
            R.drawable.person7,
            R.drawable.person7,
            R.drawable.person7,
            R.drawable.person7,
            R.drawable.person7

    };
    int[] fullimg = {
            R.drawable.person7,
            R.drawable.person1,
            R.drawable.person7,
            R.drawable.person7,
            R.drawable.person7,
            R.drawable.person7,
            R.drawable.person7,
            R.drawable.person7,
            R.drawable.person7,
            R.drawable.person7,
            R.drawable.person7,
            R.drawable.person7,
            R.drawable.person7,
            R.drawable.person7,
            R.drawable.person7

    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        setHasOptionsMenu(true);

        View view = inflater.inflate(R.layout.fragment_status,container,false);

        CustomGrid adapter = new CustomGrid(getActivity(), web, imageId, fullimg);

        //CustomGrid adapter = new CustomGrid(MainActivity.this, web, imageId);

        grid=(GridView) view.findViewById(R.id.grid);
        grid.setAdapter(adapter);
        grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {


                // Toast.makeText(MainActivity.this, "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();



                String value=(String)grid.getItemAtPosition(i);
                Intent intent=new Intent(getActivity(),Book.class);
                intent.putExtra("web", web[i]);
                intent.putExtra("imageId", imageId[i]);
                intent.putExtra("fullimg", fullimg[i]);
                startActivity(intent);

            }
        });

        //return inflater.inflate(R.layout.fragment_status, container, false);
        return view;
    }

这是我的图书活动代码,其中要显示文字和图像 图像显示正常,但我不知道如何显示文本:

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


        TextView textView = (TextView) findViewById(R.id.txttitle);
        ImageView imageView = (ImageView)findViewById(R.id.imageView2);


        //textView.setText(txttitle);
        //textView.setText(web[i]);
        imageView.setImageResource(getIntent().getIntExtra("fullimg", R.drawable.person7));


    }

5 个答案:

答案 0 :(得分:0)

//textView.setText(txttitle);
//textView.setText(web[i]);
imageView.setImageResource(getIntent().getIntExtra("fullimg", R.drawable.person7));

您不会像对图像那样检索传递的文本;)

答案 1 :(得分:0)

只需从Intent中获取文本即可。我正在考虑web包含您的文本,然后下面的代码将为您获取文本

textView.setText(getIntent().getStringExtra("web"));

答案 2 :(得分:0)

设置textview只是被注释掉。只需将TextView设置为与imageview类似:

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


    TextView textView = (TextView) findViewById(R.id.txttitle);
    ImageView imageView = (ImageView)findViewById(R.id.imageView2);


    textView.setText(getIntent().getStringExtra("web"))
    imageView.setImageResource(getIntent().getIntExtra("fullimg", R.drawable.person7));


}

答案 3 :(得分:0)

使用Application类存储数据并在应用程序中的任何位置使用,而不是通过意图将变量传递给下一个活动。

答案 4 :(得分:0)

在传递文本值时输入要输入的常量 textView.setText(getIntent()。getStringExtra(“ YourKeyValue”));