2013-03-03 168 views
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); 
      } 
     } 
     }); 
    } 
} 

如果返回true,我想提出一個小對勾當用戶返回到第一個活動時在網格視圖中查看。

回答

0

GridView使用來自適配器的數據。您必須更改適配器的數據並通知適配器數據已更改。然後,更新的方法可以在Gridview中處理這些封裝。也許這個例子可以幫助你開始: http://developer.android.com/guide/topics/ui/layout/gridview.html

而且具有活性的反應在b活動的行爲,我想你可能想看看意圖和意圖過濾器: http://developer.android.com/guide/components/intents-filters.html

否則,請提供一些您的代碼供我們評論。

+0

我編輯我的代碼的問題。 – ocry 2013-03-03 14:42:11

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>