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 !
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 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.
I'm really new in Android. I have this problem, I have an Activity (MainActivity) and there is a NavigationDrawer, this switches two fragments - ActivitiesFragment and ReportFragments.
The problem is with ActivitiesFragments, data is not displayed.
I have my adapter ready and my layouts ready and the fragment. When I debugg my app, it actually brings data, but is not shown. This is my code:
ActivitiesAdapter
public class ActivitiesAdapter extends ArrayAdapter<Activities>
{
Context mContext;
int mLayoutResourceId;
public ActivitiesAdapter(Context context, int layoutResourceId)
{
super(context, layoutResourceId);
mContext = context;
mLayoutResourceId = layoutResourceId;
}
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
final Activities currentItem = getItem(position);
if (row == null)
{
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
row = inflater.inflate(mLayoutResourceId, parent, false);
}
row.setTag(currentItem);
final TextView tituloview = (TextView) row.findViewById(R.id.tituloAct);
tituloview.setText(currentItem.getTitle());
final TextView descrpview = (TextView) row.findViewById(R.id.descrAct);
descrpview.setText(currentItem.getDescription());
return row;
}
}
This is my fragment ActivitiesFragment
public class ActivitiesFragment extends Fragment
{
protected static final String TAG = "ActivitiesFragmment";
private MobileServiceClient mClient;
private MobileServiceTable<Activities> mActivitiesTable;
private ActivitiesAdapter mAdapter;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.act_listfragment, container, false);
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
try
{
mClient = new MobileServiceClient("https://site.azure-mobile.net/",
"APPLICATIONKEYAPPLICATIONKEY",
getActivity().getApplicationContext()).withFilter(new ProgressFilter());
mActivitiesTable = mClient.getTable(Activities.class);
}
catch (MalformedURLException e)
{
createAndShowDialog(new Exception("There was an error creating the Mobile Service. Verify the URL"), "Error");
}
mAdapter = new ActivitiesAdapter(getActivity(), R.layout.act_itemlist);
ListView listViewToDo = (ListView) getView().findViewById(R.id.activities_fragment_list);
listViewToDo.setAdapter(mAdapter);
refreshItemsFromTable();
}
private void createAndShowDialog(Exception exception, String title)
{
createAndShowDialog(exception.toString(), title);
}
private void createAndShowDialog(String message, String title)
{
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity().getApplicationContext());
builder.setMessage(message);
builder.setTitle(title);
builder.create().show();
}
private void refreshItemsFromTable()
{
mActivitiesTable.execute(new TableQueryCallback<Activities>()
{
public void onCompleted(List<Activities> result, int count, Exception exception, ServiceFilterResponse response) {
if (exception == null) {
mAdapter.clear();
for (Activities item : result) {
mAdapter.add(item);
Log.i(TAG, "Titulo: " + item.getTitle());
}
} else {
createAndShowDialog(exception, "Error");
}
}
});
}
}
I don't know whats wrong, but I guess this block is not working on the OnActivityCreated method:
mAdapter = new ActivitiesAdapter(getActivity(), R.layout.act_itemlist);
ListView listViewToDo = (ListView) getView().findViewById(R.id.activities_fragment_list);
listViewToDo.setAdapter(mAdapter);
refreshItemsFromTable();
And here are my Layouts:
This is my frgament's layout:
<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:background="#e1e1e1"
android:orientation="vertical" >
<TextView
android:id="#+id/tvTituloActs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/all_activities"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:textColor="#009ad2"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ListView android:id="#+id/activities_fragment_list"
android:layout_marginTop="8dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:layout_weight="1"
tools:listitem="#layout/act_itemlist"
android:drawSelectorOnTop="false"/>
<TextView android:id="#id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="No hay datos"/>
</LinearLayout>
This is the layout act_itemlist. The row layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="64dp"
android:orientation="horizontal">
<ImageView
android:id="#+id/icon"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginLeft="8dp"
android:gravity="center_vertical"
android:src="#drawable/done"
android:layout_gravity="center_vertical"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/tvTituloAct"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginTop="16dp"
android:textSize="16sp"/>
<TextView
android:id="#+id/tvDescrAct"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:text="Description de la actividad" />
</LinearLayout>
</LinearLayout>
I really need help with this! Please if someone can see something that I'm missing please tell me!
Thanks!!!!
Try to swap these lines:
mAdapter = new ActivitiesAdapter(getActivity(), R.layout.act_itemlist);
ListView listViewToDo = (ListView) getView().findViewById(R.id.activities_fragment_list);
refreshItemsFromTable(); // populate first before attaching your adapter
listViewToDo.setAdapter(mAdapter);
I finally solved my problem. Everything was Ok, the only detail was the layout. It was on the fragments layout.
<TextView android:id="#id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="No hay datos"/>
This textview did not let to show the data so I deleted it and worked.
Thankk you for your help #LazyNinja
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 just getting my feet wet with Android and have built a UI that contains a TabHost with three tabs. Each tab is powered by its own Activity. The first Tab contains a listview with a prepopulated set of rows and is built from a custom ArrayAdapter.
The problem I'm running into is that none of the ListView rows are tappable. In other words, when I tap on them there is no orange selection. If I use the scroll ball on my Nexus One it will select, but any touch gestures don't seem to be responding.
All the UI is being handled using XML files with a main.xml housing the TabHost -> LinearLayout -> TabWidget/FrameLayout and a nearby_activity.xml file containing my ListView UI
<?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/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/nearby_no_events"
/>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1.0"
android:choiceMode="multipleChoice"
android:divider="#d9d9d9"
android:dividerHeight="1px"
android:cacheColorHint="#eee"
/>
</LinearLayout>
And the relevant code from my Activity that is set to show in the selected tab.
public class NearbyActivity extends ListActivity
{
private ArrayList<Event> m_events = null;
private EventAdapter m_adapter = null;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.nearby_activity);
getEvents();
this.m_adapter = new EventAdapter(this, R.layout.eventrow, m_events);
setListAdapter(this.m_adapter);
}
private void getEvents()
{
m_events = new ArrayList<Event>();
for (int i = 0; i < 100 ; i++)
{
Event e = new Event();
e.setEventName("Event " + i);
e.setVenueName("Staples Center");
e.setStartDate(new Date());
m_events.add(e);
}
}
private class EventAdapter extends ArrayAdapter<Event>
{
private ArrayList<Event> items;
public EventAdapter(Context context, int textViewResourceId, ArrayList<Event> items)
{
super(context, textViewResourceId, items);
this.items = items;
}
#Override
public View getView (int position, View convertView, ViewGroup parent)
{
View v = convertView;
if (v == null)
{
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.eventrow, null);
}
Event e = items.get(position);
if (e != null)
{
TextView nameText = (TextView)v.findViewById(R.id.eventName);
TextView venueNameText = (TextView)v.findViewById(R.id.venueName);
if (nameText != null)
{
nameText.setText(e.getEventName());
}
if(venueNameText != null)
{
venueNameText.setText(e.getVenueName());
}
}
return v;
}
}
}
My listview row's are populated by an XML file as well.
<?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="?android:attr/listPreferredItemHeight"
android:orientation="vertical"
android:padding="4dip">
<TextView
android:id="#+id/eventName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:inputType="text"
android:singleLine="true"
android:ellipsize="marquee"
android:textSize="18sp"
android:textColor="#000"
/>
<TextView
android:id="#+id/venueName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/eventName"
android:layout_alignParentBottom="true"
android:layout_marginRight="55dip"
android:singleLine="true"
android:ellipsize="end"
android:scrollHorizontally="true"
android:textSize="13sp"
android:textColor="#313131"
android:layout_alignWithParentIfMissing="true"
android:gravity="center_vertical"
/>
</RelativeLayout>
Thanks for any help you can offer.
You want a Listview tappable, well you need implement the method onListItemClick()
#Override
protected void onListItemClick(ListView l, View v, final int position, long id) {
super.onListItemClick(l, v, position, id);
Toast.makeText(this, "This is my row number " + position,Toast.LENGTH_LONG).show();
}
and to ensure the orange colour you must set the property
android:focusable="false"
in your Listview Row.xml