NormarActivity 아래에 있는 MyListAdapter에

setOnclickListener로 클릭할때마다 DB에 값이 바뀌게 하고싶습니다.

그런데.. 여기서 전달과정을 어떻게 해주어야할지 막히네요 ㅜ

 

NormarActivity에서 DB생성과 커서,쿼리가 다 있어서..

아래에서 그 DB값을 어떻게 변경을 시켜줘야할지 모르겠습니다....

 

이 소스에서는 MyListAdapter에 있는 btn_chk라는 int변수를

DB에 chk변수로 클릭할때마다 맞춰서 바꾸어주고싶습니다.

 

조언부탁드려요

 

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ

 

package com.api;

public class NormalActivity extends Activity {
 ArrayList arItem;
 ArrayList<MyItem> arSrc2; 
 private SQLiteOpenHelper mOpenHelper;
 private SQLiteDatabase db;
 private DatabaseHelper dbHelper;
 private static final String MY_CARDB_NAME = "car.db";
 private static final String MY_CARDB_TABLE = "car_table";
 private static final int MY_CARDB_VERSION = 2;


 private class DatabaseHelper extends SQLiteOpenHelper{
  public DatabaseHelper(Context context) {
   super(context, MY_CARDB_NAME , null,MY_CARDB_VERSION);
  }  

  @Override
  public void onCreate(SQLiteDatabase db) {   
   try{
    db.execSQL("CREATE TABLE " + MY_CARDB_TABLE + "(" +
      "_id INTEGER PRIMARY KEY autoincrement ," +
      "no_car INT, " +
      "chk INT, " +
      "name VARCHAR(20), " +
      "car_num VARCHAR(10) );"  );
   }catch(Exception ex){
    ex.printStackTrace();
   }
  }
  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int currentVersion) {
   db.execSQL("DROP TABLE IF EXISTS " + MY_CARDB_TABLE);
   onCreate(db);  
  }
 } 
 @Override
 public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_normal);
  dbHelper = new DatabaseHelper(this);
  db = dbHelper.getWritableDatabase();  
  String update = "select no_car, chk, name, car_num " + " from " + MY_CARDB_TABLE;
  Cursor c1 = db.rawQuery(update, null);
  int recordCount = c1.getCount();
  Intent add_car = new Intent(NormalActivity.this, AddCarActivity.class);
  Button btn_add = (Button)findViewById(R.id.car_add);
  btn_add.setOnClickListener(new Button.OnClickListener(){
   @Override
   public void onClick(View v) {    
    Intent add_car = new Intent(NormalActivity.this, AddCarActivity.class); 
    startActivity(add_car);
   }   
  });
  Intent i = getIntent();
  String carnumber = i.getStringExtra("car_num");
  arItem = new ArrayList<MyItem>();
  MyItem mi;
  for ( int a = 0 ; a < recordCount ; a++){
   c1.moveToNext();
   int no = c1.getInt(0);
   int chk = c1.getInt(1);
   String model = c1.getString(2);
   String car_num = c1.getString(3);   
      
   if(model.equals("K5")){
    mi = new MyItem(no,R.drawable.k5, "K5", car_num, chk);
    arItem.add(mi);                  
   }
   if(model.equals("YF SONATA")){
    mi = new MyItem(no, R.drawable.yf, "YF SONATA", car_num, chk);
    arItem.add(mi);                  
   }
   if(model.equals("MALIBU")){
    mi = new MyItem(no, R.drawable.malibu, "MALIBU", car_num, chk);
    arItem.add(mi);                  
   }
   if(model.equals("MORNING")){
    mi = new MyItem(no, R.drawable.morning, "MORNING", car_num, chk);
    arItem.add(mi);                  
   }
   if(model.equals("SPARK")){
    mi = new MyItem(no, R.drawable.spark, "SPARK", car_num, chk);
    arItem.add(mi);                  
   }
   if(model.equals("STAREX")){
    mi = new MyItem(no, R.drawable.starex, "STAREX", car_num, chk);
    arItem.add(mi);                  
   }
  }
  MyListAdapter MyAdapter = new MyListAdapter(this, R.layout.item, arItem); 
  ListView myList;
  myList = (ListView)findViewById(R.id.list);
  myList.setAdapter(MyAdapter);
 }
}


