0
我用100個項目(每個項目包含三個textView)填充我的gridView,顯然它需要滾動。如果我點擊一個項目,它的背景會變成紅色。我的問題是,當我使用滾動時,我看到另一個項目也變成了紅色!爲什麼?滾動GridView並更改項目網格中的背景顏色
在我的活動中,我有String [] subg,String [] dEstancias,String [] limp。 (每個包含100個字符串)。在我的活動:
final CustomGrid adapter = new CustomGrid(this, subg, dEstancias, limp, previousSelectedPosition);
// Getting a reference to gridview of MainActivity
final GridView gridView = (GridView) findViewById(R.id.gridview);
// Setting an adapter containing images to the gridview
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v,int position, long id)
{
previousSelectedPosition = position;
v.setBackgroundColor(Color.RED);
}
});
在我的適配器我getView爲:
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View grid;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = new View(mContext);
grid = inflater.inflate(R.layout.grid_item, parent, false);
} else {
grid = (View) convertView;
}
TextView textView = (TextView) grid.findViewById(R.id.sgob);
TextView textView2 = (TextView) grid.findViewById(R.id.item);
TextView textView3 = (TextView) grid.findViewById(R.id.limp);
textView.setText(subg[position]);
textView2.setText(estancias[position]);
textView3.setText(limp[position]);
return grid;
}
我的網格項是:
<?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"
android:padding="40dp"
android:layout_gravity="center"
android:background="@android:color/white">
<TextView
android:id="@+id/sgob"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="@android:color/black"
android:background="#2cc546"
android:gravity="center_horizontal"
/>
<TextView
android:id="@+id/item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="@android:color/black"
android:background="#2cc546"
android:gravity="center_horizontal"
/>
<TextView
android:id="@+id/limp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="@android:color/black"
android:background="#2cc546"
android:gravity="center_horizontal"
/>
</LinearLayout>