我在活动A中的gridview中有视图。在活动B中,如果发生某些操作,我想在gridview中的视图上添加一点复选标记。当我回到活动A时,我想在同一个网格视图中用这个小的复选标记显示这个视图。我该怎么做?
这是我的活动,其中包含网格视图
public class LogoSelectionActivity extends Activity {
static int pos;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_logos);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
pos = position;
if(ImageAdapter.items[position] == 0){
Intent intent = new Intent(LogoSelectionActivity.this, LogoActivity.class);
intent.putExtra ("clicked_position", position);
startActivity(intent);
}else{
Intent intent = new Intent(LogoSelectionActivity.this, CorrectActivity.class);
startActivity(intent);
}
}
});
}
这是我比较输入的其他活动:
public class LogoActivity extends Activity{
EditText text;
Button check;
Boolean a;
int id;
Names name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_logo);
check = (Button) findViewById(R.id.Check_button);
text = (EditText) findViewById(R.id.editText1);
ImageView view = (ImageView)findViewById(R.id.imageView1);
final ImageView incorrect = (ImageView)findViewById(R.id.incorrect);
switch (getIntent().getIntExtra ("clicked_position", -1))
{
case 0:
view.setImageResource(R.drawable.adese);
id = R.drawable.adese;
break;
case 1:
view.setImageResource(R.drawable.birvar);
id = R.drawable.birvar;
break;
case 2:
view.setImageResource(R.drawable.agaoglu);
break;
case 3:
view.setImageResource(R.drawable.akinsoft);
break;
default:
view.setImageResource(R.drawable.afra);
id = R.drawable.afra;
}
name = Names.forDrawable(id);
check.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
a=name.isCorrect(text.getText().toString());
if(a==true){
ImageAdapter.items[LogoSelectionActivity.pos] = 1;
Intent intent = new Intent(LogoActivity.this, CorrectActivity.class);
startActivity(intent);
}
else{
incorrect.setVisibility(0);
}
}
});
}
}
如果a返回true,我想在用户返回第一个活动时在网格视图中的视图上添加一个复选标记。
答案 0 :(得分:1)
试试此代码
package com.shreeexample.grid;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class ImageAdapter extends BaseAdapter{
private Context context;
int checked=0;
private final String[] mobileValues;
public ImageAdapter(Context context, String[] mobileValues) {
this.context = context;
this.mobileValues = mobileValues;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mobileValues.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public View getView(final int position, View convertView, ViewGroup parent) {
final ImageView img_select;
final int selectedPosition = -1;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
// get layout from mobile.xml
gridView = inflater.inflate(R.layout.two, null);
// set value into textview
TextView textView = (TextView) gridView
.findViewById(R.id.grid_item_label);
textView.setText(mobileValues[position]);
// set image based on selected text
ImageView imageView = (ImageView) gridView
.findViewById(R.id.grid_item_image);
img_select = (ImageView)gridView.findViewById(R.id.grid_item_image_select);
String mobile = mobileValues[position];
if (mobile.equals("Windows")) {
imageView.setImageResource(R.drawable.ic_launcher);
} else if (mobile.equals("iOS")) {
imageView.setImageResource(R.drawable.ic_launcher);
} else if (mobile.equals("Blackberry")) {
imageView.setImageResource(R.drawable.ic_launcher);
} else {
imageView.setImageResource(R.drawable.ic_launcher);
}
imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
img_select.setFocusable(true);
img_select.setEnabled(true);
if(checked==0)
{
img_select.setBackgroundResource(android.R.drawable.btn_plus);
checked =1;
}
else
{
img_select.setBackgroundResource(0);
checked=0;
}
}
});
} else {
gridView = (View) convertView;
}
return gridView;
}
}
为此制作一个像这样的布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/grid_item_image"
android:layout_width="50px"
android:layout_height="50px"
android:layout_marginRight="10px"
android:src="@drawable/ic_launcher" >
</ImageView>
<ImageView
android:id="@+id/grid_item_image_select"
android:layout_width="50px"
android:layout_height="50px"
android:layout_marginRight="10px"
android:src="@drawable/ic_launcher" >
</ImageView>
<TextView
android:id="@+id/grid_item_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="label"
android:layout_marginTop="5px"
android:textSize="15px"
android:layout_below="@+id/grid_item_image">
</TextView>
</RelativeLayout>
答案 1 :(得分:0)
GridView使用来自适配器的数据。您必须更改适配器的数据并通知适配器数据已更改。然后,更新方法可以处理Gridview中的插入。也许这个例子可以帮助你: http://developer.android.com/guide/topics/ui/layout/gridview.html
让活动A对活动B中的动作做出反应,我想你可能想看看Intents和Intent Filters: http://developer.android.com/guide/components/intents-filters.html
否则,请提供一些代码供我们评论。