I have a RecyclerView as follows
<android.support.v7.widget.RecyclerView
android:id="#+id/drawerList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/nav_header_container"
android:layout_marginTop="15dp" />
I have a string array like this
<string-array name="my_array">
<item>#string/item_1</item>
<!--<item>#string/item_2</item>-->
<item>#string/item_3</item>
<item>#string/item_4</item>
<item>#string/item_5</item>
<item>#string/item_6</item>
This string array is used to display data in RecyclerView. I want to display a textview alongside with item_4, Is it possible? How to make it?
From Activity Pass this list to RecyclerView adapter
try {
String[] array = getApplicationContext().getResources().getStringArray(R.array.my_array);
List<String> list = new ArrayList<String>();
list = Arrays.asList(array);
//pass this list to RecyclerView adapter
}catch (Exception e){e.printStackTrace();}
In your onBindViewHolder method just put a check for the element you want to display differently and do what you want to do with it.
Some solutions:
1.- You can concat a string to the 4th item when you check the position in the onBindViewHolder.
2.- You can create a layout and inflate it on onCreateViewHolder which has two textViews one for the items and one for the text that will go next to it. When you detect that you need to show the second textview change the content and the visibility of the textview.
<LinearLayout android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android" >
<TextView
android:id="#+id/items"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/textViewNextToItemOfChoice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
</LinearLayout>
Then on code you just change the visibility of textview "textViewNextToItemOfChoice" and the content of it.
I think you should make an object (You own model) instead of using just an array of string to populate the list. In that way you can create an object with 2 Strings and in the adapter when you check if it has both string you show them.
Related
I have a listview containing some news. Each news is a row which comtains a title, content, publisher and date. Now I want to set hyperlinks to every title TextView. How to implement that? The following is my code:
private void show_news() {
ArrayList<Map<String, Object>> list = new ArrayList<>();
get_data(list);
ListView lv = (ListView) findViewById(R.id.News_List);
SimpleAdapter adp = new SimpleAdapter(getApplicationContext(), list, R.layout.news_item, new String[]{"news_title", "news_content", "news_publisher", "news_date"}, new int[]{R.id.news_title, R.id.news_content, R.id.news_publisher, R.id.news_date});
lv.setAdapter(adp);
/* the code above runs properly*/
/*Now I want to set hyperlink to my title TextView..*/
}
This is my news_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/news_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:textColor="#000000"
android:textSize="25sp" />
<TextView
android:id="#+id/news_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:textSize="18sp" />
<TextView
android:id="#+id/news_publisher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:textSize="12sp" />
<TextView
android:id="#+id/news_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp" />
</LinearLayout>
I am new to Android programming. Can anyone help me with this? Thanks a lot!
LinearLayout first = (LinearLayout) adp.getView(0, null, lv);
TextView title = (TextView)first.getChildAt(0);
That doesn't necessarily get the first element of the list view. It doesn't even necessarily get the first element on screen. That's very implementation specific (you're assuming the list view doesn't have any children other than its rows, which is actually wrong in many cases).
If you want to change the first item of a list view, do
Item item = adb.getItem(0)
item.title= "New Title"
adp.notifyDataSetChanged();
You can extend the SimpleAdapter and use the derived adapter. When you extend the adapter, set the hyperlink property for the title by calling:
<the title textview>.setAutoLinkMask(Linkify.WEB_URLS);
in the method getView method of the adapter. A commonly used approach in Android ListView adapter is to use a so called view holder, which is a key technique to get better listview performance, and you can check the details from the developer document: Hold View Objects in a View Holder
Suppose you employ this approach, the code would looks more or less like:
holder.titleView.setAutoLinkMask(Linkify.WEB_URLS);
If your textview does not have a URL, then you can set the text in HTML form as:
holder.titleView.setText(
Html.fromHtml(
"Breaking News "));
holder.titleView.setMovementMethod(LinkMovementMethod.getInstance());
I have a row.xml file which define a row whose content and number of rows will be dynamic.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/room_detail_row_in_hotel_detail">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/name"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#color/sz_blue"
android:id="#+id/price"/>
</LinearLayout>
The best solution as suggested by #Hell is to use a Listview.
Say you are operating within a fragment, add a listview in the xml for the same.
The fragment (or activity) containing the listview will dynamically create list(preferrable)/array of data.
In your case the dataset will essentially either be a list of class object which uniquely have two objects, price and name.
Create an adapter, could be a BaseAdapter or any other form depending on your specific use-case. This adapter would essentially inflate your row layout (as specified in your question) and dynamically add the data.
Add the data into the adapter using it's constructor. Following which assign this adapter as the listview's adapter and call the "notify" method for the adapter indicating that a new dataset has been loaded and the list needs to be refreshed.
Also note, it's highly suggestible to add smoothScroll to your listview for obvious reasons.
I guess what you are looking for is a RecyclerView.
You should use the Viewholder-Pattern to reuse your layouts
I am currently developing application kind of notepad or Todo list. But I need to know to how to show empty list into listview, I guess android default ListView can not handle empty list view. Basically I need to show listview with no data, which I will add later, but initially the UI for listView should be empty.
Thank You!
this may helps you when there are no data in list
listview.setVisibility(View.INVISIBLE);
nodat.setVisibility(View.VISIBLE);
Hre nodata is Contain Empty Textview with SetText Your String like No Data
if you added the empty list into the adapter and that adapter set to listview then it will show you empty listview. after you add item to the list then you need to call notifydatasetchanged() of the adapter.
ListView as display initially empty till you add an item to the list and notify via adapter.
Note you must call method notifydatasetchanged() of an adapter when you populate the list item for add/edit/delete.
According to Developer Guidelines describing ListView you can easilty setup a TextViev, any other widget will be fine as well. Where you will encurage user to create ListItem.
Here is a snippet.
<ListView android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
<TextView android:id="#android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:text="No data"/>
I want to design below screen
When i press 'Add number' button, it will insert one entry in below scrollable layout. In that when i press 'X' button it should delete that particular row.
How to achieve this??
Any idea??
use
ViewGroup.addView(View view);
to add a view to some layout.
To create a layout dynamically, use:
TextView txtView=new TextView(this);
//Its an example, you can create layouts, buttons, image view, and other components.
To Delete a layout or view dynamically, getParent of layout, and delete, by:
ViewGroup.removeView(View view);
You should use a ListView which is backed by an ArrayList of Objects or Strings. When you want to remove an item from your ListView, remove the object from the ArrayList :
mData.remove(object);
and then notify the ListView that the date has changed :
mAdapter.notifyDataSetChanged();
Use a ListView for displaying the list of patterns
Create a custom layout for each list item. e.g.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="555*" />
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="matched 5 " />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="X" />
</LinearLayout>
Create a Custom Adapter class extending BaseAdapter
It can maintain a list for the patterns to be shown
In the getView method of the custom adapter -
inflate the xml
set the information (like pattern and number of matches) based on the index parameter, using the list
set onclick listener for the button (delete that item from list and call notifyDatasetInvalidated())
return the view.
On "Add Number" add item to the list in the adapter
I've created some composie UIs in my android apps and there are some ListView controls -among other controls- inside a view. Because of this, I have used "Activity" as my activity base class.
Now I need to display a simple message like "No Item" when the ListView that is bound to my adapter is empty. I know this is possible when using ListActivity but I'm not sure what's the best approach for this?
You can have an empty view without a ListActivity! The correct method is as follows
First add an 'empty view' to your layout XML below your list
...
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<TextView
android:id="#+id/empty"
android:text="Empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
/>
...
Next override the onContentChanged method of your activity and set the empty view of your list to your empty view:
#Override
public void onContentChanged() {
super.onContentChanged();
View empty = findViewById(R.id.empty);
ListView list = (ListView) findViewById(R.id.list);
list.setEmptyView(empty);
}
That's it! Android will take care of hiding/showing the list and empty view when you update the adapter.
The Magic
Deciding whether the empty view is shown or not is handled by the superclass of ListView, AdapterView. AdapterView registers a DataSetObserver on the set adapter so it is notified whenever the data is changed. This triggers a call to checkFocus in AdapterView which contains the following lines
if (mEmptyView != null) {
updateEmptyStatus((adapter == null) || adapter.isEmpty());
}
and sets the empty view visibility based on whether the adapter is empty or not.
You're looking for the empty view of a ListActivity:
ListActivity
If you're using ListView you can use the method setEmptyView():
setEmptyView
Just combine your ListView with TextView:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="#+id/list"
android:layout_height="fill_parent"
android:layout_width="fill_parent" />
<TextView
android:id="#+id/list_empty"
android:text="No Item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</LinearLayout>
Then check the count of items an chanche visibility on ListView accordingly:
ListView lv = (ListView)findViewById(R.id.list);
lv.setVisibility((adapter.isEmpty())?View.GONE:View.VISIBLE);
If you are using Custom Adapter, you can do this in the overridden notifyDataSetChanged method.
You can use Toast Message for this..
Check the Count of the Adapter value by adapter.getCount()
if(Adapter.getCount()!=0){
List.setAdapter(Adapter);
}else{
Toast.makeText(YourActivityName.this, "No Items Available",Toast.LENGTH_SHORT).show();
}
For the layout code by Joseph, you need to edit the #+id/list and #+id/empty to #android:id/*, like:
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<TextView
android:id="#android:id/empty"
android:text="Empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
/>
This way, you even don't need to override the onContentChanged() function.
The easiest way to achieve this was using a ListFragment instead of a ListActivity. ListFragment has the following convenience method:
setEmptyText("My no items message...");
Besides, using a ListFragment class has other advantages. For example, the possibility to combine it with the new AppCompat library (which you cannot do with ListActivity because you have to extend from ActionBarActivity).