【问题标题】:How can show text and image in next activity如何在下一个活动中显示文本和图像
【发布时间】:2018-08-01 08:14:48
【问题描述】:

我已经创建了类似画廊的 android 应用程序,其中有一些使用 gridview 片段的图像。

功能工作正常,图像也显示在片段网格视图和图书活动中,但未显示文本(未设置文本)

这是我的片段代码:

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


    }

【问题讨论】:

  • 所以要显示文字吗?
  • 是的,我想在图书活动中同时显示文本和图像
  • 也许我不明白这个问题,但你不能像拿 imageId 一样拿字符串吗? textView .setText(getIntent().getStringExtra("web"))
  • 我正在对您的代码进行一些更改,我给您更新的答案
  • 好的,我等你的更新

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


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:
    //textView.setText(txttitle);
    //textView.setText(web[i]);
    imageView.setImageResource(getIntent().getIntExtra("fullimg", R.drawable.person7));
    

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        不是通过 Intent 将变量传递给下一个 Activity,而是使用 Application 类来存储数据并在应用中的任何位置使用。

        【讨论】:

        • 你能告诉我应用程序类的代码来存储数据吗
        • 公共类 ApplicationLoader 扩展应用程序 { @Override public void onCreate() { super.onCreate(); } 私有 int 数据=0; public int getCompanyModel() { 返回数据; } public void setCompanyModel(int data) { this.data = data; } } \n 在 Manifest 中, \n设置并获取变量 \n ApplicationLoader applicationLoader = (ApplicationLoader) getApplicationContext(); applicationLoader.setCompanyModel(); applicationLoader.getUpdatesModel();
        【解决方案5】:

        在传递文本值时输入您正在输入的常量 textView.setText(getIntent().getStringExtra("YourKeyValue"));

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多