I have a listview that have buttons in every row and is connected to my SQLite database.
Here's the code:
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;
}
}
Whenever I click the Button in the second row in my listview, the button in the first row changes its text. In short, The if-condition is only applicable in the first row.
Every button must change its text into "ON" or "OFF" everytime I click them AND saves their texts in the SQLite database in their corresponding row.
Screenshot:
Aucun commentaire:
Enregistrer un commentaire