Android: ListView Trouble with textview.isChecked() and textview.setChecked() - android

I have a listview which uses this layout:
android.R.layout.simple_list_item_checked
I have an OnItemClickListener that gets the check status of the clicked item and either checks or unchecks that item but it does not work. There are NO errors in logcat. When I click on an item in the listview simply nothing happens.
The OnItemClickListener looks like this:
//------------------- OnItemClickListener -----------------------------
lvCheckList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
CheckedTextView textview = (CheckedTextView)view;
if (!textview.isChecked()){
textview.setChecked(true);
}else {
textview.setChecked(false);
}
}
});

Try with this...
//sample code
TextView TxtName = (TextView) findViewById(R.id.NameControlId);
TxtName.setOnClickListener(this);
#Override
public void onClick(View view)
{
switch (view.getId())
{
case R.id.NameControlId:
Toast.makeText(getApplicationContext(),"click",Toast.LENGTH_SHORT).show();
break;
}
}

Related

spinner item selection inside button onclick not working

Below inside my adapter class as i will get data inside button click i want to populate spinner inside button click and set item selected listener in it.
tvMediaCategory.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
for(int i=0; i<mediaList.get(position).getMediaCatList().size(); i++)
{
catArr[i] = mediaList.get(position).getMediaCatList().get(i).getCategoryName();
}
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
for(int i=0; i<catArr.length; i++)
{adapter.add(catArr[i]);}
adapter.add("HINT_TEXT_HERE"); //This is the text that will be displayed as hint.
spinner.setAdapter(adapter);
spinner.setSelection(adapter.getCount()); //set the hint the default selection so it appears on launch.
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Log.v("item", (String) parent.getItemAtPosition(position));
Toast.makeText(context, "position clicked "+position, Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
tvMediaCategory.setVisibility(View.GONE);
spinner.setVisibility(View.VISIBLE);
spinner.performClick();
//showCategoryDlg(catArr, position, selectedPos, tvMediaCategory);
}
}
});
here my spineer not getting open but selecting default item. how to make it work. this my code is inside a adapter. how can i show my spinner items on click of button?
Try this example... i am sure you will be able to get what u r looking for....
you need to implement the onItemSelectedListener
https://www.mkyong.com/android/android-spinner-drop-down-list-example

Button on ListAdapter

I have button on items in my list adapter. The problem occurs when I click the button. The action is performed on the right item, but it disables wrong button.
My code:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater =
(LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.item_friend, parent, false);
user = userList.get(position);
ImageView imageView = (ImageView)view.findViewById(R.id.user_image);
Picasso.with(JSONUserAdapter.this.getContext()).load("https://s3.amazonaws.com/profiles-pictures/"+ user.getPath()).into(imageView);
sendRequest = (ImageButton)view.findViewById(R.id.sendRequest);
sendRequest.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sendFriendRequest();
disabledAdd();
}
});
return view;
}
Do not try to catch click events for the buttons inside the rows of ListView. Each row of ListView is already clickable. Set the onItemClickListener for the ListView:
listview.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> adapter, View v, int position, long l) {
//DO SOMETHING HERE
//Use "position" to get the index of item clicked
}
});
As i can see that we had defined the "User" object as a globel variable and it will store the last postions item data.
So when you will click on any button the last items data will be reflected.
{UserType}Object user = userList.get(position);
You can set Tag on View
Use like this:
sendRequest.setTag(user);
And When user clicks item let's get data from tag like this
sendRequest.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Here you have user that was clicked.
User user = v.getTag();
sendFriendRequest();
disabledAdd();
}
});

Set Action Listener on ImageView

Hey guys i am have a listview and it has an imageview inside of every row. what i want is when i tap/touch/click i want the checkbox to clicked like the gmail app.
my code so far is:
public void registerCallClickBack() {
ListView list = (ListView) findViewById(R.id.listView1);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, final View viewClicked,
int position, long id) {
TextView tv1 =(TextView)viewClicked.findViewById(R.id.stock_name);
TextView tv2 =(TextView)viewClicked.findViewById(R.id.stock_price);
Intent intent = new Intent(MainActivity.this, StockItem.class);
intent.putExtra("name", tv1.getText().toString());
intent.putExtra("price",tv2.getText().toString());
intent.putExtra("stockInfo", stocksList.get(position));
startActivity(intent);
ImageView imageCheck = (ImageView)viewClicked.findViewById(R.id.imagestartinglist);
imageCheck.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
chk = (CheckBox)viewClicked.findViewById(R.id.checkBoxMainList);
if (!chk.isShown()){
chk.setVisibility(CheckBox.VISIBLE);
if(!chk.isChecked()){
chk.setChecked(true);
}
}
}
});
}
});
}
i dont know what i am missing. and when i click on the image it checks the one i select and some others. how can i fix it??maybe with ontouch listener??
any help will be much appreciated and accepted!!!thanks in advance!!!

