我有一些可绘制的图像并且有一个列表视图。我想单击listview中的图像并在另一个活动中完全打开该图像。我有完整的列表视图但无法显示完整图像。 activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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=".MainActivity" >
<ListView
android:id="@+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
</RelativeLayout>
list_single.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow>
<ImageView
android:id="@+id/img"
android:layout_width="50dp"
android:layout_height="50dp"/>
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="50dp" />
</TableRow>
</TableLayout>
CustomList.java
package learn2crack.customlistview;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomList extends ArrayAdapter<String>{
private final Activity context;
private final String[] web;
private final Integer[] imageId;
public CustomList(Activity context,
String[] web, Integer[] imageId) {
super(context, R.layout.list_single, web);
this.context = context;
this.web = web;
this.imageId = imageId;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_single, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
txtTitle.setText(web[position]);
imageView.setImageResource(imageId[position]);
return rowView;
}
}
MainActivity.java
package learn2crack.customlistview;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import android.app.Activity;
public class MainActivity extends Activity {
ListView list;
String[] web = {"Nature1", "Nature2","Nature3","Nature4","Nature5","Nature6","Nature7" } ;
Integer[] imageId = {R.drawable.image1,R.drawable.image2, R.drawable.image3,R.drawable.image4,R.drawable.image5,R.drawable.image6, R.drawable.image7 };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomList adapter = new CustomList(MainActivity.this, web, imageId);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this, "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();
// I am using this way to show image in another activity
Intent intent = new Intent(MainActivity.this, FullImage.class);
startActivity(intent);
}
});
}
}
full_image.xml
<?xml version =" 1.0 "encoding="utf-8"?>
<LinearLayout
Xmlns:android = " http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android : layout_height="match_parent"
<ImageView
android:id="@+id/fullImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<LinearLayout/>
FullImage.java
package learn2crack.customlistview;
import android.app.Activity;
import android.os.Bundle;
public class FullImage extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.full_image);
}
}
请建议我为什么我无法在这个全图像课程中展示图像。
答案 0 :(得分:1)
试试这个.. 在MainActivity- onItemClick
中添加此行
Intent intent = new Intent(MainActivity.this, FullImage.class);
intent.putExtra("image",imageId[position]);
startActivity(intent);
并在fullImage类中在setContentView之后添加这些行。
ImageView imageView = (ImageView)findViewById(R.id.fullImage);
int imageId = getIntent().getIntExtra("image",0);
imageView.setImageResource(imageId);
答案 1 :(得分:0)
您可以发送通过您正在使用的意图点击的图像名称,以启动您可以拍摄的第二个活动,并在第二个活动的imageView上设置适当的图像。
通过意图发送数据,您可以使用
intent.putExtra(name,value)
在第二个活动中,您可以使用intent.getStringExtra(name)
从第一个活动中获取该名称,然后在imageview上设置相应的图像。
答案 2 :(得分:0)
我有一个想法 - 无论你选择什么,你都需要将图像名称表单activity1传递给activity2。您可以使用puteExtra执行此资源对象,然后您可以以键值对的形式获取字符串或任何其他内容。您遇到此问题是因为您的imageview引用未传递给其他活动