2016-06-13 139 views
1

嘿,我可以如何更改單元格的圖像我點擊在網格視圖中,我應該添加到此代碼以顯示新圖像例如圖像「boxb」在圖像中的prgmImages列表之前插入的地方:設置新圖像時,單擊一個單元格中的gridview android

我爲GridView 「CAdapter」 自定義適配器:

public class CAdapter extends BaseAdapter { 

    int [] imageId; 
    Context context; 

    private static LayoutInflater inflater=null; 
    public CAdapter(Context context, int[] prgmImages) { 
     // TODO Auto-generated constructor stub 
     this.context=context; 
     //context=level1; 
     imageId=prgmImages; 
     inflater = (LayoutInflater)context. 
       getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    } 

    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return imageId.length; 
    } 

    @Override 
    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 

    @Override 
    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 

    public class Holder 
    { 
     //TextView tv; 
     ImageView img; 
    } 
    View rowView; 

    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 
     Holder holder=new Holder(); 
     //View rowView; 

     rowView = inflater.inflate(R.layout.game_content, null); 
     // holder.tv=(TextView) rowView.findViewById(R.id.textView1); 
     holder.img=(ImageView) rowView.findViewById(R.id.imageView1); 

     // holder.tv.setText(result[position]); 
     holder.img.setImageResource(imageId[position]); 

     rowView.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
        Toast.makeText(context, "what's up dude ! ", Toast.LENGTH_LONG).show(); 
       Intent yourIntent = new Intent(context,QuestionA.class); 
       yourIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |Intent.FLAG_ACTIVITY_MULTIPLE_TASK); 
       v.getContext().startActivity(yourIntent); 
      } 
     }); 


     return rowView; 
    } 

} 

誰使用gridview的 「1級」 的活動:

public class Level1 extends Activity { 
    GridView gv; 
    Button hammer_btn; 
    Button torch_btn; 
    Context context; 
    public static int[] prgmImages = {R.drawable.boxa, R.drawable.boxa, R.drawable.boxa, R.drawable.boxa, R.drawable.boxa, R.drawable.boxa}; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.level1_window); 
     gv = (GridView) findViewById(R.id.gridView1); 
     hammer_btn = (Button) findViewById(R.id.button); 
     torch_btn = (Button) findViewById(R.id.button1); 
     context = this; 
     gv.setAdapter(new CAdapter(this, prgmImages)); 

     gv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View v, 
            int position, long id) { 

       if(position==0){ 
        Intent iinent= new Intent(Level1.this,QuestionA.class); 
        startActivity(iinent); 
       } 


      } 
     }); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu, menu); 
     return true; 
    } 

} 

回答

0

你可以在你的gridview上設置OnItemClickListener。方法參數視圖,你只是這樣做:

public void onItemClick(AdapterView<?> parent, View view, int position, long id){ ImageView imageView = (ImageView) view.findViewById(R.id.imageviewid); imageView.setImageDrawable(yourdrawable); }

imageviewid是你gridadapter內的ImageView的ID,可繪製的圖像,你想設置,你可以用

getResources().getDrawable(R.drawable.nameofdrawable); 

說明獲取:

Click監聽器有一個參數'View view',它返回gridview(您的案例中的R.layout.game_content)中當前點擊的項目的佈局以及findViewBy如果您搜索id(imageview的id)查看並將其設置在新的imageview中,那麼您只需使用stamdard方法來設置圖像。

要求任何進一步的幫助

+1

感謝效應初探,做u由imageviewid意味着舊的圖像的ID,如果是,那麼我怎麼能得到imageviewid,因爲我直接使用列表中插入呢? –

+0

Imageview ID是imageview1(圖像視圖在你的持有人身份證)和drawablename是在res/drawable目錄中的圖像名稱 –

+0

thx,現在我低於它,但我改變這部分(getResources()。getDrawable(R.drawable.boxb );)通過這一個(ResourcesCompat.getDrawable(getResources(),R.drawable.boxb,null);)因爲getDrawable在第一個代碼中編譯器做了一行並且說它已經被棄用,當我運行該應用程序時沒有任何反應,舊圖像不會改變? –

相關問題