0

我在活动 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,当用户返回第一个活动时,我想在网格视图中的视图上放一个小复选标记。

4

2 回答 2

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>
于 2013-03-03T15:58:26.770 回答
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

否则,请提供您的一些代码供我们评论。

于 2013-03-03T13:45:03.487 回答