Listview到另一个有图像视图的活动?

时间:2017-08-15 19:29:19

标签: android listview

我在片段类中有一个listview。单击列表视图项时,将打开另一个活动。该活动的xml文件在滚动视图中有2个图像视图。图像随点击的每个列表视图项而变化。无论我点击哪个列表视图项目,它都会显示相同的图像视图。在我的情况下,它显示了R.drawable.geet94无论我点击哪个listview项目。

public class urdufrag1 extends Fragment {

    public urdufrag1() {
        // Required empty public constructor
    }

    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

    }

    private static final String TAG = "urdufrag1";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup 
    main_content, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.geetfrag_urdu, main_content, false);



         final int[] menuImage = {R.drawable.tgeet94, 
         R.drawable.tgeet95,R.drawable.tgeet96,R.drawable.tgeet97};
        final ListView listView = (ListView) view.findViewById(R.id.GeetListU);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
              Intent intent = new Intent(getActivity(), PackageG.class);
                //intent.putExtra("GeetType", 
        listView.getItemAtPosition(position).toString());
                //intent.putExtra("GeetType", Integer.toString(position));
                intent.putExtra("position", Integer.toString(position));
                startActivity(intent);

            }
        });
        AdapterGeetUrdu adapter = new AdapterGeetUrdu(getContext(), menuImage);
        listView.setAdapter(adapter);

        // Inflate the layout for this fragment



        return view;
    }
}

第二项活动

public class PackageG extends AppCompatActivity {

    ImageView img1, img2;
    PhotoViewAttacher mAttacher;



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

        img1 = (ImageView) findViewById(R.id.imageView8);
        img2 = (ImageView) findViewById(R.id.imageView9);
        mAttacher = new PhotoViewAttacher(img1);
        mAttacher = new PhotoViewAttacher(img2);

        Intent intent = this.getIntent();
        if(intent != null){
            Integer position = intent.getExtras().getInt("position");

            switch (position){

                case 0: img1.setImageResource(R.drawable.geet94);
                        img2.setImageResource(R.drawable.geet94_1);
                    break;
                case 1: img1.setImageResource(R.drawable.geet95);
                        img2.setImageResource(R.drawable.geet95_1);
                    break;
                case 2: img1.setImageResource(R.drawable.geet96);
                        img2.setImageResource(R.drawable.geet96_1);
                    break;

                default:
                    img1.setImageResource(R.drawable.carol);
                    img2.setImageResource(R.drawable.carol);
                    break;

            }

        }

        mAttacher.update();

    }
}

2 个答案:

答案 0 :(得分:0)

因为在你的活动中你期望一个整数但是你从listview传递字符串位置。根据以下代码更改单击侦听器

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
      Intent intent = new Intent(getActivity(), PackageG.class);    
      intent.putExtra("position", position);
      startActivity(intent);

    }
});

答案 1 :(得分:0)

  

intent.putExtra(&#34; position&#34;,Integer.toString(position));

这是你犯错的地方。

将此更改为,并将解决您的问题。

intent.putExtra("position", position);