2014-09-28 74 views
0

我正在開發一個具有座位計劃的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; 
    } 
} 

回答

1

使用onItemClick第二個參數view從ListView控件訪問的ImageView點擊排佈局:

image= (ImageView)view.findViewById(R.id.seatItem); 
+0

謝謝!有效。 – user1097856 2014-09-28 17:01:55

0

乍一看我沒有發現任何你的代碼錯誤。我建議您嘗試將點擊操作委派給可點擊視圖本身,而不是使用onItemClick,因此在getView方法中,可以使用setonCLickListener到圖像視圖,或者整個視圖,如果這最適合您的解決方案。 在附註中,我還建議您不要將該活動用作回調,因爲在大多數情況下,它會導致活動泄漏,創建班級或使用不合法的班級。

希望這可以幫助你。

+0

找到了答案。但感謝你的回覆! – user1097856 2014-09-28 17:09:59