How add different Icons per row inside a ListView - android

I am trying this cool library that uses Ken Burn effect here (his github page)
I got it to work but I cant add different Icons on different rows. I tried to use Mkyongs CustomAdaptor example but what he used is a layout with TextView and ImageView. What I am using is ListView. I am already able to change the text font and size of the ListView but lost trying to add icons. How do you do this?
Below is my Layout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/blur4"
tools:context="com.gio.hia.hiarewired.NoBoringActionBarActivity">
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent" />
<FrameLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="#dimen/header_height">
<com.gio.hia.hiarewired.KenBurnsView
android:id="#+id/header_picture"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/picture0" />
<ImageView
android:id="#+id/header_logo"
android:layout_width="#dimen/header_logo_size"
android:layout_height="#dimen/header_logo_size"
android:layout_gravity="center"
android:src="#drawable/ic_header_logo" />
</FrameLayout>
</FrameLayout>
This is my Activity Class
private void setupListView() {
ArrayList<String> FAKES = new ArrayList<String>();
//for (int i = 0; i < 1000; i++) {
FAKES.add("Row 1");
FAKES.add("Row 2");
FAKES.add("Row 3");
FAKES.add("Row 4");
//}
mPlaceHolderView = getLayoutInflater().inflate(R.layout.view_header_placeholder, mListView, false);
mListView.addHeaderView(mPlaceHolderView);
//mListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, FAKES));
mListView.setAdapter(new ArrayAdapter<String>(this, R.layout.list_text_config, FAKES));
mListView.setOnScrollListener(new AbsListView.OnScrollListener() { ...
I used a custom R.layout.list_text_config to change the font
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:paddingTop="10dip"
android:paddingBottom="10dip"
android:paddingLeft="15dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="35dp"
android:textColor="#color/colorWhite"/>
In his example he used a layout inflater How do I use my Custom Adapter with a layout inflator or am I looking at the problem on the wrong direction?
I usually just do this
CustomListAdapter adapter=new CustomListAdapter(this, itemname, imgid);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
but how do I do that when he used a layout inflater like below
mPlaceHolderView = getLayoutInflater().inflate(R.layout.view_header_placeholder, mListView, false);
mListView.addHeaderView(mPlaceHolderView);
mListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, FAKES));

Like #Arslan said this can be done with a custom adapter. You are actually doing it correct
CustomListAdapter adapter=new CustomListAdapter(this, itemname, imgid);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
That actually creates the list with the icons. What you need to add to that is the header image to the mPlaceHolderView but instead of using mListView use the list that you created with your "custom adapter". I placed the modified line of code from the original line of code so you can easily see the difference.
CustomListAdapter adapter=new CustomListAdapter(this, itemname, imgid);
list=(ListView)findViewById(R.id.list);
mPlaceHolderView = getLayoutInflater().inflate(R.layout.view_header_placeholder, list, false);
//mPlaceHolderView = getLayoutInflater().inflate(R.layout.view_header_placeholder, mListView, false);
list.setAdapter(adapter);
Now and since your no longer using the mListView you should probably lookout for instances of mListView and replace it with your list

Related

How to add a icon in Listview without using Imageview and Textview

I want to add icon in listview but I do not want to use Imageview and textview model.Is there any solution to do this ?
Here is my layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/listviewLinks"
android:divider="#111111"
android:dividerHeight="2dp"/>
</android.support.constraint.ConstraintLayout>
Here is my code
ListView listLinks;
ArrayList al = new ArrayList();
for(int i = 0; i < linksArray.length(); ++i) {
al.add(linksArray.getJSONObject(i).get("baslik"));
}
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, al);
listLinks.setAdapter(adapter);
Here is my listview design how to add icon each listview item without using imageview and textview style

Using ViewGroup in custom row for Listview causing casting issue [duplicate]

