Modfiying inflated inflated view in a dialogfragment after calling show() - android

Let's say you have a dialogfragment with a listView. The title of the dialog is a custom view which is inflated in onCreateDialog(). Is it possible to toggle visibility of the inflated view when the list item is clicked.

In the onCreateDialog() function, you can hold the dialog view which referencing to title view which you want to toggle on.
On click event of listview.
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
//Toggle your title here
}
});

Related

Get id / position of ListView row

I have an Android ListView with a bunch of rows. Each row contains an image and some text. If I click on the image of a row, I want to perform action A, if I click on any other place in the row, I want to perform action B. Both of these actions need the id of the ListView item to work.
So far, for action B, I simply used listview's onItemClick, where I got the id from the parameter. However, as far as I know, the only way to find out on which view in the row itself the user clicked, is to add an onClick event handler to the ImageView that triggers action A. In that case, I only get the View that was clicked, though - not the id of the row that the ImageView is in.
My question is - how do I go from a View object to getting the id of the parent row within the ListView?
The onItemClickListener method has an onClick interface already with the view and position passed into it: You can get check which view has been clicked by doing a switch statement and performing a dedicated action when the specific view has been clicked.
E.g.
listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
String item = listView.getItemAtPosition(position);
Toast.makeText(this,"You selected : " + item,Toast.LENGTH_SHORT).show();
}
});
as shown in How to create listview onItemclicklistener
You'll see that in the onItemClick interface it has the View view as a parameter and int position as a parameter, perform the action inside the listener using both of those parameters. Like i mentioned before you can use a switch statement.
listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
String item = listView.getItemAtPosition(position);
switch(view.getId()) {
case R.id.imageView:
//perform action for imageView using the data from Item (if you have used an MVC pattern)
break;
// you can do this for any widgets you wish to listen for click actions
}
}
});
Update: All credit for this goes to wwfloyd in How to know which view inside a specific ListView item that was clicked
#Override
public View getView(final int position, View convertView, final ViewGroup parent) {
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.one_line, parent, false);
}
// This chunk added to get textview click to register in Fragment's onItemClick()
// Had to make position and parent 'final' in method definition
convertView.findViewById(R.id.someName).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((ListView) parent).performItemClick(v, position, 0);
}
});
// do stuff...
}
in your adapter apply a click listener to the element and then trigger the interface method, so that you can identify the registered widget that has been clicked and then in your onItemClick do:
public void onItemClick(AdapterView adapterView, View view, int position, long id) {
long viewId = view.getId();
switch (viewId) {
case R.id.someName:
//widget has been clicked
break;
default:
//row has been clicked
break;
}
}
if you aren't using tag, then you can
int itemId=...;
OnClickListener customOnClick=...;
imageView.setTag(itemId);
imageView.setOnClickListener(customOnClick);
and inside your custom OnClickListener
int itemId = (Integer) view.getTag();
note that you can set "any" Object as tag, so you may even set your whole custom object, not only id
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long id)
in this method View arg1 gives you Item view and you can find the item content by this arg1.findViewById(R.id.content);. Here position gives you the item position of list view
You can set click listeners for each view in a row from the getView() method in the adapter.
You can impliment it in getView() method of adapter of listview.

How to change Linear layout's items visibility in a ListView

I have a linear layout in a Listview and need to change some items' visibility within the linear layout when the Linear Layout gets selected. Due to the design, the Listview has no idea what the linear layout is and thus can't explicitly refer to any item in this layout. In this case, what will be the best solution to trigger the visibility change in the Linear layout?
listView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(
final AdapterView<?> adapterView,
final View view,
final int position,
final long id) {
//the View is a linearlayout
//will need to trigger its items visibility
}
}
Thanks!
Well, one possible solution is to call a method on the selected view, that will do what you want. Considering that you are using a custom view, it will look like this:
list.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
MyCustomView entry = (MyCustomView) parent.getAdapter().getItem(position);
// Now you call some method that changes the visibility of whatever you want
entry.doSomeStuff();
}
});
Ps: in this example I used a custom view, but of course it can be your LinearLayout, if that's what you only have, although it would be more appropriate to use a custom class, for better encapsulation.

Getting views of a custom view in android

Well I have a listview with an OnItemClickListener on it, each item of listview is a customview with two textviews, I want to get text of first textview when an item is clicked, So how can I do that? when an item is clicked I can just get the view that is clicked but how can I get textview on this customview?
Traverse that view's hierarchy:
TextView textView = (TextView) clickedView.findViewById(R.id.first_text_view);
clickedView is provided to you by OnItemClickListener.
yourListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
textFromFirstView = yourArray.get(position).getTextFromFirst();
}
});
whre getTextFromFirst() is getter for your textview for example

Custom Listview - determine if the image is clicked?

I have a custom ListView which has an ImageView, Textview and CheckBox.
In the activity I can register if the ListView Item is pressed by the following code, it they press an item anywhere in the ListView item it just checks a CheckBox and my adapter is updated.
lstView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long row) {
//code
}
But is there a way to determine if the ImageView was pressed? So if they press the image I can do something else?

How to access the controls of a single item of a list view

How do i access the controls of an list view on item Click listener in a list view? As i have set the customize layout for a single list view item.
Attaching an item click listener
In the actvity where your listview is created try something similar to these
private void setupListView() {
//find by view id
customListView = ((ListView) findViewById(R.id.lv_listview);
//set custom adapter
customListView.setAdapter(
new CustomListAdapter(this, R.id.lv_listview, variableList));
//attach listener
customListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
/* whatever happens when you click the list item goes here */
}
});
}

Categories

Resources