嘿,我可以如何更改單元格的圖像我點擊在網格視圖中,我應該添加到此代碼以顯示新圖像例如圖像「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;
}
}
感謝效應初探,做u由imageviewid意味着舊的圖像的ID,如果是,那麼我怎麼能得到imageviewid,因爲我直接使用列表中插入呢? –
Imageview ID是imageview1(圖像視圖在你的持有人身份證)和drawablename是在res/drawable目錄中的圖像名稱 –
thx,現在我低於它,但我改變這部分(getResources()。getDrawable(R.drawable.boxb );)通過這一個(ResourcesCompat.getDrawable(getResources(),R.drawable.boxb,null);)因爲getDrawable在第一個代碼中編譯器做了一行並且說它已經被棄用,當我運行該應用程序時沒有任何反應,舊圖像不會改變? –