I have a ListView in which i add row items dynamically. Each row has a no of childs. Now i am setting on click listeners on these childs, and in them it asks me to keep these child items final, it works fine with them and it is ok to do this as i am creating a class in each on click listener.
Here isFree is a final checkBox.
isFree.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
if (isFree.isChecked()){...}
}
});
But i dont feel it as a good programming practice, i want to use some function which should detect which child was clicked and then i should be able to access its row using getParent and then its sibling items using getChildAt(index)
So, i used this:
isFree.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
CheckBox tv=(CheckBox) getCurrentFocus();
TableRow row = (TableRow)tv.getParent();
TextView tv1=(TextView) row.getChildAt(5);
if (tv.isChecked()){...}
}
});
But when i use it, on focussed item actualy returns the listview, but i want focused item in the row.
you can get which child of listview was clicked with onItemClickListener, but if you want to do some action on a row which has some checkbox or some other clickable view and you want to perform some action on those click then i think you have to use click listeners on those views itself.
but if you want to get listitem row at click of those individual views, you can use setTag method of view class to store the index and then getting the row item from that index on your click listeners.
public view getView(int position, View convertView, ..) {
....
....
....
// your code
convertView.setTag(position);
return convertView;
}
isFree.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
if (arg0.isChecked()){
int index = arg0.getTag();
View v = listview.getItemAtPosition(index);
// or get the adapter get data list from it and then index it.
}
}
});
NOTE that: I havent used final free inside oncheckedchange method.
On the setOnCheckedChangeListener method, the first parameter is a CompoundButton that is a super-class of CheckBox. So, actually, arg0 is your checkbox. Use it to retrieve the parent list item and make your stuff.
isFree.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
TableRow row = (TableRow)arg0.getParent();
TextView tv1=(TextView) row.getChildAt(5);
if (arg0.isChecked()){...}
}
});
Related
I am creating a sample application containing an Activity [having ListView and Button] in layout file. ListView is custom containing [Label/Name and CheckBox]. I want to write some code which will change text of the Button from adapter class of ListView based on List Item CheckBox check [T/F].
listView.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
// When clicked, show a toast with the TextView text
AppListOfAllApps Selecteditems = (AppListOfAllApps) parent.getItemAtPosition(position);
if (view != null)
{
CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkBox1);
Selecteditems = (AppListOfAllApps) checkBox.getTag();
//here you will get the selected item you may also get the text from it accordingly and then using using button variable just set text
button.settext("whatever");
}
}
});
In Activity :
public class Your_Activity extends Activity implements OnCheckListener// Implement your listener here
#Override
public void OnCheck(int position) {
// TODO Auto-generated method stub
// notify your activity component here
}
In Adapter class :
private OnCheckListener listener;
public interface OnCheckListener {
public void OnCheck(int position);
}
public Your_adapter_constructor(OnCheckListener listener) {
// TODO Auto-generated constructor stub
this.listener = listener;
}
// On your getView()
checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
listener.OnCheck(position);// If you want to pass some value add it here
}
});
Let's say I have a spinner(with 2 items) and a radiogroup (with 2 radiobuttons).
I want to show for example a TextView in my layout which will be with different values for every choice:
For example we have:
Spinner
Male
Female
RadioGroup
True False
So we have 4 cases:
Male True
Female False
Male False
Female True
In short, each time user selects any of these choices, I want to show a different textview.
I have placed event listeners on spinners and radiobuttons:
Spinner Listener
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View arg1,
int arg2, long arg3) {
``// TODO Auto-generated method stub
}
Radio Group Listener
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
//switch checkedId and check which spinner item is selected...
//then show the textview
}
But this sometimes works sometimes not- I am having many and different problems.
I also tried similar choices. But nothing worked preperly.
How can dynamically handle this without adding an extra button?
Hopefully this better explains my comment.
int selectedSpinnerItem = -1;
int selectedRadioGroupItem = -1;
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
selectedSpinnerItem = position;
updateViews();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
selectedSpinnerItem = -1;
updateViews();
}
});
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
selectedRadioGroupItem = checkedId;
updateViews();
}
});
public void updateViews(){
// Here is where you will compare the values
// and display the appropriate views.
}
You can create all TextViews you want..
Than just change their visibilty..
TextView v = (TextView) findViewById(R.id.text1);
v.setVisibility(View.GONE); // Or view.VISIBLE, View.INVISIBLE
So you can visible the specific text view you want according to your logic.
Or you can just create 2 textViews.. One will be for Male/Female and one for True/False;
I think the better thing to do is just change the text in on general textView.. But i am not sure what is the problem because i don't have code example that can help me understand
Hope this helps :)
i have a expandable list view in my app, each group has a textview say T having dynamic value and each group has a single child within it. I want to refer to that T textview object of that Group whose child has been clicked on so that i can change the value of T accordingly as per the child item clicked.
I can easily get the TextView Object in case of groupClickListener but iam not able to get in case of childClickListener... pls see both here.
mExpandableListView.setOnGroupClickListener(new OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// TODO Auto-generated method stub
TextView moreTextView = (TextView)v.findViewById(R.id.group_more); // this works fine
}
return false;
}
});
mExpandableListView.setOnChildClickListener(new OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
boolean isExpanded=false;
if(groupStatus[groupPosition]==1)
{
isExpanded=true;
}
View v1 = parent.getExpandableListAdapter().getGroupView(groupPosition, isExpanded, v, parent);
TextView moreTextView = (TextView)v1.findViewById(R.id.group_more); // doesn,t work as v1 is null all the times
return false;
}
});
// Listview on child click listener
expListView.setOnChildClickListener(new OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
Toast.makeText(
getApplicationContext(),
listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();
return false;
}
});
I hope its useful for you.
Use the getExpandableListAdapter() on the "parent" and define your custom classes accordingly.
Then modify the values within the custom objects - the ExpandableListView will update automatically.
E.g.:
#Override
public boolean onChildClick(ExpandableListView _parent, View _v,
int _groupPosition, int _childPosition, long _id
) {
// do required casts for your custom objects for groups and childs.
// In my case, I'm using class "Group"
Group group=(Group)parent.getExpandableListAdapter().getGroup(_groupPosition);
// In my case, I'm using class "Child"
Child item=(Child)optionsList.getExpandableListAdapter().getChild(_groupPosition, _childPosition);
// In my custom Group class, I've defined a field that populates a different
// TextView within the Group layout - so, getter/setter method for "selection" were defined.
// The ExpandableListView will be updated automatically based on the adapter updates.
group.setSelection(item.getName());
return true;
}
Not exactly sure why this happens - it seems like the ExpandableAdapter is tracking changes to the model itself... I don't have time to check the details on this.. but it works.
I have a listview and each item of the lisview is a linearlayout.
Each one of the linearlayouts contains 3 textviews.
How do i set a onclicklistener for those textviews?
i tried this:
TextView tv=(TextView)findById(R.id.textView1);
tv.setOnClickListener(...);
This throws me a nullpointerexception.
I also tried setonitemclickedlistener for the listview,but this only allows me to operate on the linearlayout,not the textview.
thanks in advance.
If this is needed statically and your view is XML based, this is what I did:
<TextView
...
android:clickable="true"
android:onClick="myHandler"
/>
This calls myHandler whenever the textview is touched/clicked. As you are using this in a list view, you will still need to add a tag in getView() and use that in myHandler() to figure out which row/field were pressed.
Hope this helps.
For getting this requirement you must you use custom adapter class, so that you can get this very easily. using custom adapter is very easy and simple process.
click on this link, its simple application, and In place of Buttons you can use TextView.
Have a nice day..
you should go for creating custome adpter like Use BaseAdpter
and here pass your list and set that list according to position to Textview and here you can set onClick event also
you can create base Adpter like ex
(1)
public class GridAdpter extends BaseAdapter
{
List<String> checkednamelist;
public GridAdpter(Context c, List<String> myitem) {
this.checkednamelist =myitem
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//Here change with yur row xml file name and 3 textview control id name
View grid;
if (convertView == null) {
grid = layoutInflater.inflate(R.layout.row_grid, null);
} else {
grid = convertView;
}
TextView textView2 = (TextView) grid.findViewById(R.id.txtlable2);
textView.setText(checkednamelist.get(position);
textView2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// do here as per your require
}
});
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return myitem.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return myitem.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public List<String> mycheckeditem() {
return checkednamelist;
}
}
// finally set this adpter with your listview
I know, similar questions (ListView selected items are messed up when scroll the ListView...) are asked before and the answer is doing operation in onItemClick, not in getView.
My situation is a little bit complicated. I have a custom view in each ListView row. This custom view has 5 ImageViews. So I catch onClick event of these ImageViews and change drawable image. In this case, I don't know how to move this operation to onItemClick. Any ideas?
do like this. you can change the color or background of selected item on listview. use this code in getView() method.
m_Button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
indexClick = position;
indexClickFlag = true;
notifyDataSetChanged();
Log.i("in click--------->", "" + position);
Intent i = new Intent(ArtistPlaylistActivity.this,
PlayerMainActivity.class);
i.putExtra("songnameIndexinPlaylist", position);
startActivity(i);
ArtistPlaylistActivity.this.finish();
}
});
if (indexClickFlag) {
if ((indexClick) == (position)) {
itemLayout.setBackgroundResource(R.drawable.tab_select);
Log.d("System out", "iclick" + indexClick + "pos"
+ position);
}
}
where, int indexClick ; boolean indexClickFlag = false;
itemLayout -- > is layout that background that you want to change and m_Button.
Hope helpful to you.
You can get the Child from the view using getChildAt(index) method. put the index of image view to get the image view object
Sample Code
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
System.out.println(".....view..."+arg1);
LinearLayout ll =(LinearLayout) arg1;
ImageView imageView = (ImageView) ll.getChildAt(0);
imageView.setImageDrawable(drawable);
}