Delete ListView row in Custom Adapter

I want to delete a row from my list view on click of "delete" button. My listview item has following things placed horizontally: TextView-1,TextView-2,TextView-3,ImageButton-delete button. Now when I click delete button, the row should be deleted from the view. Below is the adapter code;
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
View view = (View) v.getParent();
TextView tv = (TextView) view.findViewById(R.id.item_name);
String item = tv.getText().toString();
String tableno = mListItems.get(0).getTableNumber();
orderDetailsDB = new OrderDetailsDBAdapter(
getApplicationContext());
orderDetailsDB.deleteItem(item,tableno);
I tried by setting Individual textviews to blank but its not working.
holder.itemName.setText("");
holder.amount.setText("");
holder.quantity.setText("");
I read couple of posts and they suggest to remove item from my list(mListItems) and then do adapter.notifyDataSetChanged();. Problem is I am not using array adapter for populating list view but using Custom adapter, so unable to get the position for item to be deleted. Please advise. thanks.
First write below line in your adapter's getView method.
button.setTag(position)
in onClick method
#Override
public void onClick(View v) {
int position = (Integer)v.getTag();
yourarraylistObject.remove(position);
// your remaining code
notifyDataSetChanged();
}
Just use remove() to remove list item from the adapter
for your reference
adapter = new MyListAdapter(this);
lv = (ListView) findViewById(android.R.id.list);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
AlertDialog.Builder adb=new AlertDialog.Builder(MyActivity.this);
adb.setTitle("Delete?");
adb.setMessage("Are you sure you want to delete " + position);
final int positionToRemove = position;
adb.setNegativeButton("Cancel", null);
adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
MyDataObject.remove(positionToRemove);
adapter.notifyDataSetChanged();
}});
adb.show();
}
});
What you can do in order to get the position that you want to delete is to pass that into your listener:
// inside custom adapter
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
.....
deleteButton.setOnClickListener(new MyClickListener(position);
}
private class MyClickListener implements OnClickListener
{
int position = -1;
public MyClickListener(final int position)
{
this.position = position;
}
#Override
public void onClick(View v) {
// do your delete code here
notifyDataSetChanged();
}

Android ListItem with radio button

My page has a listView and a OK button. Each listItem has an id and RadioButtonGroup.
I am using SimpleCursorAdapter to load the list.
What I would like to do is when I click on OK, I want to get individual id and radio button selection from that radioButtonGroup.
How do I do it ?
You can add a clicklistener to your list:
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
RadioButton button = (RadioButton) view.findViewById(R.id.RadioButton);
button.getId();
}
});
You have to extend the SimpleCursorAdapter class and inside the newView / bindView methods you need to check for the state of the radioButtonGroup. In order to do that you need to set a listener for your radio button group using the OnCheckedChangedListener. I have some source code example that I used on a personal app. Bare in mind that I'm listening to checkboxes but is should be perfectly straight forward to adapt to a radio button group.
#Override
public void bindView(View view, Context context, Cursor cursor) {
NoteHolder holder = (NoteHolder) view.getTag();
holder.titleView.setText(cursor.getString(titleColIndex));
holder.modifiedView.setText(mUtils.formatDate(mUtils.formatDateFromString (cursor.getString(modifiedColIndex), context, "dd-MM-yyyy"), "dd-MM-yyyy"));
holder.priorityView.setImageResource(mUtils.getPriorityResourceId(cursor.getInt(priorityColIndex)));
Typeface tf = Typeface.createFromAsset(context.getAssets(), "fonts/ARLRDBD.TTF");
holder.titleView.setTypeface(tf);
holder.modifiedView.setTypeface(tf);
final int pos = cursor.getPosition();
final long id = cursor.getLong(idColIndex);
holder.checkbox.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
checkChecked(v, pos, id);
}
});
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
rowCheckChecked(v, pos, id);
}
});
holder.checkbox.setChecked(itemChecked.get(pos));
}
private static class NoteHolder {
TextView titleView;
TextView modifiedView;
ImageView priorityView;
CheckBox checkbox;
}
private void checkChecked(View v, int pos, long id){
CheckBox cb = (CheckBox) v.findViewById(R.id.delete_checkbox);
if (cb.isChecked())
{
itemChecked.set(pos, true);
mSelectedIDs.add(id);
}
else if(!cb.isChecked())
{
itemChecked.set(pos, false);
if(mSelectedIDs.contains(id))
mSelectedIDs.remove(id);
}
}
I would advise you to use a viewHolder pattern too. Specially if your app is going to have many list items. You can read more about it here and see an example here, you can also see it implemented in the code I've put above.
Hope that helps.

Categories

Resources