I am getting an error when trying to set my view to display the ListView for the file I want to display(text file). I am pretty sure it has something to do with the XML. I just want to display the information from this.file = fileop.ReadFileAsList("Installed_packages.txt");. My code:
public class Main extends Activity {
private TextView tv;
private FileOperations fileop;
private String[] file;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.fileop = new FileOperations();
this.file = fileop.ReadFileAsList("Installed_packages.txt");
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.TextView01);
ListView lv = new ListView(this);
lv.setTextFilterEnabled(true);
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, this.file));
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
}
});
setContentView(lv);
}
}
list_item.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp"
android:textSize="16sp"
android:textColor="#000">
</LinearLayout>
main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<ScrollView
android:id="#+id/SCROLLER_ID"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:fillViewport="true">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5sp"
android:id="#+id/TextView01"
android:text="#string/hello"/>
</ScrollView>
</LinearLayout>
The ArrayAdapter requires the resource ID to be a TextView XML exception means you don't supply what the ArrayAdapter expects. When you use this constructor:
new ArrayAdapter<String>(this, R.layout.a_layout_file, this.file)
R.Layout.a_layout_file must be the id of a xml layout file containing only a TextView(the TextView can't be wrapped by another layout, like a LinearLayout, RelativeLayout etc!), something like this:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
// other attributes of the TextView
/>
If you want your list row layout to be something a little different then a simple TextView widget use this constructor:
new ArrayAdapter<String>(this, R.layout.a_layout_file,
R.id.the_id_of_a_textview_from_the_layout, this.file)
where you supply the id of a layout that can contain various views, but also must contain a TextView with and id(the third parameter) that you pass to your ArrayAdapter so it can know where to put the Strings in the row layout.
Soution is here
listitem.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/textview"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</TextView>
</LinearLayout>
Java code :
String[] countryArray = {"India", "Pakistan", "USA", "UK"};
ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.listitem,R.id.textview, countryArray);
ListView listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(adapter);
If you are getting that message when you are extending an ArrayAdapter, you are getting that error because you have not provided the correct resource id to display the item. Call the super class in the constructor and pass in the resource id of the TextView:
//Pass in the resource id: R.id.text_view
SpinnerAdapter spinnerAddToListAdapter = new SpinnerAdapter(MyActivity.this,
R.id.text_view,
new ArrayList<>());
Adapter:
public class SpinnerAdapter extends ArrayAdapter<MyEntity> {
private Context context;
private List<MyEntity> values;
public SpinnerAdapter(Context context, int textViewResourceId,
List<MyEntity> values) {
//Pass in the resource id: R.id.text_view
super(context, textViewResourceId, values);
this.context = context;
this.values = values;
}
The problem is in the line:
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, this.file));
Because when i change it to:
lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, this.file));
It works!

Setting an Adapter to the ListView Android Studio

I'm trying to add some items in a ListView. When I swith the Layout (startActivity), I call the class Listing.
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listing);
ListView lv = (ListView) findViewById(R.id.listViewLIST);
String[] items = {"Item1", "Item2", "Item3", "Item4"};
ArrayList<String> arrayList = new ArrayList<>(Arrays.asList(items));
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.activity_listing, R.id.listViewLIST, arrayList);
lv.setAdapter(adapter);
}
I read some tutorials an followed every step but the app stops working always in the last line of code
lv.setAdapter(adapter);
What am I missing?
Edit 27.12.2015
The activity_listing.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_height="match_parent" android:fitsSystemWindows="true"
tools:context="cigarkings.cigarking.Listing">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listViewLIST"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="51dp" />
<SearchView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/searchView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="#+id/listView"
android:layout_alignEnd="#+id/listView" />
<include layout="#layout/content_listing" />
</android.support.design.widget.CoordinatorLayout>
Assuming missing statement android:layout_width="match_parent" for your Coordinator Layout as a typographical error,the major error is in setting up array adapter.
In the statement : ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.activity_listing, R.id.listViewLIST, arrayList); the constructor parameters (2nd parameter:R.layout.activity_listing and 3rd parameter:R.id.listViewLIST) are incorrect.Array Adapter takes resource id of a textview as 3rd argument and not of a listview, and the layout resource id of the layout containing the textview is passed as 2nd argument.
So, one solution for this is to create a separate xml layout containing a textview and passing the layout resource id as 2nd argument and textview id as 3rd argument to the constructor of Array Adapter
First of all activity listing should not be in this pattern.
If you are at beginning stage then First try this Tutorial...
The best explanation ever for beginners.
Then you have to implement custom listView using BaseAdaptor class and custom XML.

Android: ArrayAdapter requires the resource ID to be a TextView?

