I have a ListView set with an ArrayAdapter in my Android app. It lists a set of string values. How do I make it show the row number in front of each list item? Like this:
One
Two
Three
Simply print the position, its in the getView's first param. Dont forget to +1 becuause position starts from 0
I guess you're trying to achieve this,
If so, here's the working code :
MainActivity.java
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
ListView lv;
String[] title = {"Windows","Apple","Android" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.listView1);
CustomAdapter adapter = new CustomAdapter(this,title);
lv.setAdapter(adapter);
}
}
class CustomAdapter extends ArrayAdapter<String>
{
Context context;
String[] title;
CustomAdapter(Context c, String[] title)
{
super(c, R.layout.listitem,title);
this.context = c;
this.title=title;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = vi.inflate(R.layout.listitem, parent, false);
TextView titlee = (TextView) row.findViewById(R.id.textView1);
int pos = position+1;
titlee.setText(+pos + ". " + title[position]);
pos++;
return row;
}
}
activity_main.xml
<RelativeLayout 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" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
lisitem.xml
<?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="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="Title"
android:padding="10dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Related
I am a newbie in Android, so I am really sorry if the error that you will encounter is a blunder. I tried making a Custom ListView (An ImageView and a TextView in each row) by using custom_row.xml as the layout file and CustomAdapter.java as the customized adapter.
I do not understand why this application crashes every time I run the same. The exception (As shown in Android Monitor) is caused at this very line in the CustomAdapter.java file.
View customView = myInflater.inflate(R.layout.custom_row, parent, false);
I am attaching all of the Java and Layout codes for the reference. Thanks.
CustomAdapter.java
package com.abhinavankur.listexample;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ArrayAdapter;
class CustomAdapter extends ArrayAdapter<String> {
CustomAdapter(Context context, String[] teams) {
super(context,R.layout.custom_row,teams);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater myInflater = LayoutInflater.from(getContext());
View customView = myInflater.inflate(R.layout.custom_row, parent, false);
String singleTeam = getItem(position);
TextView myText = (TextView) customView.findViewById(R.id.myText);
ImageView myImage= (ImageView) customView.findViewById(R.id.myImage);
myText.setText(singleTeam);
myImage.setImageResource(R.drawable.mine);
return customView;
}
}
custom_row.xml
<?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">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:id="#+id/myImage"
android:src="#drawable/mine"
android:layout_margin="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/myText"
android:layout_margin="5dp" />
</LinearLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.abhinavankur.listexample.MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/myListView">
</ListView>
</RelativeLayout>
MainActivity.java
package com.abhinavankur.listexample;
import android.os.Bundle;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String teams[] = {"Real Madrid","Manchester United","Barcelona","Chelsea","Bayern Munich","PSG","Borussia Dortmund","Liverpool","Arsenal","Valencia","Villareal","Leicester City","AS Roma"};
ListAdapter myAdapter = new CustomAdapter(this, teams);
ListView myList = (ListView) findViewById(R.id.myListView);
myList.setAdapter(myAdapter);
Toast.makeText(this,"List Shown",Toast.LENGTH_LONG).show();
myList.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String team = String.valueOf(parent.getItemAtPosition(position));
Toast.makeText(MainActivity.this,team,Toast.LENGTH_SHORT).show();
}
}
);
}
}
Change your CustomAdapter as below. You were inflating the layout in a wrong way.
class CustomAdapter extends ArrayAdapter<String> {
Context context;
CustomAdapter(Context context, String[] teams) {
super(context,R.layout.custom_row,teams);
this.context = context
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.custom_row, null);
}
....
....
}
i want to display list of juice object.but here am getting only one object and displaying only one object.getcount() is calling 5 times.but it is not displaying.please help me.
i want to display list of juice object.but here am getting only one object and displaying only one object.getcount() is calling 5 times.but it is not displaying.please help me.
//FoodHome.java
package com.example.hotelmenucard;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class FoodHome extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_food_home);
populateListView();
registerClickCallBack();
}
private void populateListView() {
String[] items={"All Day BreakFast","Salads","Sandwiches","Hot Drinks","Juices"};
ArrayAdapter<String> adapter=new ArrayAdapter<String>(
this, //Context for the activity
R.layout.text_view, //Layout to use(create)
items); //Items to be displayed
ListView list=(ListView)findViewById(R.id.listView1);
list.setAdapter(adapter);
}
private void registerClickCallBack() {
ListView list=(ListView)findViewById(R.id.listView1);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
/*TextView textview=(TextView)view;
String msg="hai"+textview.getText();
Toast.makeText(FoodHome.this, msg, Toast.LENGTH_LONG).show();*/
Intent i = new Intent(FoodHome.this, ItemList.class);
startActivity(i);
}
});
}
}
//ItemList.java
package com.example.hotelmenucard;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class ItemList extends Activity {
private List<Juice> juices = new ArrayList<Juice>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_list);
populateJuiceList();
populateJuiceListView();
Log.i(juices.toString(),"list of juice");
}
private void populateJuiceList() {
juices.add(new Juice("Lemon", 15, "lemon*****", R.drawable.lemon));
juices.add(new Juice("Strawberry", 15, "Strawberry*****",
R.drawable.strawberry));
juices.add(new Juice("Watermelon", 15, "Watermelon*****",
R.drawable.watermelon));
juices.add(new Juice("Pear", 15, "pear*****", R.drawable.pear));
juices.add(new Juice("Pomegranate", 15, "Pomegranate*****",
R.drawable.pomegranate));
System.out.println(juices+"juices in list");
}
private void populateJuiceListView() {
ArrayAdapter<Juice> adapter = new MyListAdapter(R.layout.activity_item_list);
ListView list = (ListView) findViewById(R.id.carsListV);
list.setAdapter(adapter);
}
private class MyListAdapter extends ArrayAdapter<Juice> {
public MyListAdapter(int activityItemList) {
super(ItemList.this, activityItemList, juices);
}
public int getCount() {
System.out.println(juices.size()+"in getcount");
return juices.size();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
RelativeLayout itemView =(RelativeLayout) convertView;
if (itemView == null) {
itemView = (RelativeLayout)getLayoutInflater().inflate(R.layout.item_list,
parent, false);
}
Juice juice = juices.get(position+1);
ImageView imageView = (ImageView)itemView.findViewById(R.id.imageView1);
imageView.setImageResource(juice.getId());
TextView txt=(TextView)itemView.findViewById(R.id.textView1);
txt.setText(juice.getName());
TextView text1=(TextView)itemView.findViewById(R.id.textView2);
text1.setText(juice.getUsing());
itemView.bringToFront();
TextView t=(TextView)itemView.findViewById(R.id.textView3);
t.setText(Integer.toString(juice.getRate()));
System.out.println(itemView.getVisibility());
return itemView;
// return super.getView(position, convertView, parent);
}
}
}
//activity_item_list.xml
<RelativeLayout 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/bg"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.hotelmenucard.ItemList" >
<ListView
android:id="#+id/carsListV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" >
</ListView>
</RelativeLayout>
//item_list.xml (typo mistake changed from item_view.xml)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/bg"
android:gravity="top"
android:textColor="#B2F8F8" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:src="#drawable/lemon" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView1"
android:layout_centerHorizontal="true"
android:text="TextView" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView2"
android:layout_alignBottom="#+id/textView2"
android:layout_alignParentRight="true"
android:text="TextView" />
</RelativeLayout>
instead of ArrayAdapter<String> adapter=new ArrayAdapter<String>(
this,R.layout.text_view,items);
use this
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, items);
I am trying to create a listView with just images. Please this is not a stupid question so any help is appreciated. I think my code is correct, but when I run it, it just shows the contentView but empty. It does not show the images I tell it to show.I have created the java file and then two xml files, one with the listView which is the contentView for the java file, and the other for one single row of the listView.
My java file:
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
public class Craft extends Activity {
int[]images={R.drawable.one,R.drawable.two,R.drawable.three,R.drawable.four,R.drawable.five,R.drawable.six,R.drawable.seven;
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.craft);
listView=(ListView)findViewById(R.id.listView);
MyAdapter adapter=new MyAdapter(Craft.this,images);
listView.setAdapter(adapter);
}
class MyAdapter extends ArrayAdapter<String> {
Context c;
int[] images;
int count = 0;
public MyAdapter(Context c,int imgs[]) {
super(Craft.this, R.layout.single_row);
this.c = Craft.this;
this.images = imgs;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.single_row, parent, false);
Log.d("Plates", "Count: " + count++);
ImageView myImage = (ImageView) row.findViewById(R.id.imageView23);
myImage.setImageResource(images[position]);
return row;
}
}
}
my craft.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:background="#drawable/woodcrop">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:layout_gravity="center_vertical"
android:layout_weight="1" />
My single_row.xml file
<?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:background="#drawable/woodcrop">
<ImageView
android:layout_margin="20dp"
android:layout_width="48dp"
android:layout_height="48dp"
android:src="#drawable/one"
android:id="#+id/imageView23" />
Any help is appreciated!
You should change your ListView height to:
<ListView
.
.
.
android:layout_height="match_parent"/>
I see, you have not overriden getCount() in your adapter:
So your listview dosen't know that there are items.
You should add this to your adapter:
#Override
public int getCount() {
return images.length;
}
Ty to use this super statement
Super(c , R.layout.single_row,imgs);
I have an EditText inside ListView and I want to enter some integer values in all the EditTexts. I have one Button outside the ListView onClicking. From this button I want to get the data from all the EditText from ListView and save in array to show in toast. Also my EditText id is same for all the EditText. Can any buddy give so sample code so I can proceed to next step. Thanks in advance.
main.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="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/listGejalaPilih"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp" >
</ListView>
<Button
android:id="#+id/btnDiagnosis"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Diagnosis Penyakit" >
</Button>
</LinearLayout>
Item.xml
<?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="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textGejalaPilih"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_alignParentLeft="true" />
<EditText
android:id="#+id/editPersenYakin"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:inputType="number"
android:maxLength="3"
android:layout_alignParentRight="true" >
</EditText>
</RelativeLayout>
Main.java
package id.app.pakarsawit;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class DetailDiagnosis extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] pilihan = {"pilihan 1","pilihan 2","pilihan 3", "pilihan 4"};
ListView listView = (ListView)findViewById(R.id.listGejalaPilih);
Button btn = (Button)findViewById(R.id.btnDiagnosis);
ArrayAdapter<String> adapterPilih = new LVAdapterDetailDiagnosis(this, pilihan);
listView.setAdapter(adapterPilih);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
}
});
}
}
LVAdapterDetailDiagnosis.java
package id.app.pakarsawit;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class LVAdapterDetailDiagnosis extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public LVAdapterDetailDiagnosis(Context context, String[] values) {
super(context, R.layout.item);
this.context = context;
this.values = values;
}
#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.item, parent, false);
TextView textView = (TextView)rowView.findViewById(R.id.textGejalaPilih);
textView.setText(values[position]);
return rowView;
}
}
try this:
for (int a = 0; a < listGejalaPilih.getCount(); a++) {
EditText et = (EditText) listGejalaPilih.getChildAt(a).findViewById(R.id.editPersenYakin);
arraylist.add(et.getText().toString());
}
Or if you want Array
String string[] = new String[listGejalaPilih.getCount()];
for (int a = 0; a < listGejalaPilih.getCount(); a++) {
EditText et = (EditText) listGejalaPilih.getChildAt(a).findViewById(R.id.editPersenYakin);
string[a] = et.getText().toString();
}
I use a ListView with a custom adapter to build a list with ckeckboxes. Now I dont know how i can fetch the "ID" from the selected box.
The xml file for the items look like this:
<?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="wrap_content"
android:orientation="vertical"
android:padding="5dp">
<TextView android:id="#+id/class_open"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:onClick="clickHandler" />
<TextView
android:id="#+id/medi_name"
android:text="Test"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#id/checkbox" />
</RelativeLayout>
I need the value from: "class_open"
this is my activity:
package de.bodprod.dkr;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MediNotmediUserListNew extends Activity{
LayoutInflater inflater;
ArrayList<HashMap<String, Object>> medi_notmedi_items;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.medi_notmedi_user_list_new);
ListView mediListCheck = (ListView) findViewById(android.R.id.list);
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
medi_notmedi_items = new ArrayList<HashMap<String,Object>>();
SystemDatabaseHandler db = new SystemDatabaseHandler(getApplicationContext());
medi_notmedi_items = db.getAllNotmedis();
final CustomAdapter adapter = new CustomAdapter(this, R.layout.medi_notmedi_user_list_new, medi_notmedi_items);
mediListCheck.setAdapter(adapter);
}
public void clickHandler(View view) {
}
private class CustomAdapter extends ArrayAdapter<HashMap<String, Object>>{
public CustomAdapter(Context context, int textViewResourceId, ArrayList<HashMap<String, Object>> Strings) {
super(context, textViewResourceId, Strings);
}
private class ViewHolder{
TextView class_open, title;
}
ViewHolder viewHolder;
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
convertView=inflater.inflate(R.layout.medi_notmedi_user_list_new_item, null);
viewHolder=new ViewHolder();
viewHolder.class_open=(TextView) convertView.findViewById(R.id.class_open);
viewHolder.title=(TextView) convertView.findViewById(R.id.medi_name);
convertView.setTag(viewHolder);
}else{
viewHolder=(ViewHolder) convertView.getTag();
}
viewHolder.class_open.setText(medi_notmedi_items.get(position).get("sql_id").toString());
viewHolder.title.setText(medi_notmedi_items.get(position).get("wirk").toString());
return convertView;
}
}
}
How I can get the value from the class_open field in the clickHandler?
Is it possible to assign the value directly to the checkbox and request it?
Is it possible to request the value from the class_open field?
Please excuse my bad english
Markus