我目前正在创建一个应用程序,我想将数据列表从数据库加载到其中,然后在 GridView 中显示该数据。我已经创建了与数据库的连接,并成功撤回并填充了 GridView。我遇到的问题是,在我的列表行布局中,我希望能够 setLayoutParams 以便可以裁剪图像并且所有图像都具有相同的尺寸,这样就不会在其他设备尺寸上造成问题。
我拥有的文件如下:
list_row.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"
android:padding="15dp"
android:orientation="vertical">
<ImageView
android:id="@+id/list_image"
android:layout_width="110dp"
android:layout_height="110dp"
android:src="@drawable/ic_launcher" />
</LinearLayout>
ListAdapter.java
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
public class DressesAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
public DressesAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.list_row, null);
ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image
//thumb_image.setLayoutParams(new ImageView.LayoutParams(70, 70));
HashMap<String, String> dress = new HashMap<String, String>();
dress = data.get(position);
imageLoader.DisplayImage(dress.get(DressListActivity.TAG_IMAGE), thumb_image);
return vi;
}
}
ListActivity.java
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/grid_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:columnWidth="90dp"
android:horizontalSpacing="5dp"
android:verticalSpacing="5dp"
android:gravity="center"
android:stretchMode="columnWidth" >
</GridView>
运行时我得到的错误如下:
04-06 14:02:18.260: E/AndroidRuntime(12798): java.lang.ClassCastException: android.widget.AbsListView$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams
我希望有人可以帮助我纠正这个问题,因为它真的很烦人,在此先感谢:)