我在页面的第一个获得前9个项目,并且仅在这9个项目之后,即时获取假设出现在列表中的所有16个项目。 如何移动这9个重复的项目?
这是我的java代码(SearchCategories.java)
package com.example.zygy1;
public class SearchCategories extends MenuActivity {
private GridView gridView;
private GridViewAdapter customGridAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_categories);
gridView = (GridView) findViewById(R.id.gridView);
customGridAdapter = new GridViewAdapter(this, R.layout.row_grid, getData());
//DON'T continue if the adapter is not empty; prevents duplicates
gridView.setAdapter(customGridAdapter);
gridView.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
openSearchResults(position);
}
});
}
public void openSearchResults(int position){
Intent intent = new Intent(this, SearchResults.class);
intent.putExtra("position", position);
startActivity(intent);
}
private ArrayList<ImageItem> getData() {
final ArrayList<ImageItem> imageItems = new ArrayList<ImageItem>();
// retrieve String drawable array
TypedArray imgs=null;
imgs = getResources().obtainTypedArray(R.array.image_ids);
for (int i = 0; i < imgs.length(); i++) {
Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(),imgs.getResourceId(i, -1));
imageItems.add(new ImageItem(bitmap, getHeadline(i)));
}
return imageItems;
}
public String getHeadline(int num){
String headline;
if(num==0){ headline="כלי עבודה";}
else if(num==1){ headline="מכשירי חשמל";}
else if(num==2){ headline="לגינה";}
else if(num==3){ headline="ספורט";}
else if(num==4){ headline="מחשבים ומובייל";}
else if(num==5){ headline="לתינוק ולילד";}
else if(num==6){ headline="צילום";}
else if(num==7){ headline="מסיבות ואירועים";}
else if(num==8){ headline="ביגוד ואביזרים";}
else if(num==9){ headline="מכשור וציוד אוכל";}
else if(num==10){ headline="ריהוט";}
else if(num==11){ headline="למטייל";}
else if(num==12){ headline="רפואה";}
else if(num==13){ headline="כלי נגינה";}
else if(num==14){ headline="משחקים";}
else { headline="אחר";}
return headline;
}
public class GridViewAdapter extends ArrayAdapter {
private Context context;
private int layoutResourceId;
private ArrayList data = new ArrayList();
public GridViewAdapter(Context context, int layoutResourceId,
ArrayList data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
holder.imageTitle = (TextView) row.findViewById(R.id.text);
holder.image = (ImageView) row.findViewById(R.id.image);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
ImageItem item = (ImageItem) data.get(position);
holder.imageTitle.setText(item.getTitle());
holder.image.setImageBitmap(item.getImage());
return row;
}
}
public class ViewHolder {
TextView imageTitle;
ImageView image;
}
public class ImageItem {
private Bitmap image;
private String title;
public ImageItem(Bitmap image, String title) {
super();
this.image = image;
this.title = title;
}
public Bitmap getImage() {
return image;
}
public void setImage(Bitmap image) {
this.image = image;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
这里是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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.zygy1.SearchCategories" >
<GridView
android:id="@+id/gridView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:columnWidth="100dp"
android:gravity="center"
android:numColumns="auto_fit"
android:verticalSpacing="5dp"
android:drawSelectorOnTop="true"
android:stretchMode="columnWidth" >
</GridView>
以下是每个项目的xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="vertical"
android:padding="5dp"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false">
<ImageView
android:id="@+id/image"
android:layout_width="100dp"
android:layout_height="100dp" >
</ImageView>
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="center"
android:textSize="12sp" >
</TextView>
String中的字符串数组,顺便说一下我在eclipse上工作
<string-array name="image_ids">
<item>@drawable/image_1</item>
<item>@drawable/image_2</item>
<item>@drawable/image_3</item>
<item>@drawable/image_4</item>
<item>@drawable/image_5</item>
<item>@drawable/image_6</item>
<item>@drawable/image_7</item>
<item>@drawable/image_8</item>
<item>@drawable/image_9</item>
<item>@drawable/image_1</item>
<item>@drawable/image_2</item>
<item>@drawable/image_3</item>
<item>@drawable/image_4</item>
<item>@drawable/image_5</item>
<item>@drawable/image_6</item>
<item>@drawable/image_7</item>
<item>@drawable/image_8</item>
<item>@drawable/image_9</item>
<item>@drawable/image_10</item>
<item>@drawable/image_11</item>
<item>@drawable/image_12</item>
<item>@drawable/image_13</item>
<item>@drawable/image_14</item>
<item>@drawable/image_15</item>
<item>@drawable/image_16</item>
</string-array>
答案 0 :(得分:0)
你的数组中有25个项目......从1到9再从1开始。