Android自定义列表视图,在调用另一个活动并返回后,切换按钮状态重置

时间:2015-03-16 06:31:15

标签: android listview android-listview togglebutton

下面是我的活动类,它从sqllite数据库中检索数据并使用3textviews和1切换按钮生成自定义列表视图

活动类:

public class PaymentReminderActivity extends ActionBarActivity {
ReminderDataSource datasource;

// Array of booleans to store toggle button status
public boolean[] status = {
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false,
        false
};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_payment_reminder);
    datasource = new ReminderDataSource(this);

    /** Restore from the previous state if exists */
    if(savedInstanceState!=null){
        status = savedInstanceState.getBooleanArray("status");
    }

    //GET ALL DATA
    datasource.open();
    final List<ReminderClass> values = datasource.getAllUsers();
    datasource.close();

    ListView lvReminder = (ListView) findViewById(R.id.lv_countries);
    registerForContextMenu(lvReminder);

    OnItemClickListener itemClickListener = new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> lv, View item, int position, long id) {

            ListView lView = (ListView) lv;
            SimpleAdapter adapter = (SimpleAdapter) lView.getAdapter();

            HashMap<String,Object> hm = (HashMap) adapter.getItem(position);

            /** The clicked Item in the ListView */
            RelativeLayout rLayout = (RelativeLayout) item;

            /** Getting the toggle button corresponding to the clicked item */
           ToggleButton tgl = (ToggleButton) rLayout.getChildAt(2);


            String strStatus = "";
            if(tgl.isChecked()){
                tgl.setChecked(false);
                strStatus = "Off";
                status[position]=false;
            }else{
                tgl.setChecked(true);
                strStatus = "On";
                status[position]=true;
            }
            Toast.makeText(getBaseContext(), (String) hm.get("txt") + " : " + strStatus, Toast.LENGTH_SHORT).show();
        }
    };

    lvReminder.setOnItemClickListener(itemClickListener);

    // Each row in the list stores country name and its status
    List<HashMap<String,Object>> aList = new ArrayList<HashMap<String,Object>>();

    for (int i = 0; i<values.size(); i++) {
        HashMap<String, Object> hm = new HashMap<String, Object>();
        hm.put("txt", values.get(i).getType());
        hm.put("txt2", values.get(i).getDesc());
        hm.put("txt3", values.get(i).getDay() + " of every month");
        hm.put("stat", status[i]);
        aList.add(hm);
    }

    // Keys used in Hashmap
    String[] from = {"txt","txt2","txt3","stat" };

    // Ids of views in listview_layout
    int[] to = { R.id.tv_item,R.id.tv_item2, R.id.tv_item3, R.id.tgl_status};

    // Instantiating an adapter to store each items
    // R.layout.listview_layout defines the layout of each item
    SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.lv_layout, from, to);

    lvReminder.setAdapter(adapter);
}

/** Saving the current state of the activity
 * for configuration changes [ Portrait <=> Landscape ]
 */
@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putBooleanArray("status", status);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_payment_reminder, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_add) {
        Intent intent = new Intent();
        intent.setClass(this, NewReminderActivity.class);
        startActivityForResult(intent, 0);
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    if (v.getId()==R.id.lv_countries) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_list, menu);
    }
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    long selectid = info.id; //_id from database in this case
    int selectpos = info.position;
    switch(item.getItemId()) {
        case R.id.add:
            // add stuff here
            return true;
        case R.id.edit:
            // edit stuff here
            return true;
        case R.id.delete:

            return true;
        default:
            return super.onContextItemSelected(item);
    }
}

}

列表视图上的切换切换按钮没有问题。 一旦我离开那个活动并回来,状态就会被重置。

0 个答案:

没有答案