I'm trying to implement a code in which a list of linear layouts are inserted in the list view by using the array adapter. Each linear layout contains two subviews. Here's the xml.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list_item"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/list_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"/>
<TextView
android:id="#+id/list_label"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:padding="10dp"
android:textSize="16sp" />
</LinearLayout>
Now in the fragment I created a code snippet like this.
final String[] my_page_items = getResources().getStringArray(R.array.my_page_items);
int[] my_page_icons = {
R.mipmap.ic_about,
R.mipmap.ic_app_settings,
R.mipmap.ic_account_settings,
R.mipmap.ic_logout
};
LinearLayout[] items = new LinearLayout[my_page_items.length];
TextView[] labels = new TextView[my_page_items.length];
ImageView[] icons = new ImageView[my_page_icons.length];
for(int i=0; i<my_page_icons.length; i++) {
items[i] = (LinearLayout) LayoutInflater.from(getActivity().getApplication()).inflate(R.layout.list_item, null);
labels[i] = (TextView) items[i].findViewById(R.id.list_label);
icons[i] = (ImageView) items[i].findViewById(R.id.list_icon);
labels[i].setText(my_page_items[i]);
icons[i].setBackground(getResources().getDrawable(my_page_icons[i]));
}
setListAdapter(new ArrayAdapter<>(getActivity(), R.layout.list_item, items));
lv = getListView();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
});
When I run this code, I get the the following in the logcat.
java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
How should this problem be solved?
In
new ArrayAdapter<>(getActivity(), R.layout.list_item, items)
R.Layout.list_item must be the id of a xml layout file containing only a TextView(the TextView can't be wrapped by another layout, like a LinearLayout, RelativeLayout etc!),
EDIT possible solution :
If you want your list row layout to be something a little different then a simple TextView widget use this constructor:
new ArrayAdapter<>(getActivity(), R.layout.list_item, R.id.id_of_your_textview items)
where you supply the id of a layout that can contain various views, but also must contain a TextView with and id(the third parameter) that you pass to your ArrayAdapter so it can know where to put the Strings in the row layout.

Android adding a relative view to a merge adapter

Hi i'm trying to add a relative view to a merge adapter but it's currently scrolling separetly to the lists so does anyone know how i can add a relative layout with an image view and a text view to a merge adapter?
my aim is to have it look like this
header(relativelyout red bar and title);
List
header(relativelyout red bar and title);
List
header(relativelyout red bar and title);
List
and to have this all scroll as if it was all one list
heres my attempt so far
arrayAdapter = new ArrayAdapter<String>(this, R.layout.single_item, newsList);
arrayAdapter2 = new ArrayAdapter<String>(this, R.layout.single_item, newsList2);
arrayAdapter3 = new ArrayAdapter<String>(this, R.layout.single_item, newsList3);
ListView list = getListView();
list.setTextFilterEnabled(true);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE );
View header = inflater.inflate( R.layout.redcell, list, false);
setListAdapter (arrayAdapter);
adapter = new MergeAdapter();
adapter.addView(header);
adapter.addAdapter(arrayAdapter);
adapter.addView(header);
adapter.addAdapter(arrayAdapter2);
adapter.addView(header);
adapter.addAdapter(arrayAdapter3);
setListAdapter(adapter);
}
redcell.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/redcelllayout" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="30dp"
android:src="#drawable/titlerect" />
<TextView
android:id="#+id/redheadertext"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="left"
android:paddingLeft="15dp"
android:paddingTop="3dp"
android:textColor="#color/white"
android:textSize="15dp"
android:textStyle="bold"/>
</RelativeLayout>
Instead of creating them in the program, create an xml file for that view, inflate it, then add it to the adapter.
Like this:
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View header1 = inflater.inflate(R.layout.redbar_title);
View header2 = inflater.inflate(R.layout.redbar_title);
View header3 = inflater.inflate(R.layout.redbar_title);
adapter = new MergeAdapter();
adapter.addView(header1);
adapter.addAdapter(arrayAdapter);
adapter.addView(header2);
adapter.addAdapter(arrayAdapter2);
adapter.addView(header3);
adapter.addAdapter(arrayAdapter3);
setListAdapter(adapter);
Set each list layout_height to wrap_content, and wrap everything within a ScrollView.
Perhaps it would be easier if you could make your headers part of the adapter itself, as separators, and use a single list instead of 3.

Categories

Resources