我是新来的,我对我的 Android 项目完全感到困惑。我做了一些研究,但我仍然没有答案。我的问题是:当我单击 GridView 中的项目时,它只显示黑屏。当我更改某些内容时,现在它在 Eclipse 中显示一个错误。我不知道我的错误在哪里。
我使用此答案Gridview 与两列和自动调整大小的图像,使我的 gridview 很好,适合多个屏幕。
我从这个中使用了一些东西:我希望 gridView 上的图像应该在 full View 中显示。
我的代码:FirstActivity:
public class FirstActivity extends SherlockActivity {
ActionBar actionbar;
List<Item> items = new ArrayList<Item>();
@Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.Theme_Sherlock);
super.onCreate(savedInstanceState);
setContentView(R.layout.first);
actionbar = getSupportActionBar();
items.add(new Item("Name1", "Text1", R.drawable.pic_12));
items.add(new Item("Name2", "Text2", R.drawable.pic_13));
items.add(new Item("Name3", "Text3", R.drawable.pic_14));
items.add(new Item("Name4", "Text4", R.drawable.pic_15));
items.add(new Item("Name5", "Text5", R.drawable.pic_2));
GridView gridView = (GridView) findViewById(R.id.gridview);
gridView.setAdapter(new ImageAdapter(this, items));
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// Sending image id to SecondActivity
Intent i = new Intent(getApplicationContext(), SecondActivity.class);
// passing index (position of clicked item)
i.putExtra("id", position);
startActivity(i);
}
});
}
我的 ImageAdapter 类:
public class ImageAdapter extends BaseAdapter {
private List<Item> items = new ArrayList<Item>();
private LayoutInflater inflater;
public ImageAdapter(Context context, List<Item> items) {
inflater = LayoutInflater.from(context);
this.items = items;
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int i) {
return items.get(i);
}
@Override
public long getItemId(int i) {
return items.get(i).drawable;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View v = view;
ImageView picture;
TextView name;
if (v == null) {
v = inflater.inflate(R.layout.squareimageview, viewGroup, false);
v.setTag(R.id.picture, v.findViewById(R.id.picture));
v.setTag(R.id.text, v.findViewById(R.id.text));
}
picture = (ImageView) v.getTag(R.id.picture);
name = (TextView) v.getTag(R.id.text);
Item item = (Item) items.get(i);
picture.setImageResource(item.getDrawable());
name.setText(item.name);
return v;
}
}
第二活动:
public class SecondActivity extends Activity {
List<Item> items = new ArrayList<Item>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
// get intent data
Intent i = getIntent();
// Selected image id
int position = i.getExtras().getInt("id");
TextView name = (TextView) findViewById(R.id.text_small);
ImageView imageView = (ImageView) findViewById(R.id.picture);
TextView text = (TextView) findViewById(R.id.text_big);
Item item = (Item) items.get(position);
//***************HERE ARE MY ERRORS***************************************
name.setText(items.name);
text.setText(items.text);
imageView.setImageResource(items.drawableId);
//**********************************************************************
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.second, menu);
return true;
}
}
物品类别:
public class Item {
String name;
int drawable;
String text;
public int getDrawable() {
return drawable;
}
public void setDrawable(int drawable) {
this.drawable = drawable;
}
public Item(String name, String text, int id) {
this.name = name;
this.text = text;
this.drawable = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
我的布局:首先:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<GridView
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:horizontalSpacing="10dp"
android:numColumns="2"
android:stretchMode="columnWidth"
android:verticalSpacing="0dp" />
</FrameLayout>
第二:
<?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"
android:orientation="vertical" >
<ImageView
android:id="@+id/picture"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop" />
<TextView
android:id="@+id/text_small"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text_big"
android:layout_width="wrap_content"
android:layout_height="fill_parent" />
</LinearLayout>
方形图像视图:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.zva.app.SquareImageView
android:id="@+id/picture"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop" />
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#55000000"
android:paddingBottom="10dp"
android:paddingLeft="7dp"
android:paddingRight="7dp"
android:paddingTop="10dp"
android:textColor="@android:color/white" />
</FrameLayout>