0
我正在使用GridView示例 我在gridview上有圖像的數量,如果我點擊一個項目,它應該打開它或顯示在我的另一個活動(DetailActivity.class)與該圖像和圖像名稱。我嘗試過,但沒有工作。
下面是我的代碼不工作,你有什麼想法是什麼錯?具有圖像和圖像標題的GridView示例
1.grid_item_layout.xml
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/grid_image"
android:scaleType="centerCrop"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/grid_image_text"
android:layout_marginTop="5dp"
android:gravity="center"
android:maxLines="2"
android:ellipsize="marquee"
android:textSize="12sp"/>
2.MainActivity類
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ImageItem imageItem = (ImageItem)parent.getItemAtPosition(position);
Intent intent = new Intent(MainActivity.this, DetailsActivity.class);
intent.putExtra("value1",imageItem.getTitle());
intent.putExtra("value2", imageItem.getImage());
startActivity(intent);
}
});
3公共類DetailsActivity延伸活動{
private ImageView imageView;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details_view);
textView = (TextView)findViewById(R.id.text);
Bundle b = getIntent().getExtras();
String val1 = b.getString("value1");
textView.setText(val1);
// I also try below code but not working..!
String title = getIntent().getStringExtra("value1");
Bitmap b = getIntent().getParcelableExtra("value2");
textView = (TextView)findViewById(R.id.text);
textView.setText(title);
}
}
4 activity_details_view.xml
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/image"
android:layout_gravity="center"
android:scaleType="fitCenter"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text"
android:text="hello"
android:textSize="40dp"/>
你是什麼意思*不工作* ?.你有什麼錯誤嗎? – Rohit5k2
是的,錯誤是不幸的應用程序還沒有停止/或有一段時間我要去下一個活動,但它沒有任何結果,我想要做的! –
請發佈你的錯誤。 – Rohit5k2