I have 4 fields on a custom row layout for a listview. I have 4 fields in my array that I want to map to those 4 fields on the listview:
cat_ID_PK
cat_name
cat_amount
is_recurring
I have no idea how to correctly do that. Here's what I have so far:
private void loadListView(Expenses[] mExpenseArray) {
//This code will write the "name" variable correctly to the logcat, so I know I'm
getting the right values in my array.
String name = mExpenseArray[0].getCatName();
Log.v("log_tag", name);
ArrayAdapter<Expenses> adapter = new ArrayAdapter<Expenses>(getApplicationContext(), R.layout.list_row, R.id.catName, mExpenseArray);
final ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);
}
This code only populates R.id.catName, but when it populates it, it looks like this: mypackage#8ed455 (or something similar). None of the other fields are populated at all, which I'm guessing has to do with the 3rd parameter in my ArrayAdapter being R.id.catName. However, if I take this parameter out I get this error:
java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
Here is the code for my custom row layout:
<?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:background="#drawable/list_selector"
android:orientation="horizontal"
android:padding="5dip" >
<TextView
android:id="#+id/catName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_centerVertical="true"
android:paddingBottom ="10dip"
android:text="#string/catName"
android:textColor="#040404"
android:textSize="25dip"
android:textStyle="bold"
android:typeface="sans" />
<TextView
android:id="#+id/catAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="27dip"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:paddingBottom ="10dip"
android:text="$45.00"
android:textColor="#040404"
android:textSize="25dip"
android:textStyle="bold"
android:typeface="sans" />
<TextView
android:id="#+id/catType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/catName"
android:layout_alignLeft="#+id/catName"
android:paddingTop="5dip"
android:layout_centerHorizontal="true"
android:text="#string/catType"
android:textColor="#343434"
android:textSize="15dip" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="catID"
android:id="#+id/catID"
android:layout_alignBottom="#+id/catType"
android:layout_toRightOf="#+id/catType"
android:layout_toEndOf="#+id/catType"
android:layout_marginLeft="27dp"
android:visibility="invisible" />
</RelativeLayout>
How can I correctly map my 4 array fields to my ListView?
make a custom adapter like this and then assign the values to the text views
pass array like this to adapter
UserList disadpt = new UserList(HomePage.this, mEmployeeList);
userList.setAdapter(disadpt);
then in adapter do this ..
public class UserList extends BaseAdapter
{
private Context mContext;
private ArrayList<Employee> items;
public UserList(Context c, ArrayList<Employee> items)
{
this.mContext = c;
this.items = items;
}
public int getCount() {
return this.items.size();
}
public Object getItem(int position) {
return this.items.get(position);
}
public long getItemId(int position) {
return position;
}
#Override
public View getView(int pos, View child, ViewGroup arg2) {
Holder mHolder;
LayoutInflater layoutInflater;
if (child == null) {
layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
child = layoutInflater.inflate(R.layout.listcell, null);
mHolder = new Holder();
//mHolder.txt_id = (TextView) child.findViewById(R.id.txt_id);
mHolder.txt_fName = (TextView) child.findViewById(R.id.txt_fName);
mHolder.txt_lName = (TextView) child.findViewById(R.id.txt_lName);
mHolder.txt_userName = (TextView) child.findViewById(R.id.txt_userName);
child.setTag(mHolder);
} else {
mHolder = (Holder) child.getTag();
}
Employee employee = this.items.get(pos);
ArrayList<String> employeeInfo = new ArrayList<String>();
employeeInfo.add(employee.employeeId);
employeeInfo.add(employee.firstName);
employeeInfo.add(employee.lastName);
mHolder.Details.setTag(employeeInfo);
mHolder.txt_fName.setText(employee.firstName + " " + employee.lastName);
mHolder.txt_userName.setText(employee.emailId);
return child;
}
public class Holder
{
//TextView txt_id;
TextView txt_fName;
TextView txt_lName;
TextView txt_userName;
Button Details;
}
}
hope this helps ...
Create String type of array Adapter and use-
adapter.add(mExpenseArray[0].getCatName());
by using any loop.
Related
I am struggling from Listview , how to add two value in this Listview , i am using SimpleAdapter but i can't add two value , Listview shows only one item from database , how to add two values from mysql database . How can i add two values ?
//this is my layout file
//main activity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="0dp">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="New Class :"
android:textSize="17dp"
android:fontFamily="serif"
android:textColor="#000"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_weight="1"/>
<Spinner
android:id="#+id/new_class"
style="#style/Platform.Widget.AppCompat.Spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:background="#drawable/spinner_background"
android:popupBackground="#fff"
android:fontFamily="serif"
android:layout_marginRight="10dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:choiceMode="multipleChoice"
android:focusable="false"/>
</LinearLayout>
</LinearLayout>
//this is my row activity file for custom layout
<?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="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/thumbImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="sample text"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceMedium" />
<CheckedTextView
android:id="#+id/itemCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:focusable="false"
android:focusableInTouchMode="false"/>
</LinearLayout>
</RelativeLayout>
//this is my code
listview.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
sparseBooleanArray = listview.getCheckedItemPositions();
String ValueHolder = "" ;
int i = 0 ;
while (i < sparseBooleanArray.size()) {
if (sparseBooleanArray.valueAt(i)) {
ValueHolder += data4.get(sparseBooleanArray.keyAt(i)) + ",";
}
i++ ;
}
ValueHolder = ValueHolder.replaceAll("(,)*$", "");
Toast.makeText(MainActivity.this, ValueHolder, Toast.LENGTH_LONG).show();
}
});
how to create custom array adapter please help me
just use custom array adapter as shown below
this is my adapter class
public class ItemAdapter extends ArrayAdapter<User> {
// declaring our ArrayList of items
private ArrayList<User> objects;
/* here we must override the constructor for ArrayAdapter
* the only variable we care about now is ArrayList<User> objects,
* because it is the list of objects we want to display.
*/
public ItemAdapter(Context context, int textViewResourceId, ArrayList<User> objects) {
super(context, textViewResourceId, objects);
this.objects = objects;
}
/*
* we are overriding the getView method here - this is what defines how each
* list item will look.
*/
public View getView(int position, View convertView, ViewGroup parent){
// assign the view we are converting to a local variable
View v = convertView;
// first check to see if the view is null. if so, we have to inflate it.
// to inflate it basically means to render, or show, the view.
if (v == null) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.list_item, null);
}
/*
* Recall that the variable position is sent in as an argument to this method.
* The variable simply refers to the position of the current object in the list. (The ArrayAdapter
* iterates through the list we sent it)
*
* Therefore, i refers to the current User object.
*/
User i = objects.get(position);
if (i != null) {
// This is how you obtain a reference to the TextViews.
// These TextViews are created in the XML files we defined.
TextView tt = (TextView) v.findViewById(R.id.toptext);
TextView ttd = (TextView) v.findViewById(R.id.toptextdata);
TextView mt = (TextView) v.findViewById(R.id.middletext);
TextView mtd = (TextView) v.findViewById(R.id.middletextdata);
TextView bt = (TextView) v.findViewById(R.id.bottomtext);
TextView btd = (TextView) v.findViewById(R.id.desctext);
// check to see if each individual textview is null.
// if not, assign some text!
if (tt != null){
tt.setText("Name: ");
}
if (ttd != null){
ttd.setText(i.getName());
}
if (mt != null){
mt.setText("Price: ");
}
if (mtd != null){
mtd.setText("$" + i.getPrice());
}
if (bt != null){
bt.setText("Details: ");
}
if (btd != null){
btd.setText(i.getDetails());
}
}
// the view must be returned to our activity
return v;
}
}
set an instance of custom adapter in your list view like this
ItemAdapter itemAdapter = new ItemAdapter(...);
listView.setAdapter(itemAdapter)
if it helped please verify the answer
I am developing an app where I show a dropdownlist using a spinner.
I used a custom adapter, here is the code:
public class DemandeCongeAdapter extends BaseAdapter {
public static final String TAG = "DemandeCongeAdapter";
private final Context mContext;
private List<DemandeCongeType> mData;
protected LayoutInflater mInflater;
public DemandeCongeAdapter(Context mContext, List<DemandeCongeType> mData) {
this.mContext = mContext;
this.mData = mData;
this.mInflater = LayoutInflater.from(mContext);
}
static class ViewHolder {
public ViewHolder(View v) {
typedeDemandeName = (TextView) v.findViewById(R.id.type_demande_name);
}
protected final TextView typedeDemandeName;
}
#Override
public int getCount() {
return mData.size();
}
#Override
public DemandeCongeType getItem(int position) {
return mData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.layout_create_demande_conge_custom_spinner, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
if (position != 0) {
DemandeCongeType item = getItem(position);
String name = item.getTypdeDemandeName() == null ? "" : item.getTypdeDemandeName();
viewHolder.typedeDemandeName.setText(name);
}
return convertView;
}
}
And for the activity code, here is it:
private List<DemandeCongeType> congeTypes;
private DemandeCongeAdapter spinnerCongeTypeArrayAdapter;
congeTypes.addAll(new ArrayList<>((List<DemandeCongeType>) responseBody));
congeTypes.add(0, new DemandeCongeType());
spinnerCongeTypeArrayAdapter = new
DemandeCongeAdapter(DemandeCongeNewActivity.this, congeTypes);
congeTypeSpinner.setAdapter(spinnerCongeTypeArrayAdapter);
congeTypeSpinner.setSelection(Adapter.NO_SELECTION, false);
Everything works fine, but when I select the spinner for the first time, it shows the list correctly like this:
Once I click outside the spinner and I click for the second time on the spinner, I got it like this:
So I can't the find the problem, please if anyone can help me with that.
Here is the XML code:
<Spinner
android:id="#+id/spinner_type_conge"
fontPath="fonts/light.otf"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:background="#null"
android:dropDownWidth="match_parent"
android:ellipsize="end"
android:lines="1"
android:paddingRight="2dp"
android:spinnerMode="dropdown"
android:textSize="13sp" />
And for the spinner item xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/type_demande_name"
fontPath="fonts/light.otf"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:lines="1"
android:padding="10dp"
android:textColor="#color/annuaire_hint_color"
android:textSize="13sp" />
Try This way i have use this in my Application.
XML Design Code.
Spinner set in your Activity
<Spinner
android:id="#+id/sp_pen_Category"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="#android:color/transparent"
android:padding="#dimen/margin_10" />
This file XML File sp_list.xml
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="marquee"
android:padding="15dp"
android:singleLine="true"
android:textColor="#color/black"
android:textSize="#dimen/sub_title">
</CheckedTextView>
This file XML File sp_items.xml
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="marquee"
android:singleLine="true"
android:textColor="#color/black">
</CheckedTextView>
This is Java code when you fill spinner.
ArrayAdapter<String> adp_sp = new ArrayAdapter<String>(Activity.this, R.layout.sp_items, arr_spinner);
adp_sp.setDropDownViewResource(R.layout.sp_list);
sp_pen_Category.setAdapter(adp_sp);
How do you pass an Arraylist of objects into an adapter, and then into a listview? I'm trying to pass the Group Name, which is a property of Group, into the listview. If it is possible, I would also like to pass the ID as a value (in case of a click).
private void populateList(){
// Enable Local Datastore.
Parse.enableLocalDatastore(this);
ParseObject.registerSubclass(Group.class);
//PARSE INITI REMOVED
ParseQuery<ParseObject> query = ParseQuery.getQuery("Group");
// query.whereEqualTo("playerName", "Dan Stemkoski");
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> scoreList, ParseException e) {
if (e == null) {
Log.d("Name", "Retrieved " + scoreList.size() + " groups");
for (ParseObject gGroup : scoreList) {
Log.d("Name", "Retrieved " + gGroup.toString() + " groups");
Group newGroup = new Group();
newGroup.setName(gGroup.toString());
listGroups.add(newGroup);
}
} else {
Log.d("Name", "Error: " + e.getMessage());
}
}
});
//build
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.da_list,R.id.name,listGroups);
//list
ListView mGroups = (ListView) findViewById(R.id.uiGroups);
mGroups.setAdapter(adapter);
}
XML View
<?xml version="1.0" encoding="utf-8"?>
<ListMenuItemView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<!-- The title and summary have some gap between them, and this 'group' should be centered vertically. -->
<RelativeLayout
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:duplicateParentState="true">
<TextView
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:textAppearance="?android:attr/textAppearanceLargePopupMenu"
android:singleLine="true"
android:duplicateParentState="true"
android:ellipsize="marquee"
android:fadingEdge="horizontal" />
</RelativeLayout>
</ListMenuItemView>
You have to create your own Adapter. The skeleton looks like this :
public class MySimpleArrayAdapter extends ArrayAdapter<Group> {
private Context mContext;
private ArrayList<Group> mGroup;
public MySimpleArrayAdapter(Context context, ArrayList<Group> group) {
mContext = context;
mGroup = group;
}
#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.rowlayout, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.text);
textView.setText(mGroup.get(position).getGroupName());
return rowView;
}
}
Note : This adapter does not take into account possible improvements to the management of the ListView. look at this link for more precision : http://developer.android.com/training/improving-layouts/smooth-scrolling.html
Where R.layout.rowlayout is something like that :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp">
<TextView android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:textSize="14sp" />
</LinearLayout>
Now you have to set this adapter to your ListView :
ListView listView = (ListView) findViewById(R.id.listView);
MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(getActivity().getApplicationContext(), groupArrayList);
listView.setAdapter(adapter);
Have a nice day !
I am getting a string[] Array and storing it in an String array [variable mystrings],Now i want this array to display it in a list view with check box .
ec_checkbox_number.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" >
<CheckBox
android:id="#+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="26dp"
android:text="number" />
<TextView
android:id="#+id/checkboxtextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/checkBox"
android:layout_alignBottom="#+id/checkBox"
android:layout_alignParentLeft="true"
android:layout_marginLeft="107dp"
android:text="TextView" />
</RelativeLayout>
ec_number_selection.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/meetingprofilename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:text="Profile Name:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/meetingprofilename"
android:layout_alignBottom="#+id/meetingprofilename"
android:layout_alignParentRight="true"
android:layout_toRightOf="#+id/meetingprofilename"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="#+id/cancelnumberbutton"
android:layout_width="158dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignRight="#+id/editText2"
android:text="Cancel" />
<Button
android:id="#+id/donenumberbutton"
android:layout_width="158dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_toRightOf="#+id/cancelnumberbutton"
android:text="Done" />
<ListView
android:id="#+id/numberselectionlistView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/cancelnumberbutton"
android:layout_alignParentLeft="true"
android:layout_below="#+id/editText1" >
</ListView>
</RelativeLayout>
CheckBoxData.java
public class CheckBoxData {
private String[] number;
public String[] getNumber() {
return number;
}
public void setNumber(String[] number) {
this.number = number;
}
}
MyConferenceNumber.java
public class EcConferenceNumber extends Activity{
ListView checkBoxNumberListView;
ConferenceAdapter adapter;
Button doneBtn,cancelBtn;
EditText profileName;
MyCustomAdapter dataAdapter = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ec_number_selection);
adapter = new ConferenceAdapter(this);
checkBoxNumberListView = (ListView) findViewById(R.id.numberselectionlistView);
doneBtn=(Button) findViewById(R.id.donenumberbutton);
cancelBtn=(Button) findViewById(R.id.cancelnumberbutton);
Intent intent = getIntent();
String[] myStrings = intent.getStringArrayExtra("strings");
List<String> strings =
new ArrayList<String>(Arrays.asList(myStrings));
System.out.println(""+strings);
dataAdapter = new MyCustomAdapter(this,
R.layout.ec_checkbox_number, strings);
checkBoxNumberListView.setAdapter(dataAdapter);
checkBoxNumberListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
CheckBoxData country = (CheckBoxData) parent.getItemAtPosition(position);
Toast.makeText(getApplicationContext(),
"Clicked on Row: " + country.getNumber(),
Toast.LENGTH_LONG).show();
}
});
}
class MyCustomAdapter extends ArrayAdapter<String>{
private List<String> strings;
public MyCustomAdapter(Context context, int textViewResourceId, List<String> strings) {
super(context, textViewResourceId,strings);
this.strings=new ArrayList<String>();
this.strings.addAll(this.strings);
// TODO Auto-generated constructor stub
}
private class ViewHolder {
TextView code;
CheckBox name;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Log.v("ConvertView", String.valueOf(position));
if (convertView == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.ec_checkbox_number, null);
holder = new ViewHolder();
holder.code = (TextView) convertView.findViewById(R.id.checkboxtextView);
holder.name = (CheckBox) convertView.findViewById(R.id.checkBox);
convertView.setTag(holder);
holder.name.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v ;
CheckBoxData country = (CheckBoxData) cb.getTag();
Toast.makeText(getApplicationContext(),
"Clicked on Checkbox: " + cb.getText() +
" is " + cb.isChecked(),
Toast.LENGTH_LONG).show();
}
});
}
else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
}
}
The output what i am getting is..
For example:If String[] a={002,111,122} i want to display it in a listview with checkbox and i have to get the selected CheckBox text.But my output is displying like this
number
number
number
number
But i want the output like this ,
123
112
123
Since ,I am new to android i did not know how to set the array to output view.Any answers will be helpfull.
Paste your full xml code. There is no button in xml and some properties are missing.
I'm completely new to Java/Android and am stuck with listviews.
What I want is to have 4 lines in a row in listview with different font styles
rowlayout.xml
<TextView
android:id="#+id/1"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/2"
android:textSize="30sp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/3"
android:typeface="sans"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/4"
android:typeface="sans"
android:textSize="15sp"
android:textStyle="italic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
I have this in a separate xml file
results.xml
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#id/TitleLbl" >
</Listview>
and my array adapter looks like this
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.rowlayout, R.id.2, Array);
setListAdapter(adapter);
What I'm trying to do is get an arraylist (containing ID, name, surname and number) to populate the listview so that each information is on its own line in the row.
the array adapter above works, but each of the four informations is in their own row.
Can anyone help?
(also, I've scoured the internet to try and sort this out, but can't seem to find anything that helps me)
You may need to use custom list view with custom array adapter. Refer this link for example.
Content:
The Android HelloListView ( http://developer.android.com/resources/tutorials/views/hello-listview.html ) tutorial shows how to bind a ListView to an array of string objects, but you'll probably outgrow that pretty quickly. This post will show you how to bind the ListView to an ArrayList of custom objects, as well as create a multi-line ListView.
Let's say you have some sort of search functionality that returns a list of people, along with addresses and phone numbers. We're going to display that data in three formatted lines for each result, and make it clickable.
First, create your new Android project, and create two layout files. Main.xml will probably already be created by default, so paste this in:
<?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">
<TextView
android:layout_height="wrap_content"
android:text="Custom ListView Contents"
android:gravity="center_vertical|center_horizontal"
android:layout_width="fill_parent" />
<ListView
android:id="#+id/ListView01"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
</LinearLayout>
Next, create a layout file called custom_row_view.xml. This layout will be the template for each individual row in the ListView. You can use pretty much any type of layout - Relative, Table, etc., but for this we'll just use Linear:
<?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">
<TextView android:id="#+id/name"
android:textSize="14sp"
android:textStyle="bold"
android:textColor="#FFFF00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/cityState"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Now, add an object called SearchResults. Paste this code in:
public class SearchResults {
private String name = "";
private String cityState = "";
private String phone = "";
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setCityState(String cityState) {
this.cityState = cityState;
}
public String getCityState() {
return cityState;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getPhone() {
return phone;
}
}
This is the class that we'll be filling with our data, and loading into an ArrayList.
Next, you'll need a custom adapter. This one just extends the BaseAdapter, but you could extend the ArrayAdapter if you prefer.
public class MyCustomBaseAdapter extends BaseAdapter {
private static ArrayList<SearchResults> searchArrayList;
private LayoutInflater mInflater;
public MyCustomBaseAdapter(Context context, ArrayList<SearchResults> results) {
searchArrayList = results;
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return searchArrayList.size();
}
public Object getItem(int position) {
return searchArrayList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.custom_row_view, null);
holder = new ViewHolder();
holder.txtName = (TextView) convertView.findViewById(R.id.name);
holder.txtCityState = (TextView) convertView.findViewById(R.id.cityState);
holder.txtPhone = (TextView) convertView.findViewById(R.id.phone);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtName.setText(searchArrayList.get(position).getName());
holder.txtCityState.setText(searchArrayList.get(position).getCityState());
holder.txtPhone.setText(searchArrayList.get(position).getPhone());
return convertView;
}
static class ViewHolder {
TextView txtName;
TextView txtCityState;
TextView txtPhone;
}
}
(This is basically the same as the List14.java API demo)
Finally, we'll wire it all up in the main class file:
public class CustomListView extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<SearchResults> searchResults = GetSearchResults();
final ListView lv1 = (ListView) findViewById(R.id.ListView01);
lv1.setAdapter(new MyCustomBaseAdapter(this, searchResults));
lv1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Object o = lv1.getItemAtPosition(position);
SearchResults fullObject = (SearchResults)o;
Toast.makeText(ListViewBlogPost.this, "You have chosen: " + " " + fullObject.getName(), Toast.LENGTH_LONG).show();
}
});
}
private ArrayList<SearchResults> GetSearchResults(){
ArrayList<SearchResults> results = new ArrayList<SearchResults>();
SearchResults sr1 = new SearchResults();
sr1.setName("John Smith");
sr1.setCityState("Dallas, TX");
sr1.setPhone("214-555-1234");
results.add(sr1);
sr1 = new SearchResults();
sr1.setName("Jane Doe");
sr1.setCityState("Atlanta, GA");
sr1.setPhone("469-555-2587");
results.add(sr1);
sr1 = new SearchResults();
sr1.setName("Steve Young");
sr1.setCityState("Miami, FL");
sr1.setPhone("305-555-7895");
results.add(sr1);
sr1 = new SearchResults();
sr1.setName("Fred Jones");
sr1.setCityState("Las Vegas, NV");
sr1.setPhone("612-555-8214");
results.add(sr1);
return results;
}
}
Notice that we first get an ArrayList of SearchResults objects (normally this would be from an external data source...), pass it to the custom adapter, then set up a click listener. The listener gets the item that was clicked, converts it back to a SearchResults object, and does whatever it needs to do.