我想在从活动返回时更改gridview中的图像。点击position == 0
后,会打开Activity2.class
。我在Activity2.class
点击了一个按钮,当它被点击时返回到Activity1.class
,这就是我想在网格视图中替换position == 0
的图像。
ImageAdapter.java
public class ImageAdapter extends BaseAdapter {
private Context mContext;
Integer[] mThumbIds;
public ImageAdapter(Context c) {
mContext = c;
this.mThumbIds=mThumbIds;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(mContext);
final ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.item_level_single, null, true);
holder = new ViewHolder();
holder.image = (ImageView) convertView
.findViewById(R.id.myImageView);
holder.number = (TextView) convertView
.findViewById(R.id.myImageViewText);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.image.setImageResource(mThumbIds[position]);
if (position == 0) {
holder.number.setText("1");
}
if (position == 1) {
holder.number.setText("2");
}
if (position == 2) {
holder.number.setText("3");
}
if (position == 3) {
holder.number.setText("4");
}
return convertView;
}
private static class ViewHolder {
public ImageView image;
public TextView number;
}
}
Activity1.java
public class Activity1 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_1);
Integer[] mThumbIds = {
R.drawable.ic_round, R.drawable.ic_round,
R.drawable.ic_round, R.drawable.ic_round,
};
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this,mThumbIds));
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
if (position == 0) {
Intent intent = new Intent(v.getContext(), Activity2.class);
intent.putExtra("text1", getString(R.string.test));
startActivityForResult(intent, 2);
}
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
// check if the request code is same as what is passed here it is 2
if(requestCode==2)
{
/*Change image at 0th position and pass array to ImageAdapter*/
Integer[] mThumbIds = {
R.drawable.ic_complete, R.drawable.ic_complete,
R.drawable.ic_complete, R.drawable.ic_complete,
};
gridview.setAdapter(new ImageAdapter(this));
}
}
}
Activity2.java
public class Activity2 extends AppCompatActivity {
Button btn_next_exercise;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
btn_replace = (Button) findViewById(R.id.btn_replace);
final Intent i = new Intent();
btn_replace.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent=new Intent();
setResult(2,intent);
finish();//finishing activity
}
});
}
}
答案 0 :(得分:0)
Activity1.java
public class Activity1 extends AppCompatActivity {
GridView gridview ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_1);
private Integer[] mThumbIds = {
R.drawable.ic_round, R.drawable.ic_round,
R.drawable.ic_round, R.drawable.ic_round,
};
gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this,mThumbIds));
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
if (position == 0) {
Intent intent = new Intent(v.getContext(), Activity2.class);
intent.putExtra("text1", getString(R.string.test));
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
startActivityForResult(intent, 2);// Activity is started with requestCode 2
}
}
});
}
// Call Back method to get the Message form other Activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
// check if the request code is same as what is passed here it is 2
if(requestCode==2)
{
/*Change image at 0th position and pass array to ImageAdapter*/
private Integer[] mThumbIds = {
R.drawable.ic_round1, R.drawable.ic_round,
R.drawable.ic_round, R.drawable.ic_round,
};
gridview.setAdapter(new ImageAdapter(this,mThumbIds));
}
}
}
Activity2.java
public class Activity2 extends AppCompatActivity {
Button btn_next_exercise;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
btn_replace = (Button) findViewById(R.id.btn_replace);
final Intent i = new Intent();
btn_replace.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent=new Intent();
setResult(2,intent);
finish();//finishing activity
}
});
}
}
在适配器中,删除mThumbIds图像数组并在activity1中设置适配器时传递mThumbIds,
gridview.setAdapter(new ImageAdapter(this,mThumbIds));
ImageAdapter:
public class ImageAdapter extends BaseAdapter {
private Context mContext;
Integer[] mThumbIds;
public ImageAdapter(Context c,Integer[] mThumbIds) {
mContext = c;
this.mThumbIds=mThumbIds;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(mContext);
final ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.item_level_single, null, true);
holder = new ViewHolder();
holder.image = (ImageView) convertView
.findViewById(R.id.myImageView);
holder.number = (TextView) convertView
.findViewById(R.id.myImageViewText);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.image.setImageResource(mThumbIds[position]);
if (position == 0) {
holder.number.setText("1");
}
if (position == 1) {
holder.number.setText("2");
}
if (position == 2) {
holder.number.setText("3");
}
if (position == 3) {
holder.number.setText("4");
}
return convertView;
}
private static class ViewHolder {
public ImageView image;
public TextView number;
}
}