I have a list of items that each has their own ArrayList<String>. When an item is clicked, the ViewPager activity is opened. I want each string to fill up an entire view in ViewPager, so that the user can swipe through each of these strings as pages.
Is ViewPager the best way to go about doing this?
If so, how can I populate it with the contents of the ArrayList<String>?
You should probably use a ViewPager. To use it you need to supply a PagerAdapter. In your subclass of PagerAdapter, just have your instatiateItem override create a TextView (either programmatically, or by inflating a layout with a TextView) and set the text to the String at the position in the array corresponding to the position parameter of instantiateItem. Something like this
#Override
public Object instantiateItem (ViewGroup container, int position) {
TextView tv = new TextView(getContext());
tv.setText(mList.get(position));
return tv;
}
Related
I have a RecyclerView that shows a list of items.
If there is no item to show, The recyclerview shows one item with a specific view (to tell the user there is no item instead of a white screen).
Within HistoryFragment:
private void initRecyclerView(Boolean isNoResult){
HistoryRecyclerViewAdapter adapter = new HistoryRecyclerViewAdapter(mContext, mRecords, **isNoResult**);
mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
mRecyclerView.setAdapter(adapter);
}
Within HistoryRecyclerViewAdapter:
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view;
if(**isEmpty**) {
**view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_listitem_prhistory_empty, parent, false);**
} else {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_listitem_prhistory, parent, false);
}
ViewHolder holder = new ViewHolder(view);
return holder;
}
So, it's possible to remove items one by one, if we click on them.
I would like to set isEmpty to true and refresh the RecyclerView when the dataSet is null.
I already know where to call that method but I really don't know how I can do that? (i.e. refresh the RecyclerView with isEmpty = true so I can display the cell that explain to the user that there is no record anymore).
Don't inflate different view-holders, because when the adapter has no items, not a single one of them will ever be inflated. Instead one can wrap the RecyclerView together with a "no data" Fragment into a ViewFlipper, which then can be switched to the Fragment, when the RecyclerView adapter has no items.
Best practice is to use an empty view outside of RecyclerView but in case you like to do what you want:
1.in onCreateViewHolder only inflate one layout which has empty and item views
on item delete check if your array is empty and then add a null item
then in onBindViewHolder check the model if model is Null visible empty view otherwise show item view
summary:
onBind:
model is null : empty View visible
model is not null: item View visible
use interface to refresh the RecyclerView after remove something like this
public interface RefreshRecyclerView {
public void Refresh();
}
then in activity or fragment implement the interface
Fragment implements RefreshRecyclerView
you will have override method like this
#Override
public void Refresh() {
// set adapter again here
}
then pass the interface to adapter like this
RefreshRecyclerView refresh = (RefreshRecyclerView) this;
yourRecycler.setadapter(refresh);
fially when user clicked on adapter use this
refresh. Refresh();
My method add views to PagerAdapter:
public void initdata() {
for(int i = 0;i<=1189;i++){
view = inflater.inflate(R.layout.testss,null);
tx = (TextView)view.findViewById(R.id.txts);
tx.setText(textseries[i]));
pageList.add(tx);}
}
My pagerAdapter:
#Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(pageList.get(position));
return pageList.get(position);}
it takes 5~6 sec add views to PagerAdapter , but the TextView is just change it's content,how to avoid call view method so much times ?
how to avoid call view method so much times ?
The best solution would be to replace ViewPager with RecyclerView, so that pages can get recycled.
The next-best solution is to work out some PagerAdapter that can manage recycling of your pages.
The solution closest to what you have is:
Step #1: Get rid of initdata()
Step #2: Get rid of pageList()
Step #3: Inflate the layout and set the text on the TextView in instantiateItem(), so you only need to do it for pages that the user visits, and then only on demand
I have a 'ChecklistItem' class that has the following properties:
private CheckBox checkBox;
private ImageButton noteButton;
private TextView vitalField;
I have an onClick Listener for my checkbox. Now the problem is, when I click on that checkbox and the OnClick() method gets called, how can I figure out what ChecklistItem that checkbox is a part of?
Whenever I click on a checkbox, I want to add the ChecklistItem that the checkbox is a part of to an array, but the OnClick() only knows about the checkbox that called it.
How can I get around this?
Ok so this answer is according to the "long discussion" we had
let's assume you want to make a - re usable - view of your list and you wrote a separate xml layout file called list_item as the following:
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/checkbox"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text_view"/>
so now let's assume you are in the activity or fragment or wherever you want to host your view , NOW I have to point out this is just an example , usually a list view is what you would need in this case but again I have very little details about your app so I'm going to keep it simple
Assuming you have a vertical linear layout and you want to add these "rows" to it, each row represents one of your custom view
LinearLayout layout = findViewById(R.id.layout);
LayoutInflater inflater = LayoutInflater.from(this); // This inflater is responsible of creating instances of your view
View myView = inflater.inflate(R.layout.list_item, layout, false); // This view objects is the view you made in your xml file
CheckBox checkBox = (CheckBox) myView.findViewById(R.id.checkbox);
TextView textView = (TextView) myView.findViewById(R.id.text_view);
checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//if checkbox is checked enable textview for example
// here you have a reference to all the views you just created
// Weather you want to save them in a class together that's up to you and your app's logic
}
});
layout.addView((myView));
if the list is might exceed the screen height you may want to wrap your linear layout in a scroll view.
BTW: ListView is just a neat way to do this automatically by defining how you want each row to appear, and of course it manages your views for you and recycle them when they get of screen, but I just wanted to point out the concept.
Hope this helps you
As illustrated in the photo, i have a list view that is made up of a custom layout which has two TextView. One TextView is for storing numbers which has a visibility of gone, the other is for storing the name, which is visible.
all i want is to be able to get a string of all numbers that are selected when the send button is clicked.
A good suggestion here is to use a recyclerview instead of list view.
To achieve want you want with a list view you just set an item click listener to your list view in your activity. The item click listener will pass the View which is the current cell in the list view. Then you can find textview by id to locate. The Textview ID is set in the layout xml.
Example
listView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View cell,int pos, long arg1)
{
TextView textView = (TextView)cell.findViewByID(R.id.myTextView);
String texViewContents = textView.getText().toString();
}
});
Is it possible to have methods and actual coding inside each item for a GridView?
The app that I am attempting to create is a currency converter, and I am currently displaying 3 images in the gridView: Euros, Pesos, and Rupees.
Once the user clicks on one, I want the open to open up a new XML which displays a textView. The user enters the value of US dollars in the textView and clicks the compute button. The app then displays the converted amount in the bottom of the screen.
The problem is that I am unable to figure out how to open up a new XML every time a picture is clicked on in the gridView. Assuming that I am able to do this, I am also unsure of where to place the code that goes behind the conversions. Would I make a new .java or just place is all in MainActivity.java?
Thanks.
What you might be best doing is when the user clicks on a currency it takes them to another activity where you would then load another xml for whatever you want to show.
In order to detect which item had been clicked you can implement an onItemClickListener for example
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//this assumes you give the gridview a list of currency which it then displays. Here we get the currency selected to then pass to our new activity
String selectedCurrency = myArrayOfCurrencies.get(position);
//then start new activity and give it the currency. This means we won't have to create an activity for each currency. You just need to create 1 and then based on what currency you give it will change the functionality
Intent intent = new Intent(this, Converter.class);
Intent.putExtra("currency", selectedCurrency);
startActivity(intent);
}
First you should be able to detect the clicks on each item of the GridView by calling the setOnItemClickListener() method.
If you set the clicklistener and you still can't detect the clicks, then most probably you need to add those attribtutes to your imageView in the xml
android:focusable="false"
android:focusableInTouchMode="false"
Second, once you are able to detect the clicks you can start new activity or add fragment that contains that edit text that will promote the user to enter the value.
Third, I would suggest to put the code responsible for the currency conversion in a class separately and create static methods that takes a value and convert it to the other curreny such as:
public class CurrencyConverter {
public static double convertToRupees (String currencyType, double currencyValue){
....
return currencyInRupees;
}
}
and by the way I would suggest you to use RecyclerView with grid layout manager instead of GridView.
I would create more classes.
You asked how to open a different XML file for each gridView item.
Create a custom adapter that extends BaseAdapter.
Override getView and for each view attach the right Xml file, according to the position.
For example:
YourActivity.java:
GridView gridView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gridView = (GridView) findViewById(R.id.gridView);
gridView.setAdapter(new MyAdapter(getApplicationContext());
}
MyAdapter.java:
...
#Override
public int getCount() {
return XmlArr.length;
}
#Override
public Object getItem(int position) {
return XmlArr[position];
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Xml myXml = (Xml) getItem(position);
Holder holder;
if (convertView == null) {
// Set your view's layout. Consider using LayoutInflater.
// Use a static holder to prevent re-initialization:
holder = new Holder();
// holder.textView = ...
// holder.Xml = ...
// Or whatever you decided to have in each gridView item.
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
holder.Xml = myXml;
...
return convertView;
}
static class Holder() {
TextView tv;
Xml xml;
...
}
I assumed you would used an Xml array (xmlArr).
Now you have option to play with each gridView item as you wish. You can set each view/button/textView an onItemClickListener, or you can also set the whole gridView an onItemClickListener (from YourActivity.java).
Hope this helps.