Populate android listview Android - android

My listview below contains only one textview per row. I need another textview to add a second textview to my row. the problem is i dont know how to populate more than one textview. it there is a second textview txIndex for example. How can i modify my adapter to populate data from two textviews.
String[] bible_list_one = getResources().getStringArray(R.array.bible_list_one);
String[] bible_list_two = getResources().getStringArray(R.array.bible_list_two);
list.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, bible_list_one));
list.setOnItemClickListener(this);
secondlist.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, bible_list_two));
secondlist.setOnItemClickListener(this);

You will have to write your own CustomArrayAdapter. I hope the tutorial link is helpful.

Check this out: Android Hive Link
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:background="#drawable/listselect"
android:orientation="horizontal"
android:padding="5dip" >
<TextView
android:id="#+id/txtViewNameDAR"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name : "
android:textColor="#040404"
android:textSize="15sp"
android:textStyle="bold"
android:typeface="sans" />
<TextView
android:id="#+id/txtViewVisitedCityDAR"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="Type : "
android:textColor="#343434"
android:textSize="13sp" />
<TextView
android:id="#+id/textViewTypeDAR"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Visited City : "
android:textColor="#343434"
android:textSize="13sp" />
</RelativeLayout>
Change the IDs and colors to your needs.
This is an independent XML file.
Use it like below :
list.setAdapter(new ArrayAdapter<String>(this,
"R.Layout.ABOVE_XML_FILENAME, bible_list_one));
Then, In getView
TextView text = (TextView) row.findViewById(" Your Desired textViewID");
text.setText(" ");
Hope you get some idea

Related

Kotlin Android - ImageView in ListView

I'm using an ArrayAdapter to populate my listView:
val adapter = ArrayAdapter(applicationContext, R.layout.listview_text, emailAddresses)
listView.adapter = adapter
and this layout for custom textView:
<?xml version="1.0" encoding="utf-8"?>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#FFFFFF"
android:textSize="16sp"
android:paddingTop="35dp"
android:fontFamily="#font/familiar_pro_bold"
android:textColor="#212121"
android:id="#+id/listText"/>
Now, I want to display an icon (as a "view full item") at right of each items (which are added by user).
I want to avoid hundred of lines of code just for an image, what can I do?
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#FFFFFF"
android:textSize="16sp"
android:paddingTop="35dp"
android:fontFamily="#font/familiar_pro_bold"
android:textColor="#212121"
android:id="#+id/listText"
android:drawableRight="#drawable/view_full_item"/>
Also you can add some padding with android:drawablePadding=xdp

How to get value from a TextView which is in a ListView without any custom adapters?

I have a ListView with a TextView (which has an integer as string) and a Seekbar.
I need to extract the integer value and set it to the Seekbar.
Unfortunately, when I do this, I get a NullPointer exception because the content that populates the ListView item is not recognized by the onCreateView of the PlaceholderFragment.
String progressValue = percentileTextView.getText().toString();
bar.setProgress(Integer.parseInt(progressValue));
I have an activity layout for this activity, then a fragment layout which contains the listview and then I have a content.xml where the actual TextView is present. My question is how to reference this TextView? Am I doing this right, i.e. Activity layout contains a ConstraintLayout with Toolbars etc, then the Fragment Layout contains the ListView and I am using a separate content.xml and applying that to the ListView via a SimpleAdapter.
newAdapter = new SimpleAdapter(getContext(), newList,
R.layout.content, new String[] {
"name",
"percent"
},
new int[] {
R.id.name1,
R.id.percentile1
});
I hope you understand my problem.
When I do the following, it works (i.e. Android Studio understand that is the TextView) - but throws a null pointer exception upon build.
TextView percentileTextView = (TextView) rootView.findViewById(R.id.percentile1);
UPDATE 1 for pskink:
Newlist contents:
[{TA=Item1, IA=46}, {TA=Item2, IA=98}, {TA=Item3, IA=70}, {TA=Item4, IA=54}, {TA=Item5, IA=99}, {TA=Item6, IA=98}]
Row item layout (content.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="wrap_content"
android:orientation="vertical"
android:padding="#dimen/activity_horizontal_margin">
<TextView
android:id="#+id/name1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:textStyle="bold"
android:textColor="#color/colorAccent" />
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:padding="5dp">
<TextView
android:id="#+id/greater_than"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#5d5d5d"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="#string/greater_than" />
<TextView
android:id="#+id/percentile1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#5d5d5d"
android:layout_toRightOf="#id/greater_than"
android:layout_toEndOf="#id/greater_than"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#5d5d5d"
android:layout_toRightOf="#id/percentile1"
android:layout_toEndOf="#id/percentile1"
android:text="#string/percentile_text"/>
</RelativeLayout>
<SeekBar
android:id="#+id/seekBar2"
android:paddingTop="12dip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end|bottom"
android:paddingTop="12dip"
android:orientation="horizontal">
<ImageView
android:contentDescription="info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#mipmap/someimg" />
</LinearLayout>
</LinearLayout>

ArrayAdapter requires the resource ID to be a TextView - ERROR

I am trying to customize my listview in Android like
What I am exactly trying to achieve is I want to add those color at the left side of each listview item and of course I want to use a different color for every item. How can I achieve this ?
This is the code where my listview is:
<RelativeLayout
android:id="#+id/hidden_panel"
android:layout_width="match_parent"
android:layout_marginTop="56dp"
android:layout_height="match_parent"
android:background="#android:color/white"
android:visibility="gone" >
<ListView
android:id="#+id/listView1"
android:layout_width="wrap_content"
android:layout_height="500dp" >
</ListView>
<Button
android:id="#+id/button7"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:textColor="#fff"
android:textSize="18sp"
android:layout_marginTop="5dp"
android:text="Compute admission chance"
android:textAllCaps="false"
android:background="#drawable/roundshapebtn"
android:layout_below="#id/listView1"
android:layout_centerInParent="true" />
</RelativeLayout>
EDIT:
This is what I did for my listview item:
ListView lv = (ListView) findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.spinner_layout,listRecNames);
lv.setAdapter(adapter);
Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="95dp"
android:divider="?android:dividerVertical"
android:showDividers="middle" >
<!-- You can use any view here. Set background "rectangle.xml" drawable to this view.-->
<View
android:layout_width="20dip"
android:layout_height="20dip"
android:background="#drawable/rectangle"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
/>
<TextView
android:id="#+id/custom_spinner"
android:textSize="16sp"
android:padding="10dp"
android:textColor="#000"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
but I get this error:
ArrayAdapter requires the resource ID to be a TextView
You need to use a custom implementation of ArrayAdapter where you implement the getView() to inflate your View and do exactly what you want.
What you have used is a generic adapter that works for list items that have just one text to display in a list with TextViews.
For more information, you may refer a tutorial like: Custom ArrayAdapters
The Array adapter requires the id of the Textview to bind the value into list view.
ListView lv = (ListView) findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.spinner_layout,R.id.custom_spinner,listRecNames);
lv.setAdapter(adapter);

Custom layout for each row in a ListView

I am trying to get a button to work in a ListView Row. I been Reading about Custom Adapter but can't get it to work. Basically what i have a List_item class that have 4 TextView and 1 button which fieds a ListView in another class. one of the textViews has id/Name. so what i want is when i press the button on that row. it will get that Value of TextView "name" and add it to a ArrayList and if i press another button on different row it will add the Value of "name" to the same arraylist. so basically will add all the ones that i pressed on the button to an ArrayList. is that possible??.
JUST TO ADD. the textView Are being feed in by my database ..thanks upfront for any replies.
List_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="10dp" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false" >
<TextView
android:id="#+id/id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:visibility="gone" />
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="left"
android:layout_weight="3"
android:textAllCaps="true"
android:textColor="#FFFFFF"
android:textSize="30sp"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:id="#+id/price"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="right"
android:paddingRight="15dp"
android:textAllCaps="true"
android:textColor="#FFFFFF"
android:textSize="30sp"
android:textStyle="bold"
android:typeface="serif" />
<Button
android:id="#+id/order"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="0"
android:focusable="false"
android:minHeight="0dp"
android:minWidth="50dp"
android:text="+"
android:textStyle="bold" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/tableRow1"
android:gravity="center_horizontal" >
<TextView
android:id="#+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAllCaps="true"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:textStyle="bold"
android:typeface="serif"
android:visibility="gone" />
</TableRow>
</RelativeLayout>
my menu.xml has
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="697dp" >
</ListView>
now i want to get Values of TextView " name " when i press the button on its row.
that is how i used to fill my listView before i add the button and it was working fine
ListAdapter adapter = new SimpleAdapter (getCategoryItems.this,itemsList,R.layout.list_item, new String[]{ TAG_ID, TAG_NAME,TAG_PRICE, TAG_DESCRIPTION},new int[] { R.id.id, R.id.name, R.id.price, R.id.description});
setListAdapter(adapter);
but now after i added the button it doesn't get any values so i reaserched and i found that i have to change my sampleAdapter to a custom Adapter. and add the button inside getView() but i can't get that hang of it.
my button OnClickListener
Button order = (Button) view.findViewById(R.id.order);
order.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
String name = ((TextView)view.findViewById(R.id.name)).getText().toString();
Log.d("Selected : ", name);
}
});
You will need to create a custom adapter class for your ListView. Since you are getting data from a database, I suggest that you extend CursorAdapter and override newView() and bindView(). From my experience, how to do this correctly is poorly documented, so I'll try to give some pointers here.
In newView(), you need to use a LayoutInflator which is provided to you by calling getLayoutInflator() on your ListActivity. After you call inflate() on this LayoutInflator, you can then get the Button for that specific row and set its OnClickListener.
In bindView(), you do just about all the same things, except that you don't need to inflate a new view since Android is requesting that you reuse an existing View object. You might want to create some extra methods in order to reduce the amount of repeated code.
I hope this helps even though it is explained in English rather than with a code example.

