Updating a textview from the adapter - android

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

Related

Add listView header between list

This is my listView
I want to add a February header on top of 2016-02-02 and January in 2016-01-31. Is it possible?
Yes, you can do this by returning a different view in your getView() method in your adapter class. In your master list, that you pass to your adapter, you can add a divider item, a String or however you are holding all this data, I assume a custom class, that you know is meant to show a Month title. You can do a quick check in your getView() method and return a different view that displays the month..
In your getView() method, you can do this...
#Override
public View getView(final int position, View convertView, ViewGroup parent){
LayoutInflater mInflator = LayoutInflater.from(getContext());
View customView = mInflator.inflate(R.layout.times_layout, parent, false);
Time temp = getItem(position);
//Check to see if the time is supposed to be a header
//This is where you check to see if it meant to be a section header
if(temp.getDate.equals("HEADER")){
//Header, return section view instead of normal view
View sectionHeader = mInflator.inflate(R.layout.layout_list_divider, parent, false);
TextView txt_Section = (TextView) sectionHeader.findViewById(R.id.txt_Header);
sectionHeader.setClickable(false);
return sectionHeader;
}
//Normal View... do what you would do normally
return customView;
}
I hope this helps! Let me know.. it worked for me
In android It's called ExpandableListView
You can try this tutorial:
http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/
It also has a sample to download.

Android: ListFragment get clicked item within ListItem

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;
}

HorizontalScrollView in ListView items

I'm using a LinearLayout which contains an ImageView and a HorizontalScrollView as ListView items.
The problem is OnItemClickListener of the ListView doesn't work. After adding android:descendantFocusability="blocksDescendants" on LinearLayout it works when ImageView is clicked but it doesn't work when HorizontalScrollView is clicked.
android:focusable="false" and android:focusableInTouchMode="false" doesn't work too.
I want regular clicks on HorizontalScrollView also fires OnItemClickListener of ListView.
I'm testing on android 4.2.2
I had the same problem and did not find any better solution than just to assign View.onClickListener to descendant of HorizontalScrollView. In my case I needed to know the position value from ArrayAdapter belonging to the clicked listview item, which I passed to the view during the getView execution as a view tag.
RelativeLayout -> HorizontalScrollView -> Relative Layout #+id/clickit ...
public View getView(int position, View convertView, ViewGroup parent)
{
layoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(resourceID, parent, false);
MyType mt = getItem(position);
RelativeLayout rl = (RelativeLayout) rowView.findViewById(R.id.clickit);
rl.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
m_menuEventsListener.onActionsMenuClick(getItem((Integer) view.getTag()));
}
});
rl.setTag(position);
}
Note that the same m_menuEventsListener.onActionsMenuClick is called from the onItemClickListener attached to the listview. m_menuEventsListener is my own listener implemented in the Activity.

Android ListView with custom view - get elements

I create a list view with this tutorial - http://sunil-android.blogspot.com/2013/04/android-listview-checkbox-example.html
Now I have a questions:
1. How to create a button like "Check All"/"Uncheck All" ?
2. How to have access to some view element (like edittext) in a single row ?
your answer is in the tutorial as well...
Check the tutorial link again. There is a private class MyCustomAdapter extends ArrayAdapter<country>{} which is your answer.
When you bind your data to listview using adapter (in this case MyCustomAdapter is your adapter), for each element of your data array the #Override public View getView(int position, View convertView, ViewGroup parent) { } method will fire.
View convertView parameter in the #Override public View getView(int position, View convertView, ViewGroup parent) { } method is a view for each single row of your listview. If you have define xml template for your listview row with id for each element in a row, you can find element using...
TextView code = (TextView) convertView.findViewById(R.id.code);
CheckBox name = (CheckBox) convertView.findViewById(R.id.checkBox);
Here code and name are TextBox & CheckBox of the view from specific row.
So run through the example tutorial again and I think you probably would get your answer.

How do use the LayoutInflater inflater properly

I created the Layout design using java code only not from the XML Layout Designs. The code I used is following
public View getView(int position, View convertView, ViewGroup parent) {
TextView tv = new TextView(mContext);
tv.setText(hotelList.get(position).name);
return tv;
}
How to use layoutInflator for creating layout fro this. I need 2 more textviews in a single list item. the whole list contains 10 different list items
Please provide some codes for this. Help appreciated
I have gone through this before by having my static class too. Check this out, it will help:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if ( rowView == null) {
LayoutInflater inflator = this._activity.getLayoutInflater();
rowView = inflator.inflate(R.layout.todolistlisting, null);
TodoListViewHolder viewHolder = new TodoListViewHolder();
viewHolder._name = (TextView) rowView.findViewById(R.id.tVTLName);
viewHolder._completed = (TextView) rowView.findViewById(R.id.tVTLCCount);
viewHolder._remaining = (TextView) rowView.findViewById(R.id.tVTLRCount);
rowView.setTag(viewHolder);
}
TodoListViewHolder holder = (TodoListViewHolder) rowView.getTag();
VO_TodoList votodolist = this._items.get(position);
holder._name.setText(votodolist._title);
holder._completed.setText(votodolist._completed);
holder._remaining.setText(votodolist._remaining);
return rowView;
}
TodoListViewHolder is my view component holder here. like your TextView.
I guess you know how to make XML layout for this layout. So just make the XML layout and get the object of the main layout using the following code:
LinearLayout mainLayout=(LinearLayout) View.inflate(R.layout.yourlayout); //if yourlayout.xml is the name of the xml file you made and put in the layout folder.
To get the child of the layout, let's say if it's a TextView with the id text, then the code would be:
TextView textView=(TextView)mainLayout.findViewById(R.id.text);
You can add view at runtime by using inflater like this
LinerLayout linearLayout = (LinearLayout)inflater.inflate(R.layout.news_categories_item, null);
TextView categoryValueTextView = (TextView)linearLayout.findViewById(R.id.news_category_item_value);
mMainLinearLayout.addView(categoryValueTextView);
Here i am inflating one text view which is there in another linear layout(this is simple linear layout which holds only textview) at runtime and adding it to my main linear layout.
you can get the inflater object in your acitivity by using getLayoutInflater(). And if you want to get inflater in adapter you have to pass inflater object to constructor of adapter from your activity.

Categories

Resources