2015-01-28 3 views
0

모든 행에 버튼이 있고 SQLite 데이터베이스에 연결된 목록 뷰가 있습니다. 내 목록보기에서 두 번째 행의 버튼을 클릭 할 때마다SQLite 데이터베이스에 연결된리스트 뷰에 버튼의 텍스트 저장

public class UpdateActivity extends Activity implements AdapterView.OnItemClickListener, View.OnClickListener { 

ListView listView; 
SimpleCursorAdapter dataAdapter; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_update); 
    listView = (ListView)findViewById(R.id.listView1); 

    Appliances db = new Appliances(getApplicationContext()); 


    Cursor cursor = db.getAllApp(); 
    // The desired columns to be bound 
    String[] columns = new String[] { Appliances.KEY_APPNAME,Appliances.KEY_ONOFF }; 

    // the XML defined views which the data will be bound to 
    int[] to = new int[] { R.id.text, R.id.right}; 

    // create the adapter using the cursor pointing to the desired data 
    // as well as the layout information 
    dataAdapter = new SimpleCursorAdapter(this, R.layout.row_view, 
      cursor, columns, to, 0) { 
     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      View row = super.getView(position, convertView, parent); 



      View right = row.findViewById(R.id.right); 
      right.setTag(position); 
      right.setOnClickListener(UpdateActivity.this); 

      return row; 
     } 
    }; 



    listView.setAdapter(dataAdapter); 
    listView.setOnItemClickListener(this); 

    EditText myFilter = (EditText) findViewById(R.id.myFilter); 
    myFilter.addTextChangedListener(new TextWatcher() { 

     public void afterTextChanged(Editable s) { 
     } 

     public void beforeTextChanged(CharSequence s, int start, int count, 
       int after) { 
     } 

     public void onTextChanged(CharSequence s, int start, int before, 
       int count) { 
      dataAdapter.getFilter().filter(s.toString()); 
     } 
    }); 

    dataAdapter.setFilterQueryProvider(new FilterQueryProvider() { 
     public Cursor runQuery(CharSequence constraint) { 
      Appliances db = new Appliances(getApplicationContext()); 
       return db.getAppliancesByName(constraint.toString()); 

     } 
    }); 
} 

    public void onClick(View v) { 
     switch(v.getId()) { 
     case R.id.right: 
      Button tog = (Button)findViewById(R.id.right); 
      Appliances db = new Appliances(getApplicationContext()); 
      Toast.makeText(this, "Right Accessory "+v.getTag(), Toast.LENGTH_SHORT).show(); 
      Integer i = (Integer) v.getTag(); 
      if(tog.getText().toString().equals("ON")){ 
       //db.update_on(i, "ON"); 
       tog.setText("OFF"); 
      }else if(tog.getText().toString().equals("OFF")){ 
      // db.update_on(i, "OFF"); 
      tog.setText("ON"); 
      } 

      break; 
     default: 
      break; 
     } 
    } 

는, 첫 번째 행에있는 버튼은 텍스트를 변경 :

여기에 코드입니다. 즉, if-condition은 첫 번째 행에만 적용됩니다.

모든 버튼은 클릭 할 때마다 텍스트를 "켜짐"또는 "꺼짐"으로 변경하고 해당 행의 SQLite 데이터베이스에 텍스트를 저장해야합니다.

스크린 샷 : Screenshot

답변

0

내가 강하게 때문에 신분증이라고 생각 ..하지만 내가 완전히 잘못 될 수있다.

Repalce이 코드 :

Button tog = (Button)findViewById(R.id.right); 

이미 해당 뷰에 ID를 할당하고 온 클릭 방법에 있음을 통과 한 Becuase

Button tog = (Button)v 

로하고 다음 할당이 버튼을 의미 귀하의 주요 레이아웃에서.

관련 문제