Dynamic population of spinner with custom layout

I have a custom layout item for my listview. There is a spinner in the layout item which needs to be populated with values, usually by android:entries
The problem I have with this method is that the end user can't modify the values, which is something I would like to include. As the layout item and subsequently the spinner, is repeated multiple times on the same listview, I imagine there must be a way to populate it once, programmatically. I just can't figure it out.
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/customListItem"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/exerciselbl" />
<Spinner
android:id="#+id/Exercise"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:entries="#array/workout_items" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/repslbl" />
<Spinner
android:id="#+id/Reps"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:entries="#array/reps_count" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/weightlbl" />
<EditText
android:id="#+id/Weight"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
</LinearLayout>
</LinearLayout>
So yeah, I want to avoid using android:entries="#array/workout_items" as this means manually typing out every single item for the spinner in an XML resources file AND I can't dynamically add items while the program is running.
Here is a simple example of how it would be done, obviously the generics can change and there are other ways of loading the spinner.
public void populateSpinner () {
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
List<CharSequence> list = new ArrayList<CharSequence>();
for (int i = 0; i < 10; i++) {
list.add("Item " + i);
}
adapter.addAll(list);
adapter.notifyDataSetChanged();
}
One of the more important lines is adapter.notifyDataSetChanged(), this is because it tells the view that it needs to refresh the data associated with this spinner.
For more information, see the ArrayAdapter and Spinner classes.

Categories

Resources