ListView动态更新

listview更新有两种方式:一种是通知Adapter更新,不更新Activity,一种是使用Handler。

下面就使用notifyDataSetChanged()函数来更新listview

 无法贴图,很遗憾,

就帖代码吧:main.xml:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
           android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:gravity="center_horizontal"
              android:background="@drawable/bg"
>
 <include android:layout_alignParentLeft ="true"
           layout="@layout/app_top_title"
 />
 <ListView android:id="@+id/system_profile_list"
           android:layout_width="320px"
        android:layout_height="wrap_content"
        android:divider="@drawable/line01"
        android:cacheColorHint="#00000000"
        android:background="@drawable/bg_a"
 />
</LinearLayout>

item_xml:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
           android:orientation="horizontal"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
>
   <LinearLayout android:layout_width="fill_parent"
              android:layout_height="80px"
              android:gravity="center_vertical|center_horizontal"
   >
  <ImageView android:id="@+id/sysprofile_item_image"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
        />
  <LinearLayout android:layout_width="200px"
                      android:layout_height="fill_parent"
                      android:orientation="vertical"
                      android:gravity="center"
            
         >
                <TextView android:id="@+id/sysprofile_item_title"
                          android:layout_width="fill_parent"
                          android:layout_height="wrap_content"
                          android:textColor="@color/black"
                          android:textSize="20sp"
                />
                <TextView android:id="@+id/sysprofile_item_content"
                          android:layout_width="fill_parent"
                          android:layout_height="wrap_content"
                          android:textColor="@color/black"
                          android:textSize="16sp"
                 />
        </LinearLayout>
  <ImageView  android:id="@+id/sysprofile_item_label"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:visibility="gone"
        />
  <CheckBox android:id="@+id/sysprofile_item_check"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:button="@drawable/btn_checkbox"
                  android:visibility="gone"
        />
    </LinearLayout>
</LinearLayout>

java代码:

package com.mobimtech.imiParadise;

