Listview无法正确显示名称

时间:2018-01-21 01:16:59

标签: android listview android-studio

我的应用程序需要有多个ListView,当单击一个选项时,将打开第二个显示可缩放图片的活动。我的代码中的所有内容都会编译,打开正确的图片并进行相应的缩放。但是,我列表中的“名称”没有正确显示,所有人都说同样的事情:“com.example.poweruser.fcemsandroid.obstetric”。任何人都有解决方案来纠正这个?我的代码如下:

    final Context context = getApplicationContext();

    //list contains image name, image location
    final List<imagedisplay> myImageList = new ArrayList<imagedisplay>();
    myImageList.add(new imagedisplay("Aspirin", R.drawable.aspirin));
    myImageList.add(new imagedisplay("Drug2", R.drawable.ekghomepic));
    myImageList.add(new imagedisplay("Drug3", R.drawable.aspirin));
    myImageList.add(new imagedisplay("Drug4", R.drawable.ekghomepic));

    //define ListView and create onItemClick Listener
    listview = (ListView) findViewById(R.id.obstetriclistview);

    ArrayAdapter<imagedisplay> arrayAdapters = new ArrayAdapter<imagedisplay>(this, R.layout.support_simple_spinner_dropdown_item, myImageList);
    listview.setAdapter(arrayAdapters);
    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            Intent myIntent = new Intent(context, imagedisplay.class);

            myIntent.putExtra("name", myImageList.get(position).getName());
            myIntent.putExtra("imagePath", myImageList.get(position).getPath());
            startActivityForResult(myIntent, 0);


           }
        });
    }}

public class ImageDisplay {
    private String Name;
    private int Path; // use String if you use a path, in here i'm storing image in drawable
public ImageDisplay(){}
public ImageDisplay(String name, int path){
    this.Name = name;
    this.Path = path;
}

public String getName() {
    return Name;
}

public void setName(String name) {
    Name = name;
}

public int getPath() {
    return Path;
}

public void setPath(int path) {
    this.Path = path;
}

}

第二个活动代码如下:

public class imagedisplay extends AppCompatActivity {

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

    ImageView imageView = (ImageView) findViewById(R.id.imageView3);
    Intent myItent = getIntent();

    String Name = myItent.getStringExtra("name"); //Do whatever you need with image title
    int Path = myItent.getIntExtra("imagePath", 0); //pass this to ImageView

    imageView.setImageResource(Path);

    TouchImageView img = new TouchImageView(this);
    img.setImageResource(Path);
    img.setMaxZoom(4f);
    setContentView(img);

}

}

0 个答案:

没有答案