My app contains 3 tabs.All the tabs contains List so I have used ListFragment. There is a button in each ListItem. I want to do "something" when "Click" button is clicked within the ListItem as shown in the figure.
How do I implement this. There are tuts about doing same thing for ListActivity but not ListFragment.Thanks in advance. :)
First of all, you need to implement a custom adapter for your ListView. Please, read this article if you are not familiar with this.
Next, you have to disable click on ListView items. If you don't know how, check this out.
Now, in your getView method from your custom adapter, you can find your Button and set up an onClickListener, but only after you have inflated the view for the current ListView position.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
textView.setText(values[position]);
Button mButton = (Button) view.findViewById(R.id.my_button_id);
mButton.setOnClickListener(mClickListener);
return rowView;
}
Related
I have two views one for list and the other one for the header. The list has an item and a checkbox without text. I am trying to update a textview in the header section with the total count of selected items on each checkbox click. But it is not getting updated. Here is my code in the adapter. Can anybody help with a working solution?
#Override
public View getView(final int position, View convertView, final ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View pview=inflater.inflate(R.layout.activity_main, parent,false);
TextView txtcnt= (TextView) pview.findViewById(R.id.txtcount);
txtcnt.setText("8"); //This code not updating the textview.
........
Yes you can update the textview ,lets say your header textview is in activity or fragment class and you are trying to update the header from adapter then make the header textview in your activity or fragment class as public static and access header textview to set values in adapter
I'm trying to put a button to delete a row of a listview. I've modified the layout of the single rows like this:
With lst.setOnItemClickListener... I manage the click on the row, but I don't know how to click the button inside the list.
It can be done?
Search on google "custom adapter"
Then the 5 first links :
Custom Adapter for List View
http://www.javacodegeeks.com/2013/06/android-listview-custom-adapter-with-imageview.html
http://www.learn-android-easily.com/2013/06/listview-with-custom-adapter.html
http://www.vogella.com/tutorials/AndroidListView/article.html
http://developer.xamarin.com/recipes/android/data/adapters/create_a_custom_adapter_for_contacts/
Override your adapter getview method to handle the button click.
public View getView(final int position, View convertView,
ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.vehicals_details_row, parent,
false);
Button deleteImageView = (Button) row.findViewById(R.id.DeleteImageView);
deleteImageView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
});
}
}
In your listItem xml layout, set the button to have the following attribute and it will cause the list item to be clickable as well:
android:focusable="false"
I am new to Android. I would like to create an Activity very similar to "Add event" part of Android Calendar app. To me it looks like a ListView with different components in each row. I could be wrong. If I am right, I still don't know how to add different components to each row of a ListView, e.g., EditText in one row, TextView in another row, etc. If this app is not ListView at all, if anybody can tell me how I can create something similar, I'd appreciate that a lot.
hello check this link it be helpful 1
listView with different component
First you have to learn how to implement a custom Adapter (see this tutorial: http://www.vogella.com/tutorials/AndroidListView/article.html#adapterown)
Then in your getView overriden method (you'll learn about it in the tutorial) you have to do something like this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = null;
if (position == LAYOUT1_POSITION) //In this row you will place the layout named layout1
rowView = inflater.inflate(R.layout.rowLayout1, parent, false);
if (position == LAYOUT2_POSITION)
rowView = inflater.inflate(R.layout.rowLayout2, parent, false);
//Do similar for all your different layouts
return rowView;
}
I have a List view with a custom adapter (imageviews), in the same activity I have a header and footer, the listView is between those.
What I want is to add a button as the last item of the list view so when you arrive to the last item it appear, i cant add the button outside because it wont scroll
Sorry about my english
Regards
use ListView.addFooterView() to add view as a footer which is visible only in the end of the list:
http://developer.android.com/reference/android/widget/ListView.html#addFooterView(android.view.View)
If you already have a custom adapter class implemented, the solution is rather simple. Based on an xml layout implemented for your list-view rows which contains both a Button and an ImageView, you can hide/display them in the adapter's getView() method based on the index. This is a code sample, which I currently don't have the chance to test and might not be the most efficient solution, but it should give you an idea:
class CustomAdapter extends SimpleAdapter {
[...]
#Override
public int getCount() {
// number of images to be displayed + 1 for the button
return images.length + 1;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View row = inflater.inflate(R.layout.row, parent, false);
final ImageView imageView = (ImageView) row.findViewById(R.id.image);
final Button button = (Button) row.findViewById(R.id.button);
if (position == getCount() - 1) {
// The last element
imageView.setVisibility(View.GONE);
// set an OnClickListener on the button or whatever...
} else {
button.setVisibility(View.GONE);
// do your regular ImageView handling...
}
return row;
}
}
I have created a ListActivity class, with a custom Adapter.
Rows are simple: TextView and a Button.
Implemented getView method is as below:
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.row, null);
}
TextView tV = (TextView) convertView.findViewById(R.id.tv);
tV.setText(MY_LIST[position]);
return convertView;
}
Now I want that each row, when pressed, dynamically add another button to itself, below the others components, and that consequently, the row height is increased.
How can I perform this steps?
New button should be hidden by default. When user clicked on row your handler makes something like mHiddenButton.setVisibility(View.VISIBLE).