class MyItem{
 int no;
 int icon;
 String name;
 String car_num;
 int check;
 public MyItem(int aNo, int aIcon, String aName, String aCar_num, int aCheck){
  no = aNo;
  icon = aIcon;  
  name = aName;
  car_num = aCar_num;
  check = aCheck;
 } 
}

class MyItem2{
 int no2; 
 String car_num2;
 int check2;
 public MyItem2(int aNo2, String aCar_num2, int aCheck2){
  no2 = aNo2;
  car_num2 = aCar_num2;
  check2 = aCheck2;
 } 
}

class MyListAdapter extends BaseAdapter{
 Context ctx;
 LayoutInflater Inflater;
 ArrayList<MyItem> arSrc; 
 private int btn_chk;
 private int num;
 int layout; // 작은 뷰에 대한 layout
 public MyListAdapter
 (Context context, int alayout, ArrayList<MyItem> aarSrc){
  ctx = context;
  Inflater = (LayoutInflater)context.getSystemService(
    Context.LAYOUT_INFLATER_SERVICE);

  arSrc = aarSrc;
  layout = alayout;
 }

 @Override
 public int getCount() {  
  return arSrc.size();
 }

 @Override
 public Object getItem(int position) {  
  return arSrc.get(position).name;
 }
 @Override
 public long getItemId(int position) {
  return position;
 }
 @Override
 public View getView(final int position, View convertView, ViewGroup parent) {
  
  if(convertView == null){
   convertView = Inflater.inflate(layout, parent, false);
   //Inflater.inflate
   //(int resource, ViewGroup root, boolean attachToRoot)
  }
  ImageView img = (ImageView)convertView.findViewById(R.id.img);
  img.setImageResource(arSrc.get(position).icon);

  TextView txt = (TextView)convertView.findViewById(R.id.text);
  TextView txt2 = (TextView)convertView.findViewById(R.id.text2);
  
  txt.setText(arSrc.get(position).name );
  txt2.setText(arSrc.get(position).car_num);
  final Button btn = (Button)convertView.findViewById(R.id.btn);
  if(arSrc.get(position).check == 0){
   btn_chk = 0;
  }else{
   btn_chk = 1;
  }
  
  btn.setOnClickListener(new Button.OnClickListener(){
     

   @Override
   public void onClick(View v) {
            
    if(btn_chk==0)
    {
     final String str = arSrc.get(position).name;   
    
     
     if(str.equals("k5")){
      new AlertDialog.Builder(ctx).setTitle("KIA K5").setMessage("<KIA K5> / <5 People> / <80000W / day> / <Gasoline>")
      .setNeutralButton("RENT", new DialogInterface.OnClickListener() {
       
       @Override
       public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub
        btn_chk = 1;
        btn.setText("OFF");
        btn.setBackgroundColor(Color.RED);
        Toast.makeText(ctx, str, Toast.LENGTH_SHORT).show();             
       }
      })
      .setNegativeButton("NO", new DialogInterface.OnClickListener() {
       
       @Override
       public void onClick(DialogInterface dialog, int which) {
             
       }
      }).show();

     }
     if(str.equals("YF SONATA")){
      new AlertDialog.Builder(ctx).setTitle("HYUNDAI YF SONATA").setMessage("<KIA K5> / <5 People> / <80000W / day> / <Gasoline>")
      .setNeutralButton("RENT", new DialogInterface.OnClickListener() {
       
       @Override
       public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub
        btn_chk = 1;
        btn.setText("OFF");
        btn.setBackgroundColor(Color.RED);
        Toast.makeText(ctx, str, Toast.LENGTH_SHORT).show();             
       }
      })
      .setNegativeButton("NO", new DialogInterface.OnClickListener() {
       
       @Override
       public void onClick(DialogInterface dialog, int which) {
             
       }
      }).show();

     }
     if(str.equals("MALIBU")){
      new AlertDialog.Builder(ctx).setTitle("CHEVROLET MALIBU").setMessage("<CHEVROLET MALIBU> / <5 People> / <100000W / day> / <Gasoline>")
      .setNeutralButton("RENT", new DialogInterface.OnClickListener() {       
       @Override
       public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub
        btn_chk = 1;
        btn.setText("OFF");
        btn.setBackgroundColor(Color.RED);
        Toast.makeText(ctx, str, Toast.LENGTH_SHORT).show();             
       }
      })
      .setNegativeButton("NO", new DialogInterface.OnClickListener() {
       
       @Override
       public void onClick(DialogInterface dialog, int which) {
        
       }
      }).show();
     }
     if(str.equals("MORNING")){
      new AlertDialog.Builder(ctx).setTitle("KIA MORNING").setMessage("<KIA MORNING> / <5 People> / <50000W / day> / <Gasoline>")
      .setNeutralButton("RENT", new DialogInterface.OnClickListener() {
       
       @Override
       public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub
        btn_chk = 1;
        btn.setText("OFF");
        btn.setBackgroundColor(Color.RED);
        Toast.makeText(ctx, str, Toast.LENGTH_SHORT).show();             
       }
      })
      .setNegativeButton("NO", new DialogInterface.OnClickListener() {
       
       @Override
       public void onClick(DialogInterface dialog, int which) {
       }
      }).show();
     }
     if(str.equals("STAREX")){
      new AlertDialog.Builder(ctx).setTitle("HYUNDAI STAREX").setMessage("<HYUNDAI STAREX> / <12 People> / <150000W / day> / <Diesel>")
      .setNeutralButton("RENT", new DialogInterface.OnClickListener() {
       
       @Override
       public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub
        btn_chk = 1;
        btn.setText("OFF");
        btn.setBackgroundColor(Color.RED);
        Toast.makeText(ctx, str, Toast.LENGTH_SHORT).show();             
       }
      })
      .setNegativeButton("NO", new DialogInterface.OnClickListener() {
       
       @Override
       public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub
             
       }
      }).show();
     }
     if(str.equals("SPARK")){
      new AlertDialog.Builder(ctx).setTitle("CHEVROLET SPARK").setMessage("<CHEVROLET SPARK> / <5 People> / <50000W / day> / <Gasoline>")
      .setNeutralButton("RENT", new DialogInterface.OnClickListener() {
       
       @Override
       public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub
        btn_chk = 1;
        btn.setText("OFF");
        btn.setBackgroundColor(Color.RED);
        Toast.makeText(ctx, str, Toast.LENGTH_SHORT).show();             
       }
      })
      .setNegativeButton("NO", new DialogInterface.OnClickListener() {       
       @Override
       public void onClick(DialogInterface dialog, int which) {
       }
      }).show();
     }
    }
    else
    {  
     final String str = arSrc.get(position).name;
     
     new AlertDialog.Builder(ctx).setTitle("RETURN").setMessage("Return?")
     .setNeutralButton("OK", new DialogInterface.OnClickListener() {
      
      @Override
      public void onClick(DialogInterface dialog, int which) {
       // TODO Auto-generated method stub
       btn_chk = 0;
       btn.setText("ON");
       btn.setBackgroundColor(Color.parseColor("#FFFFFF"));
       
       Toast.makeText(ctx, str, Toast.LENGTH_SHORT).show();            
      }
     })
     .setNegativeButton("NO", new DialogInterface.OnClickListener() {
      
      @Override
      public void onClick(DialogInterface dialog, int which) {               
      }
     }).show();     
    }
   }
  });
  return convertView;
 }
}