我正在開發一個具有座位計劃的Android應用程序。座位是放置在GridView中的ImageView。點擊時,imageview將被設置爲不同的圖像。問題不在於我點擊哪個座位,只有第一個座位的圖像發生了變化。希望能夠得到一些幫助,爲什麼。以下是我的代碼:在GridView中爲ImageView更改第一個圖像
activity_main.xml中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="vertical">
<TextView
android:id="@+id/grid_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="15dp"
android:text="@string/screen"
android:textSize="20sp"
android:gravity="center" >
</TextView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="50dp"
android:paddingRight="50dp"
android:paddingTop="10dp">
<GridView
android:id="@+id/gridView"
android:layout_width="240dp"
android:layout_height="match_parent"
android:paddingTop="15dp"
android:listSelector="@android:color/transparent"
android:numColumns="8"
android:columnWidth="30dp"
android:gravity="center"
android:verticalSpacing="0dp"
android:stretchMode="none"
android:layout_centerInParent ="true">
</GridView>
</RelativeLayout>
</LinearLayout>
seat_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/seatItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:adjustViewBounds="true"/>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity implements OnItemClickListener {
GridView gridview;
ImageAdapter imageAdapter;
ArrayList<Item> data = new ArrayList<Item>();
ImageView image;
boolean showingFirst = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView(); // Initialize the GUI Components
fillData(); // Insert The Data
setDataAdapter(); // Set the Data Adapter
}
// Initialize the GUI Components
private void initView() {
gridview = (GridView) findViewById(R.id.gridView);
gridview.setOnItemClickListener(this);
}
// Insert The Data
private void fillData() {
for (int i = 1; i < 17; i++) {
data.add(new Item(i, getResources().getDrawable(R.drawable.seat)));
}
}
// Set the Data Adapter
private void setDataAdapter() {
imageAdapter = new ImageAdapter(getApplicationContext(),
R.layout.seat_item, data);
gridview.setAdapter(imageAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onItemClick(final AdapterView<?> arg0, final View view,
final int position, final long id) {
image= (ImageView) findViewById(R.id.seatItem);
if(showingFirst == true){
String message = "Clicked: " + position;
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT)
.show();
image.setImageResource(R.drawable.seat_blue);
showingFirst = false;
}else{
image.setImageResource(R.drawable.seat);
showingFirst = true;
}
}
}
ImageAdapter.java
public class ImageAdapter extends ArrayAdapter<Item> {
private Context mContext;
int resourceId;
ArrayList<Item> data = new ArrayList<Item>();
ImageView image;
// Constructor
public ImageAdapter(Context c,int layoutResourceId, ArrayList<Item> data) {
super(c, layoutResourceId, data);
this.mContext = c;
this.resourceId = layoutResourceId;
this.data = data;
}
@Override
public Item getItem(int position) {
System.out.println("Get: "+position);
return data.get(position);
}
@Override
public long getItemId(int arg0)
{
return arg0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null)
{
final LayoutInflater layoutInflater =
(LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(resourceId, parent, false);
holder = new ViewHolder();
holder.imgItem = (ImageView) convertView.findViewById(R.id.seatItem);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
Item item = getItem(position);
System.out.println(position);
holder.imgItem.setImageDrawable(item.getImage());
return convertView;
}
static class ViewHolder
{
ImageView imgItem;
}
}
謝謝!有效。 – user1097856 2014-09-28 17:01:55