import com.mobimtech.imiParadise.util.Globals;
import com.mobimtech.imiParadise.util.SystemProfile;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class SystemProfileActivity extends Activity implements OnItemClickListener{

 private ListView mProfileList;
 private int image [] = {R.drawable.vedio_mode,R.drawable.search,R.drawable.prompt_tone,
                            R.drawable.integrate_sms,R.drawable.off_line
                            };
 private String title [] ;
 private String content [] ;
 private String vediomode [] ;
 private TextView mVedio;
 private TextView mTitle;
 private CheckBox checkbox[] = new CheckBox [4];
 
 private int vedio_mode;
 private int search_id;
 private int prompt_tone_id;
 private int integrate_sms_id;
 private int off_line_sms_id;
 private ProfileAdapter mProfileAdapter;
 
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.system_profile_main);
  mProfileAdapter = new ProfileAdapter();
  mProfileList = (ListView) findViewById(R.id.system_profile_list);
  mProfileList.setAdapter(mProfileAdapter);
  
  mTitle = (TextView) findViewById(R.id.option_title);
  mTitle.setText(getString(R.string.system_setting));
  
  title = getResources().getStringArray(R.array.system_setting_title);
  content = getResources().getStringArray(R.array.system_setting_content);
  vediomode = getResources().getStringArray(R.array.system_setting_vedio);
  
  setVedio_mode(SystemProfile.getVedioMode(this));
  setSearch_id(SystemProfile.getSearchSet(this));
  setPrompt_tone_id(SystemProfile.getPromptTone(this));
  setIntegrate_sms_id(SystemProfile.getIntegrateSms(this));
  setOff_line_sms_id(SystemProfile.getOffLineSms(this));
  mProfileList.setOnItemClickListener(this);
  
 }

 @Override
 public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
  // TODO Auto-generated method stub
  if(position == 0){
   showDialog(Globals.DIALOG_VEDIO_MODE);
  }else{
   
   if(checkbox[position-1].isChecked()){
    checkbox[position-1].setChecked(false);
   }else{
    checkbox[position-1].setChecked(true);
   }
//   更新listview
   mProfileAdapter.notifyDataSetChanged();
  }
 }
 
 @Override
 protected Dialog onCreateDialog(int id) {
  // TODO Auto-generated method stub
  switch(id){
   
   case Globals.DIALOG_VEDIO_MODE:{
      
    return new AlertDialog.Builder(this)
                          .setTitle(R.string.vedio_mode_title)
                          .setItems(vediomode,new DialogInterface.OnClickListener(){

          @Override
          public void onClick(DialogInterface dialog,final int which) {
           // Auto-generated method stub
           if(which == 1){
            setVedio_mode(1);
           }else{
            setVedio_mode(0);
           }
//           更新listview
           mProfileAdapter.notifyDataSetChanged();
           SystemProfile.setVedioMode(SystemProfileActivity.this,getVedio_mode());
           removeDialog(Globals.DIALOG_VEDIO_MODE);
           }})
                            .create();
     }       
  }
  return null;
 }
 
 class CheckBoxChecked implements OnCheckedChangeListener{

  int id;
  
  public CheckBoxChecked(int id){
   this.id = id;
  }
  
  @Override
  public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
   // TODO Auto-generated method stub

   switch(id){
   case 1:
    if(isChecked){
     setSearch_id(1);
//     Log.e("debug","1:/tchecked");
    }else{
     setSearch_id(0);
    }
    SystemProfile.setSearchSet(SystemProfileActivity.this, getSearch_id());
    break;
   case 2:
    if(isChecked){
     setPrompt_tone_id(1);
    }else{
     setPrompt_tone_id(0);
    }
    SystemProfile.setPromptTone(SystemProfileActivity.this, getPrompt_tone_id());
    break;
   case 3:
    if(isChecked){
     setIntegrate_sms_id(1);
    }else{
     setIntegrate_sms_id(0);
    }
    SystemProfile.setIntegrateSms(SystemProfileActivity.this, getIntegrate_sms_id());
    break;
   case 4:
    if(isChecked){
     setOff_line_sms_id(1);
    }else{
     setOff_line_sms_id(0);
    }
    SystemProfile.setOffLineSms(SystemProfileActivity.this, getOff_line_sms_id());
    break;
   }
  }
 }
 
 public int getVedio_mode() {
  return vedio_mode;
 }

 public void setVedio_mode(int vedioMode) {
  vedio_mode = vedioMode;
 }
 
 public int getSearch_id() {
  return search_id;
 }

 public void setSearch_id(int searchId) {
  search_id = searchId;
 }

 public int getPrompt_tone_id() {
  return prompt_tone_id;
 }

 public void setPrompt_tone_id(int promptToneId) {
  prompt_tone_id = promptToneId;
 }

 public int getIntegrate_sms_id() {
  return integrate_sms_id;
 }

 public void setIntegrate_sms_id(int integrateSmsId) {
  integrate_sms_id = integrateSmsId;
 }

 public int getOff_line_sms_id() {
  return off_line_sms_id;
 }

 public void setOff_line_sms_id(int offLineSmsId) {
  off_line_sms_id = offLineSmsId;
 }
 class ProfileAdapter extends BaseAdapter{

  @Override
  public int getCount() {
   // TODO Auto-generated method stub
   return image.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 position;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
   // TODO Auto-generated method stub
   LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   View view = inflater.inflate(R.layout.system_profile_item, null); 
   ImageView title_view   = (ImageView) view.findViewById(R.id.sysprofile_item_image);
   TextView  title_text = (TextView) view.findViewById(R.id.sysprofile_item_title);
   title_view.setImageResource(image[position]);
   title_text.setText(title[position]);
   switch(position){
   case 0:{
    mVedio = (TextView)  view.findViewById(R.id.sysprofile_item_content);
    ImageView check_view   = (ImageView) view.findViewById(R.id.sysprofile_item_label); 
    check_view.setImageResource(R.drawable.list_icon);
    check_view.setVisibility(1);
    
    if(getVedio_mode()==1){
     mVedio.setText(vediomode[getVedio_mode()]);
    }else{
     mVedio.setText(vediomode[position]);
    }
//    Log.e("debug","getView");
    break;
   }
   case 1:{
    checkbox [position-1]  = (CheckBox) view.findViewById(R.id.sysprofile_item_check);
    TextView  content_text = (TextView) view.findViewById(R.id.sysprofile_item_content);
    content_text.setText(content[0]);
    checkbox [position-1].setVisibility(1);
    if(getSearch_id()==1){
     checkbox [position-1].setChecked(true);
    }else{
     checkbox [position-1].setChecked(false);
    }
    checkbox [position-1].setOnCheckedChangeListener(new CheckBoxChecked(position));
    checkbox [position-1].setFocusable(false);
    break;
   }
   case 2:{
    checkbox [position-1]  = (CheckBox) view.findViewById(R.id.sysprofile_item_check);
    TextView  content_text = (TextView)  view.findViewById(R.id.sysprofile_item_content);
    content_text.setText(content[1]);
    checkbox [position-1].setVisibility(1);
    if(getPrompt_tone_id()==1){
     checkbox [position-1].setChecked(true);
    }else{
     checkbox [position-1].setChecked(false);
    }
    checkbox [position-1].setOnCheckedChangeListener(new CheckBoxChecked(position));
    checkbox [position-1].setFocusable(false);
    break;
   }
   case 3:{
    checkbox [position-1]  = (CheckBox) view.findViewById(R.id.sysprofile_item_check);
    TextView  content_text = (TextView)  view.findViewById(R.id.sysprofile_item_content);
    content_text.setText(content[2]);
    checkbox [position-1].setVisibility(1);
    if(getIntegrate_sms_id()==1){
     checkbox [position-1].setChecked(true);
    }else{
     checkbox [position-1].setChecked(false);
    }
    checkbox [position-1].setOnCheckedChangeListener(new CheckBoxChecked(position));
    checkbox [position-1].setFocusable(false);
    break;
   }
   case 4:{
    checkbox [position-1]  = (CheckBox) view.findViewById(R.id.sysprofile_item_check);
    TextView  content_text = (TextView)  view.findViewById(R.id.sysprofile_item_content);
    content_text.setText(content[3]);
    checkbox [position-1].setVisibility(1);
    if(getOff_line_sms_id()==1){
     checkbox [position-1].setChecked(true);
    }else{
     checkbox [position-1].setChecked(false);
    }
    checkbox [position-1].setOnCheckedChangeListener(new CheckBoxChecked(position));
    checkbox [position-1].setFocusable(false);
    break;
   }
   }
   return view;
  }
  
 }
}

ps:使用ListActivity点击某一行的view不会让view上的checkbox响应,但是使用listview+activity可以,点击非checkbox区域,可以让回调checkbox响应函数。

 @Override
 public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
  // TODO Auto-generated method stub
  if(position == 0){
   showDialog(Globals.DIALOG_VEDIO_MODE);
  }else{
   
   if(checkbox[position-1].isChecked()){
    checkbox[position-1].setChecked(false);
   }else{
    checkbox[position-1].setChecked(true);
   }
//   更新listview
   mProfileAdapter.notifyDataSetChanged();
  }
 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值