2013-02-23 38 views
0

我想通過意向幫助將列表視圖中存在的複選框的位置傳遞給其他活動。但它在另一項活動中顯示我爲空。或者有人可以告訴我如何使用下面的代碼將選中的複選框數據傳遞給另一個活動?未通過其他活動的複選框數據

我想從各個複選框的列表視圖中將值存儲在數組中。然後轉移到另一個活動

public class MyAdapter extends ArrayAdapter<Model> { 
int getPosition; 
Intent in; 
private final List<Model> list; 
public final Activity context; 
boolean checkAll_flag = false; 
boolean checkItem_flag = false; 

public MyAdapter(Activity context, List<Model> list) { 
    super(context, R.layout.row, list); 
    this.context = context; 
    this.list = list; 
} 

static class ViewHolder { 
    protected TextView text; 
    protected CheckBox checkbox; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    ViewHolder viewHolder = null; 
    if (convertView == null) { 
     LayoutInflater inflator = context.getLayoutInflater(); 
     convertView = inflator.inflate(R.layout.row, null); 
     viewHolder = new ViewHolder(); 
     viewHolder.text = (TextView) convertView.findViewById(R.id.label); 
     viewHolder.checkbox = (CheckBox) convertView 
       .findViewById(R.id.check); 
     viewHolder.checkbox 
       .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 

        @Override 
        public void onCheckedChanged(CompoundButton buttonView, 
          boolean isChecked) { 
         getPosition = (Integer) buttonView.getTag(); // setTag. 

        } 
       }); 
     convertView.setTag(viewHolder); 
     convertView.setOnClickListener(new MyCustomItemClickListener(getPosition)); 

     convertView.setTag(R.id.label, viewHolder.text); 
     convertView.setTag(R.id.check, viewHolder.checkbox); 
    } else { 
     viewHolder = (ViewHolder) convertView.getTag(); 
    } 
    viewHolder.checkbox.setTag(position); // This line is important. 

    viewHolder.text.setText(list.get(position).getName()); 
    viewHolder.checkbox.setChecked(list.get(position).isSelected()); 

    return convertView; 
} 

class MyCustomItemClickListener implements OnClickListener { 

    MyCustomItemClickListener(int getPositionx) { 
     /* 
     * Whatever initialization you need can be done here. You can pass 
     * values in constructor when calling it from getview and use with 
     * intent extras 
     */ 
     in = new Intent(context, YourActivity.class); 
     in.putExtra("aa", getPositionx); 
    } 

    @Override 
    public void onClick(View arg0) { 
     // Launch Activity 

     context.startActivity(in); 
    } 

    } 
} 
+0

您需要將複選框文本傳遞給其他活動或複選框位置? – 2013-02-23 06:28:08

+0

你是如何從意圖中提取價值的?你可以顯示代碼,因爲你實際上在代碼中是空的,不是嗎? – 2013-02-23 06:33:38

+0

我想從各個複選框的列表視圖中將值存儲在數組中。然後轉移到另一個活動 – 2013-02-23 13:21:01

回答

0

按照下面的方式試試。無需在構造函數中傳遞getPositionx,因爲您已經在類中全局地定義它。

class MyCustomItemClickListener implements OnClickListener { 

    MyCustomItemClickListener() { 
     /* 
     * Whatever initialization you need can be done here. You can pass 
     * values in constructor when calling it from getview and use with 
     * intent extras 
     */ 

    } 

    @Override 
    public void onClick(View arg0) { 
     // Launch Activity 

    in = new Intent(context, YourActivity.class); 
     in.putExtra("aa", getPositionx); 

     context.startActivity(in); 
    } 

} 

,改變你的初始化

convertView.setOnClickListener(new MyCustomItemClickListener()); 
1

Declear爲getPosition作爲一個靜態變量,您可以訪問 值MyAdapter.getPosition到另一個活動

public static getPosition; 

和活動,其中你想訪問這個變量

MyAdapter.getPosition use this. 

但你逝去,這也是賴特值,但傳遞 它的意圖之前檢查度過你的價值的方式是不爲空,在這種情況下

getPosition = (Integer) buttonView.getTag(); 

是不是能夠得到的值。