我调用我的布局并为其设置项目,但getview()不返回任何内容。
我的主要课程:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.MymainView);
grid = (GridView) findViewById(R.id.grid);
if (getScreenOrientation() == 1)
grid.setNumColumns(3);
else
grid.setNumColumns(4);
items = new ArrayList<LauncherGridItem>();
Intent intent = new Intent(this, Activity2.class);
intent.putExtra("Param1", "Child Activity");
items.add(new LauncherGridItem("ic_launcher", "Item 1",
intent));
adapter = new LauncherGridAdapter(this, items, grid, dragLayer,
deleteZone);
adapter.setPersistenceToken("MyLauncherGrid");
adapter.setDragListener(this);
adapter.setEditable(true);
grid.setAdapter(adapter);
我的LauncherGridAdapter,getview()部分在文件中:
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater
.inflate(R.layout.grid_view_item_layout, null);
holder = new ViewHolder();
holder.image = (ImageView) convertView
.findViewById(R.id.selection_item_image);
holder.text = (TextView) convertView
.findViewById(R.id.selection_item_text);
convertView.setTag(holder);
//holder.image.setPadding(8, 8, 8, 8);
} else {
holder = (ViewHolder) convertView.getTag();
}
DraggableRelativeLayout layout = (DraggableRelativeLayout) convertView;
convertView.setOnLongClickListener(this);
convertView.setOnClickListener(this);
LauncherGridItem item = getItems().get(position);
layout.setItem(item);
layout.setDragListener(this);
// holder.image.setOnLongClickListener(this);
Drawable d = holder.image.getDrawable();
// if (d != null) {
d.setCallback(null);
// }
holder.image.setImageDrawable(null);
if (!item.getUrl().startsWith("http")) {
try {
holder.image.setImageDrawable(context.getResources()
.getDrawable(
context.getResources().getIdentifier(
context.getApplicationContext()
.getPackageName()
+ ":drawable/" + item.getUrl(),
null, null)));
} catch (NotFoundException e) {
holder.image.setImageResource(android.R.drawable.ic_menu_view);
}
} else {
// Image cached = images.get(position);
if(item.getImage() == null)
{
holder.image.setImageResource(R.drawable.avatar);
} else {
holder.image.setScaleType(ScaleType.CENTER_INSIDE);
holder.image.setImageBitmap(item.getImage());
}
}
}
holder.text.setText(item.getCaption());
layout.setImage(holder.image);
layout.setText(holder.text);
layout.setItem(item);
layout.canDelete(item.canDelete());
return convertView;
}
我甚至检查了if语句和程序项是否有效而不是null。 香港专业教育学院也添加了这些但在gridview中仍然没有:
@Override
public int getCount() {
return getItems().size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
我猜问题是“grid_view_item_layout”根本没有显示
这是“主要”布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical" android:background="#CCCCCC">
<com.myapp.ui.drag.DragLayer
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:launcher="http://schemas.android.com/apk/res/com.android.launcher"
android:id="@+id/drag_layer" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_above="@+id/handle">
<GridView
android:id="@+id/grid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="65dip"
android:background="@color/BlanchedAlmond"
android:numColumns="3"
android:scrollbars="vertical" />
<com.myapp.ui.drag.DeleteZone
android:id="@+id/delete_zone_view" android:layout_width="fill_parent"
android:layout_height="60dp" android:layout_alignParentBottom="true"
android:layout_centerInParent="true" android:background="@drawable/delete_zone_bg"
android:visibility="gone">
<ImageView android:layout_width="60dp"
android:layout_height="60dp" android:layout_centerInParent="true"
android:src="@drawable/trashcan" />
</com.myapp.ui.drag.DeleteZone>
</com.myapp.ui.drag.DragLayer>
</LinearLayout>
这是grid_view_item_layout布局:
<?xml version="1.0" encoding="utf-8"?>
<com.myapp.ui.drag.DraggableRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/CadetBlue"
android:orientation="vertical" >
<ImageView android:id="@+id/selection_item_image" android:scaleType="centerInside"
android:src="@drawable/avatar" android:layout_width="wrap_content"
android:layout_height="60dip" android:layout_centerHorizontal="true" />
<TextView android:layout_width="wrap_content" android:id="@+id/selection_item_text"
android:maxLines="1" android:lines="1" android:scrollHorizontally="true" android:ellipsize="end"
android:layout_height="wrap_content" android:textColor="@android:color/black"
android:text="(Selection Text)" android:layout_below="@+id/selection_item_image" android:layout_centerHorizontal="true"
></TextView>
</com.myapp.ui.drag.DraggableRelativeLayout>
我使用plusgridlayout在gridview之间移动项目。该函数适用于源项目,但是当我尝试在我的下发布它时,没有错误发生,加上没有gridview。
如果我从return convertView更改getview()的最后一行;返回布局;仍然没有任何影响。
答案 0 :(得分:4)
您必须覆盖getCount()
方法,并将列表大小的返回值更改为以下代码:
@Override
public int getCount() {
log.d("result of count is",""+items.size())
return items.size();
}
答案 1 :(得分:0)
默认情况下,您必须在适配器中覆盖4个方法:getCount, getItem, getItemId and getView.