I've populated a Listview, adapting data from database. And of course per list item have same layout. Inside this layout there are some buttons, checkboxs and other widgets.
Now I want to set onClickListenser. How to achieve that?
Here is the overview:
What I have:
private void populateListView(){
DatabaseHandler db = new DatabaseHandler(this);
Cursor alarms = db.getAllAlarms();
startManagingCursor(alarms);
String[] time = new String[]{DatabaseHandler.KEY_TIME};
int[] toViewIDs = new int[]{R.id.timeTitle};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
this,
R.layout.per_alarm_activity,
alarms,
time,
toViewIDs
);
ListView listView = (ListView) findViewById(R.id.allAlarmsView);
listView.setAdapter(adapter);
mTimeView = (TextView) findViewById(R.id.timeTitle);
mAmPmTextView = (TextView) findViewById(R.id.amPmTextView);
mAlarmOnOff = (Switch) findViewById(R.id.alarmOnOff);
mRepeatCheck = (CheckBox) findViewById(R.id.repeatCheckBox);
mDrop = (ImageView) findViewById(R.id.arrowDown);
mUp = (ImageView) findViewById(R.id.arrowUp);
}
Try this
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
CheckBox mRepeatCheck =((CheckBox)view.findViewById(R.id.repeatCheckBox));
//same code goes for othe buttons, imageview etc
}
});
Related
I have a listview populated from a table with a simple adapter. Now, I can change the value of a textview if I click the list item, like this:
ArrayList<HashMap<String, String>> userList = controller.getOld();
// If users exists in SQLite DB
if (userList.size() != 0) {
// Set the User Array list in ListView
ListAdapter adapter = new SimpleAdapter(OldTips.this, userList, R.layout.old_tips, new String[] {
"userId", "tara","ora_meci" ,"echipa_acasa","echipa_deplasare","cota","pronostic","explicatii","rezultat"}, new int[] { R.id.userId, R.id.tara , R.id.ora_meci, R.id.echipa_acasa, R.id.echipa_deplasare, R.id.cota,R.id.pronostic,R.id.explicatii,R.id.rezultat});
final ListView myList = (ListView) findViewById(android.R.id.list);
myList.setAdapter(adapter);
myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// Object obj = myList.getAdapter().getItem(position);
// String value= obj.toString();
TextView textView = (TextView) view.findViewById(R.id.echipa_acasa);
// String nume_campanie = textView.getText().toString();
textView.setText("value of header text");
}
});
Question: is it possible to change the textview value onload or something like this and not onclick of the item from the listview?
I have made an android SQLite database with two tables, each of them are performing in separate activities. And I'm trying to put them in the same listview.
This is my activity where the listview is.
public class MainActivity extends ActionBarActivity {
ListView lv;
SQLController dbcon;
TextView incomeID_tv, incomePayer_tv, incomeAmount_tv;
TextView incomeDate_tv, incomeCategory_tv, incomePayments_tv;
TextView expenseID_tv, expensePayee_tv, expenseAmount_tv;
TextView expenseDate_tv, expenseCategory_tv, expensePayments_tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbcon = new SQLController(this);
dbcon.open();
lv = (ListView) findViewById(R.id.incomeList_id);
Cursor cursor = dbcon.readIncomeData();
String[] from = new String[] { DBHelper.INCOME_ID, DBHelper.INCOME_AMOUNT, DBHelper.INCOME_PAYER,
DBHelper.INCOME_DATE, DBHelper.INCOME_CATEGORY, DBHelper.INCOME_PAYMENTS};
int[] to = new int[] { R.id.income_id, R.id.income_amount, R.id.income_payer, R.id.income_date,
R.id.income_category, R.id.income_payments};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
MainActivity.this, R.layout.income_entry, cursor, from, to);
adapter.notifyDataSetChanged();
lv.setAdapter(adapter);
Cursor ecursor = dbcon.readExpenseData();
String[] efrom = new String[] { DBHelper.EXPENSE_ID, DBHelper.EXPENSE_AMOUNT, DBHelper.EXPENSE_PAYEE,
DBHelper.EXPENSE_DATE, DBHelper.EXPENSE_CATEGORY, DBHelper.EXPENSE_PAYMENTS};
int[] eto = new int[] { R.id.expense_id, R.id.expense_amount, R.id.expense_payee, R.id.expense_date,
R.id.expense_category, R.id.expense_payments};
SimpleCursorAdapter eadapter = new SimpleCursorAdapter(
MainActivity.this, R.layout.expense_entry, ecursor, efrom, eto);
eadapter.notifyDataSetChanged();
lv.setAdapter(eadapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
incomeID_tv = (TextView) view.findViewById(R.id.income_id);
incomeAmount_tv = (TextView) view.findViewById(R.id.income_amount);
incomePayer_tv = (TextView) view.findViewById(R.id.income_payer);
incomeDate_tv = (TextView) view.findViewById(R.id.income_date);
incomeCategory_tv = (TextView) view.findViewById(R.id.income_category);
incomePayments_tv = (TextView) view.findViewById(R.id.income_payments);
String incomeID = incomeID_tv.getText().toString();
String incomeAmount = incomeAmount_tv.getText().toString();
String incomePayer = incomePayer_tv.getText().toString();
String incomeDate = incomeDate_tv.getText().toString();
String incomeCategory = incomeCategory_tv.getText().toString();
String incomePayments = incomePayments_tv.getText().toString();
Intent modify_intent = new Intent(getApplicationContext(),
IncomeEdit.class);
modify_intent.putExtra("incomeID", incomeID);
modify_intent.putExtra("newIncomeAmount", incomeAmount);
modify_intent.putExtra("newIncomePayer", incomePayer);
modify_intent.putExtra("newIncomeDate", incomeDate);
modify_intent.putExtra("newIncomeCategory", incomeCategory);
modify_intent.putExtra("newIncomePayments", incomePayments);
startActivity(modify_intent);
}
});
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
expenseID_tv = (TextView) view.findViewById(R.id.expense_id);
expenseAmount_tv = (TextView) view.findViewById(R.id.expense_amount);
expensePayee_tv = (TextView) view.findViewById(R.id.expense_payee);
expenseDate_tv = (TextView) view.findViewById(R.id.expense_date);
expenseCategory_tv = (TextView) view.findViewById(R.id.expense_category);
expensePayments_tv = (TextView) view.findViewById(R.id.expense_payments);
String expenseID = expenseID_tv.getText().toString();
String expenseAmount = expenseAmount_tv.getText().toString();
String expensePayer = expensePayee_tv.getText().toString();
String expenseDate = expenseDate_tv.getText().toString();
String expenseCategory = expenseCategory_tv.getText().toString();
String expensePayments = expensePayments_tv.getText().toString();
Intent modify_intent = new Intent(getApplicationContext(),
ExpenseEdit.class);
modify_intent.putExtra("expenseID", expenseID);
modify_intent.putExtra("newExpenseAmount", expenseAmount);
modify_intent.putExtra("newExpensePayee", expensePayer);
modify_intent.putExtra("newExpenseDate", expenseDate);
modify_intent.putExtra("newExpenseCategory", expenseCategory);
modify_intent.putExtra("newExpensePayments", expensePayments);
startActivity(modify_intent);
}
});
}
The problem is that it shows just the expenses. I think the problem is from the adapter, but I don't know how to include both of them in the same adapter. :)
set doesn't mean add. If you setAdapter it means the old one (if any) will be replaced. The same goes for the OnItemClickListener.
If the cursors had the same column names, you could combine them however they differ and the adapter has no means to distinguish between the data of the 2 cursors (expenses and incomes).
Furthermore each click listener has type-specific actions like putting specific intent extras. This just means that you want multiple adapters therefore multiple ListView's.
You need to make your table and app design more generic or continue using 2 ListViews.
So, this might be a simple question, or I may be doing things totally off here, but here's what I have:
public class SetPrefsActivity extends ListActivity{
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.radiolist);
ArrayList<Hall> listItems = new ArrayList<Hall>();
ArrayAdapter<Hall> ar = new ArrayAdapter<Hall>(this, android.R.layout.simple_list_item_single_choice, listItems);
setListAdapter(ar);
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener()
{
boolean somethingChecked = false;
int lastChecked;
public void onItemClick(AdapterView arg0, View arg1, int arg2,
long arg3) {
if(somethingChecked){
ListView lv = (ListView) arg0;
TextView tv = (TextView) lv.getChildAt(lastChecked);
CheckedTextView cv = (CheckedTextView) tv;
cv.setChecked(false);
}
ListView lv = (ListView) arg0;
TextView tv = (TextView) lv.getChildAt(arg2);
CheckedTextView cv = (CheckedTextView) tv;
if(!cv.isChecked())
cv.setChecked(true);
lastChecked = arg2;
somethingChecked=true;
}
});
new LoadListTask().execute(); //This loads Items into the List
Button b = (Button) findViewById(R.id.savePrefBtn);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//IF SOMETHING IS SELECTED,
//THEN UPDATE SHARED PREFERENCES
Intent i = new Intent(SetPrefsActivity.this, SomeOtherActivity.class);
startActivity(i);
}
}
});
}
//other stuff to fill the arrayAdapter
}
What I want to do, is:
When someone clicks the button, it gets information from the listview and updates a shared preference according to the radio option that's selected.
What I'm having trouble with is getting the index of the currently selected item. What is the best way to retrieve that information? Am I implementing the single choice list completely wrong?
Thank You!
arg2 is the position in your list.
It looks like you are doing some extra (unnecessary) processing. arg1 is your row view, and since the android.R.layout.simple_list_item_single_choice layout contains only a CheckedTextView, you can use that directly without having to look for it.
CheckedTextView cv = (CheckedTextView) arg1;
I have created a list. And I need to get the text on the list item, when it is clicked. Then that text need to be set in a TextView. Following is my code and i get a force stop when I run it. Please give some ideas.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtTask = (EditText)findViewById(R.id.txtTask);
btnAdd = (Button)findViewById(R.id.btnAddTask);
selectedTask = (TextView)findViewById(R.id.textViewTask);
list = getListView();
list.setTextFilterEnabled(true);
btnAdd.setOnClickListener(this);
list.setOnKeyListener(this);
toDoItems = new ArrayList<String>();
oo = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, toDoItems);
list.setAdapter(oo);
list.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id3) {
int tmp = list.getSelectedItemPosition();
String v= toDoItems.get(tmp).toString();
selectedTask.setTag(v);
flippy.showNext();
}
});
}
Replace below 3 lines of your code in onItemClick method with my suggested code.
int tmp = list.getSelectedItemPosition();
String v= toDoItems.get(tmp).toString();
selectedTask.setTag(v);
Suggested Code
String v= toDoItems[position]; // or
String v = list.getItemAtPosition(position).toString();
selectedTask.setText(v);
After you have got the string v, you need to put the following line :
selectedTask.setText(v);
Also there is no need to put list.setOnKeyListener(this); since you need to listen for the item being clicked.
I'm trying to have another activity launch when a list item gets clicked. Below is my code:
public class AvoidForeclosure extends CustomListTitle {
/** Called when the activity is first created. */
private DbAdapter db;
private SimpleCursorAdapter clients;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView list = getListView();
setContentView(R.layout.main);
this.title.setText("Avoid Foreclosure");
db = new DbAdapter(this);
db.open();
fillData();
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick( AdapterView<?> parent, View view, int position, long id ) {
int viewId = view.getId();
TextView theView = (TextView) findViewById(viewId);
String name = theView.getText().toString();
Cursor clientData = db.getClientByName(name);
Intent intent = new Intent();
intent.setClass(view.getContext(), CurrentMarketValue.class);
intent.putExtra("clientId", clientData.getInt(0));
startActivity(intent);
}
});
}
private void fillData() {
// Get all of the notes from the database and create the item list
Cursor c = db.fetchAllClients();
startManagingCursor(c);
String[] from = new String[] { DbAdapter.KEY_NAME };
int[] to = new int[] { R.id.text1 };
// Now create an array adapter and set it to display using our row
clients = new SimpleCursorAdapter(this, R.layout.clientsrow, c, from, to);
setListAdapter(clients);
}
}
Yet when I click it, nothing happens at all. Any ideas?
Try switching this line:
ListView list = getListView();
to be after this one:
setContentView(R.layout.main);
Otherwise you'll be getting a handle to the ListActivity's default layout's ListView, rather than R.layout.main's listview (which is the one you really want).