我使用Horizontal Listview在适合的屏幕上显示四个图像,这样这些图像将从数据库中获取。
我的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" >
<com.test.hlistview.HorizListview
android:id="@+id/hlvSimpleList"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
MainActivity类如下
public class MainActivity extends Activity {
HorizListview hlist;
private int[] imageid = new int[] { R.drawable.lion, R.drawable.love,
R.drawable.android, R.drawable.handshake };
private String[] imagename = new String[] { "Lion", "Love", "Android",
"Handshake" };
List<Details> additems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hlist = (HorizListview) findViewById(R.id.hlvSimpleList);
additems = new ArrayList<Details>();
Log.i("Length of array", "size is" + imagename.length);
int length = imagename.length;
for (int i = 0; i < length; i++) {
Details det = new Details(imageid[i], imagename[i]);
additems.add(det);
}
CAdapter c = new CAdapter(this, additems);
hlist.setAdapter(c);
}
}
我在tablelayout中有imageview和Textview,xml如下所示,我将其命名为photo.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"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@android:color/black"
android:gravity="center_vertical" >
<TableRow
android:id="@+id/row1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/layout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="@+id/image1"
android:layout_width="150dp"
android:layout_height="150dp"
android:background="@drawable/mymilan_circle_white" />
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="@+id/layout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:orientation="vertical" >
<ImageView
android:id="@+id/image2"
android:layout_width="150dp"
android:layout_height="150dp"
android:background="@drawable/mymilan_circle_white" />
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</TableRow>
<TableRow
android:id="@+id/row2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/layout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="@+id/image3"
android:layout_width="150dp"
android:layout_height="150dp"
android:background="@drawable/mymilan_circle_white" />
<TextView
android:id="@+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="@+id/layout4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@id/layout2"
android:orientation="vertical" >
<ImageView
android:id="@+id/image4"
android:layout_width="150dp"
android:layout_height="150dp"
android:background="@drawable/mymilan_circle_white" />
<TextView
android:id="@+id/tv4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</TableRow>
</TableLayout>
现在我的自定义Adapter类如下。
public class CAdapter extends ArrayAdapter<Details> {
private LayoutInflater mInflater;
private List<Details> item;
private Context con;
private int newpostion;
public CAdapter(Context context, List<Details> objects) {
super(context, R.layout.photo, objects);
this.con = context;
this.item = objects;
mInflater = (LayoutInflater) getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return item.size()/4;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder;
newpostion = 0 * position;
newpostion = 0 * position+1;
newpostion = 0 * position+2;
newpostion = 0 * position+3;
if (convertView == null) {
// Inflate the view since it does not exist
convertView = mInflater.inflate(R.layout.photo, parent, false);
// Create and save off the holder in the tag so we get quick access to inner fields
// This must be done for performance reasons
holder = new Holder();
Log.i("VIEW HOLDER", "Position is" + newpostion);
holder.trow = (TableRow) convertView.findViewById(R.id.row1);
holder.trow2 = (TableRow) convertView.findViewById(R.id.row2);
holder.image = (ImageView) convertView
.findViewById(R.id.image1);
holder.textview = (TextView) convertView.findViewById(R.id.tv1);
holder.thirdimage = (ImageView)convertView.findViewById(R.id.image3);
holder.ttextview = (TextView) convertView.findViewById(R.id.tv3);
holder.secondimage = (ImageView) convertView
.findViewById(R.id.image2);
holder.stextview = (TextView) convertView.findViewById(R.id.tv2);
holder.fourthimage = (ImageView) convertView.findViewById(R.id.image4);
holder.ftextview = (TextView) convertView.findViewById(R.id.tv4);
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
Details det = (Details) getItem(newpostion);
for (int i = 0; i < item.size(); i++) {
if(i ==0){
holder.textview.setText(det.getImgname());
holder.image.setBackgroundResource(det.getImgid());
}else if(i ==1){
holder.stextview.setText(det.getImgname());
holder.secondimage.setBackgroundResource(det.getImgid());
}else if(i ==2){
holder.thirdimage.setBackgroundResource(det.getImgid());
holder.ttextview.setText(det.getImgname());
}else if(i==3){
holder.fourthimage.setBackgroundResource(det.getImgid());
holder.ftextview.setText(det.getImgname());
}
}
return convertView;
}
/** View holder for the views we need access to */
private static class Holder {
public TextView textview;
public TextView stextview;
public TextView ttextview;
public TextView ftextview;
public TableRow trow;
public TableRow trow2;
public ImageView image;
public ImageView secondimage;
public ImageView thirdimage;
public ImageView fourthimage;
}
}
但我的输出是这样的..
我想在那边有四张不同的图片。 1.狮子,2.Love,3.Android和4.握手。但是我在四个位置只得到握手。我是android的新手。请在这方面帮助我,非常感谢。
答案 0 :(得分:0)
我会立即尝试解决我注意到的问题:
1)处理变量newpostion
此变量在getView方法中应该是本地的,不能在其他任何地方使用
getView开头的赋值没有意义
newpostion = 0 * position;
newpostion = 0 * position+1;
newpostion = 0 * position+2;
newpostion = 0 * position+3;
你不断覆盖价值观,只有最后一行有任何影响。
2)当您尝试为视图设置值时,您只能在循环外获取数据对象:
Details det = (Details) getItem(newpostion);
然后为所有四组视图分配相同的值。相反,你应该在for循环中找到正确的Details对象并使用它(你应该检查你的项目数不能被4整除的情况)
3)在同一个循环中,您循环遍历所有项目,而实际上您只想使用最多4个对象
4)当项目数不能被4整除时,getCount()方法将返回不正确的结果 - 6/4将返回1
答案 1 :(得分:0)
public class CAdapter extends ArrayAdapter<Details> {
private LayoutInflater mInflater;
private List<Details> item;
private Context con;
public CAdapter(Context context, List<Details> objects) {
super(context, R.layout.photo, objects);
this.con = context;
this.item = objects;
mInflater = (LayoutInflater) getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
if(item.size()%4 == 0){
return item.size()/4;
}else
return (item.size()/4)+1;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder;
/*newpostion = 0 * position;
newpostion = 0 * position+1;
newpostion = 0 * position+2;
newpostion = 0 * position+3;*/
if (convertView == null) {
// Inflate the view since it does not exist
convertView = mInflater.inflate(R.layout.photo, parent, false);
// Create and save off the holder in the tag so we get quick access to inner fields
// This must be done for performance reasons
holder = new Holder();
Log.i("VIEW HOLDER", "Position is" + position);
holder.lay1 = (LinearLayout) convertView.findViewById(R.id.layout1);
holder.lay2 = (LinearLayout) convertView.findViewById(R.id.layout2);
holder.lay3 = (LinearLayout) convertView.findViewById(R.id.layout3);
holder.lay4 = (LinearLayout) convertView.findViewById(R.id.layout4);
holder.image = (ImageView) convertView
.findViewById(R.id.image1);
holder.textview = (TextView) convertView.findViewById(R.id.tv1);
holder.thirdimage = (ImageView)convertView.findViewById(R.id.image3);
holder.ttextview = (TextView) convertView.findViewById(R.id.tv3);
holder.secondimage = (ImageView) convertView
.findViewById(R.id.image2);
holder.stextview = (TextView) convertView.findViewById(R.id.tv2);
holder.fourthimage = (ImageView) convertView.findViewById(R.id.image4);
holder.ftextview = (TextView) convertView.findViewById(R.id.tv4);
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
int i = 0 * position;
if(i<=item.size()){
Details det = (Details) getItem(i);
holder.lay1.setVisibility(View.VISIBLE);
holder.textview.setText(det.getImgname());
holder.image.setBackgroundResource(det.getImgid());
}
int j = 0*position+1;
if(j<=item.size()){
Details det = (Details)getItem(j);
holder.lay2.setVisibility(View.VISIBLE);
holder.stextview.setText(det.getImgname());
holder.secondimage.setBackgroundResource(det.getImgid());
}
int k = 0* position+2;
if(k<item.size()){
Details det = (Details)getItem(k);
holder.lay3.setVisibility(View.VISIBLE);
holder.thirdimage.setBackgroundResource(det.getImgid());
holder.ttextview.setText(det.getImgname());
}
int l = 0* position+3;
if(l<item.size()){
Details det = (Details)getItem(l);
holder.lay4.setVisibility(View.VISIBLE);
holder.fourthimage.setBackgroundResource(det.getImgid());
holder.ftextview.setText(det.getImgname());
}
return convertView;
}
/** View holder for the views we need access to */
private static class Holder {
public TextView textview;
public TextView stextview;
public TextView ttextview;
public TextView ftextview;
public ImageView image;
public ImageView secondimage;
public ImageView thirdimage;
public ImageView fourthimage;
public LinearLayout lay1;
public LinearLayout lay2;
public LinearLayout lay3;
public LinearLayout lay4;
}
}
我的输出如下
因此我得到了输出..