I'am trying to make a ListView that contains two items (two textView's)
Here is my code:
Header XML:
<?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">
<TextView android:id="#+id/txtHeader"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:textStyle="bold"
android:textSize="22dp"
android:textColor="#FFFFFF"
android:padding="10dp"
android:text="Shoping List"
android:background="#336699" />
</LinearLayout>
Hers is the 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="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp">
<TextView android:id="#+id/prudctName"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textStyle="bold"
android:textSize="22sp"
android:textColor="#000000"
/>
<TextView android:id="#+id/productAmount"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textStyle="bold"
android:textSize="22sp"
android:textColor="#000000"
/>
</LinearLayout>
Here is the MAIN SCREEN XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF">
<ListView
android:id="#+id/shoping_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
Here is the class that represents the object containing the two filed:
public class ShopingListItem
{
public String productName;
public String procuctAmount;
public ShopingListItem(String _productName,String _productAmount)
{
this.procuctAmount=_productAmount;
this.productName=_productName;
}
}
Here is the Adapter class:
public class ShopingListItemAdapter extends ArrayAdapter<ShopingListItem>
{
public Context context;
public int layoutResourceId;
public ShopingListItem[] items;
public ShopingListItemAdapter(Context context,int layoutResourceId, ShopingListItem[] objects)
{
super(context, layoutResourceId, objects);
this.context=context;
this.layoutResourceId=layoutResourceId;
this.items=objects;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ShopingListItemHolder holder = null;
if(row==null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ShopingListItemHolder();
holder.productName=(TextView)row.findViewById(R.id.prudctName);
holder.productAmount=(TextView)row.findViewById(R.id.productAmount);
row.setTag(holder);
}
else
{
holder = (ShopingListItemHolder)row.getTag();
}
ShopingListItem item = items[position];
String amount = item.procuctAmount;
String name = item.productName;
TextView v = holder.productAmount;
TextView vv = holder.productName;
holder.productAmount.setText(amount);
holder.productName.setText(name);
return row;
}
static class ShopingListItemHolder
{
TextView productName;
TextView productAmount;
}
}
Here is the main activity:
public class ShopingListActivity extends Activity
{
private ListView listView1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.shoping_list_screen);
//fill list with temp data
ShopingListItem[] items = fillData();
ShopingListItemAdapter adapter = new ShopingListItemAdapter(this, R.layout.listview_item_row, items);
listView1 = (ListView)findViewById(R.id.shoping_list);
View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null);
listView1.addHeaderView(header);
listView1.setAdapter(adapter);
}
private ShopingListItem[] fillData()
{
ShopingListItem[] items = new ShopingListItem[]
{
new ShopingListItem("Bamba", "2"),
new ShopingListItem("Bisli", "12"),
new ShopingListItem("Shoko", "3"),
new ShopingListItem("Yello Cheese", "2"),
new ShopingListItem("Marak", "7"),
new ShopingListItem("Cola", "2"),
new ShopingListItem("Orez", "3"),
new ShopingListItem("Kaki", "9"),
new ShopingListItem("Battery", "1"),
new ShopingListItem("Bla", "1"),
new ShopingListItem("Bla bla", "100"),
new ShopingListItem("bbb", "200"),
new ShopingListItem("Red", "2"),
};
return items;
}
}
The result of the code is a list but with only one item (the name) the amount isnt add'd
Can anyone find the problem?
Your row layout's first TextView fills the entire row, pushing the second TextView off of the screen. You need to change your row layout. Either use wrap_content:
<TextView android:id="#+id/prudctName"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
... />
Or you can incorporate layout_weight.
Related
I am new to android. I am working at a project and i have a question. How can i take data from a ArrayList<the_class> and populate them to a listview? There is an xml file that contains 6 textviews that must be filled with the context of the class items. Thank you
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="10dp">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:text="origin"
android:textSize="19dp"
android:id="#+id/origin"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="destination"
android:textSize="19dp"
android:paddingLeft="15dp"
android:id="#+id/destination"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
*a part of the xml file.
It's very simple actually.
I created a model
public class Model {
String top, bottom;
public Model(String top, String bottom) {
this.top = top;
this.bottom = bottom;
}
public String getTop() {
return top;
}
public void setTop(String top) {
this.top = top;
}
public String getBottom() {
return bottom;
}
public void setBottom(String bottom) {
this.bottom = bottom;
}
}
Then, I need to create a listview adapter for it.
public class ListAdapter extends ArrayAdapter<Model> {
private Context activityContext;
private List<Model> list;
public static final String TAG = "ListView";
public ListAdapter(Context context, List<Model> list){
super(context, R.layout.single_listview, list);
this.activityContext = context;
this.list = list;
}
#Override
public View getView(final int position, View view, ViewGroup viewGroup){
final ViewHolder viewHolder;
if (view == null) {
view = LayoutInflater.from(activityContext).inflate(R.layout.single_listview, null);
viewHolder = new ViewHolder();
viewHolder.top = (TextView) view.findViewById(R.id.top);
viewHolder.bottom = (TextView) view.findViewById(R.id.bottom);
viewHolder.top.setText(list.get(position).getTop());
viewHolder.bottom.setText(list.get(position).getBottom());
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
return view;
}
private static class ViewHolder {
TextView top;
TextView bottom;
}
}
In my main activity, i do the following code
public class MainActivity extends AppCompatActivity {
ListView listview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview = (ListView) findViewById(R.id.listview);
List<Model> list = new ArrayList<>();
list.add(new Model("top-one", "bot-one"));
list.add(new Model("top-two", "bot-two"));
list.add(new Model("top-three", "bot-three"));
list.add(new Model("top-four", "bot-four"));
list.add(new Model("top-five", "bot-five"));
list.add(new Model("top-six", "bot-six"));
list.add(new Model("top-seven", "bot-seven"));
list.add(new Model("top-eight", "bot-eight"));
ListAdapter adapter = new ListAdapter(listview.getContext(), list);
listview.setAdapter(adapter);
}
}
inside of the main activity layout xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.fuhnatik.customlistview.MainActivity">
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.constraint.ConstraintLayout>
finally, inside of the listview adapter layout 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="?android:attr/listPreferredItemHeight"
android:padding="6dip"
android:orientation="vertical">
<TextView
android:id="#+id/top"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="TOP SECTION HERE"
android:textSize="16sp" />
<TextView
android:id="#+id/bottom"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="BOTTOM SECTION HERE"
android:textSize="12sp" />
</LinearLayout>
result:
Sorry for my English. As research, I want to try to use the spinner with baseAdapter. But I do not know why I spinner does not output anything.
Its xml:
student_list_questions.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/listsQuestions"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp" >
</ListView>
</LinearLayout>
student_list.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:layout_margin="10dp"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/s_textQuestions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="0.5"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:textColor="#7F8C8D"
android:textSize="16dp" />
<Spinner
android:id="#+id/s_spinner_rating"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" />
</LinearLayout>
</LinearLayout>
And my Class:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.student_list_questions);
Spinner sp = new Spinner(this);
SpinnerBaseAdapter adapter = new SpinnerBaseAdapter(this, new ArrayList<String>());
sp.setAdapter(adapter);
//then code
}
And this spinner adapter:
public class SpinnerBaseAdapter extends BaseAdapter {
private List<String> numberList;
private LayoutInflater mInflater;
#SuppressWarnings("unchecked")
public SpinnerBaseAdapter(Context context, Object results) {
numberList = (List<String>) results;
numberList.add("1");
numberList.add("2");
numberList.add("3");
mInflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return numberList.size();
}
#Override
public Object getItem(int position) {
return numberList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null) {
convertView = mInflater.inflate(R.layout.student_list, null);
holder = new ViewHolder();
holder.spinnerValue = (TextView) convertView.findViewById(R.id.s_spinner_rating);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.spinnerValue.setText(numberList.get(position));
return convertView;
}
static class ViewHolder {
TextView spinnerValue;
}
}
I have a TableLayout in my application. I have Two definite Rows so far. One Row has static items assigned to it, and the other has 3 spinners assigned top it.
What I'd like to do is, add a new TableRow when ever I click on a button. So far I have the following code:
<TableRow
android:layout_width="fill_parent">
android:stretchColumns="0,1,2"
<Spinner
android:padding="3dip"
android:gravity="left"
android:id="#+id/Spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp" />
<Spinner
android:padding="3dip"
android:gravity="left"
android:id="#+id/Spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp" />
<Spinner
android:padding="3dip"
android:gravity="left"
android:id="#+id/spinner3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp" />
</TableRow>
This is just me second row, the row that I want to add when ever I click said button. MY question is, how do I add the following row into my view, every time the user clicks the button? The values of the spinners will differ, but the variables will stay the same.
Edit:
More code:
/* Find Tablelayout defined in teh XML file */
TableLayout tl = (TableLayout) findViewById(R.id.listTable);
/* Create a new row to be added. */
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
/* Create a Button to be the row-content. */
Button b = new Button(this);
b.setText("Add row");
b.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
/* Add Button to row. */
tr.addView(b);
/* Add row to TableLayout. */
tl.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
}
View changed to this:
<TableRow
android:id="#+id/copyRow"
android:layout_width="fill_parent" >
android:stretchColumns="0,1,2"
<Spinner
android:padding="3dip"
android:gravity="left"
android:id="#+id/Spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp" />
<Spinner
android:padding="3dip"
android:gravity="left"
android:id="#+id/Spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp" />
<Spinner
android:padding="3dip"
android:gravity="left"
android:id="#+id/spinner3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp" />
</TableRow>
If It makes a difference, I'm using JellyBean 4.3 as the target API
here is solution with ListView
you can finde all source code here
Activity Class
public class MainActivity extends Activity {
private List<RowItem> rows = new ArrayList<RowItem>();
private ListView list;
private Button addButton;
private SimpleListAdapter listAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get view elements
list = (ListView)findViewById(R.id.list);
addButton = (Button)findViewById(R.id.addButton);
//lets add few items to our list, note that selection item should not exceed length of spinners item
rows.add(new RowItem(0, 0, 0));
rows.add(new RowItem(0, 1, 2));
rows.add(new RowItem(2, 1, 1));
//create adapter and assign it to your list view
listAdapter = new SimpleListAdapter(this, R.layout.list_item, rows);
list.setAdapter(listAdapter);
addButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
rows.add(new RowItem(0, 0, 0));
listAdapter.notifyDataSetChanged();
}
});
}
Adapter Class
public class SimpleListAdapter extends ArrayAdapter<RowItem>{
private Context context;
private int layoutResourceId;
private List<RowItem> data = null;
private ArrayList<String> spinnerArray1 = new ArrayList<String>();
private ArrayList<String> spinnerArray2 = new ArrayList<String>();
private ArrayList<String> spinnerArray3 = new ArrayList<String>();
public SimpleListAdapter(Context context, int layoutResourceId, List<RowItem> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
spinnerArray1.add("S1 One");
spinnerArray1.add("S1 Two");
spinnerArray1.add("S1 Three");
spinnerArray2.add("S2 One");
spinnerArray2.add("S2 Two");
spinnerArray2.add("S2 Three");
spinnerArray3.add("S3 One");
spinnerArray3.add("S3 Two");
spinnerArray3.add("S3 Three");
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ItemHolder holder;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ItemHolder();
holder.Spinner1 = (Spinner)row.findViewById(R.id.Spinner1);
holder.Spinner2 = (Spinner)row.findViewById(R.id.Spinner2);
holder.Spinner3 = (Spinner)row.findViewById(R.id.Spinner3);
//add adapter to spinners so that there is something to select from
ArrayAdapter<String> spinnerArrayAdapter1 = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_dropdown_item, spinnerArray1);
holder.Spinner1.setAdapter(spinnerArrayAdapter1);
ArrayAdapter<String> spinnerArrayAdapter2 = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_dropdown_item, spinnerArray2);
holder.Spinner2.setAdapter(spinnerArrayAdapter2);
ArrayAdapter<String> spinnerArrayAdapter3 = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_dropdown_item, spinnerArray3);
holder.Spinner3.setAdapter(spinnerArrayAdapter3);
row.setTag(holder);
}
else
{
holder = (ItemHolder)row.getTag();
}
final RowItem rowItem = data.get(position);
holder.Spinner1.setSelection(rowItem.getSelectionIndex1());
holder.Spinner2.setSelection(rowItem.getSelectionIndex2());
holder.Spinner3.setSelection(rowItem.getSelectionIndex3());
holder.Spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Toast.makeText(context, "you selected an item from spinner 1", Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
return row;
}
class ItemHolder{
Spinner Spinner1;
Spinner Spinner2;
Spinner Spinner3;
}
simple model
public class RowItem {
private int selectionIndex1;
private int selectionIndex2;
private int selectionIndex3;
public RowItem(int selectionIndex1, int selectionIndex2, int selectionIndex3) {
super();
this.selectionIndex1 = selectionIndex1;
this.selectionIndex2 = selectionIndex2;
this.selectionIndex3 = selectionIndex3;
}
public int getSelectionIndex1() {
return selectionIndex1;
}
public void setSelectionIndex1(int selectionIndex1) {
this.selectionIndex1 = selectionIndex1;
}
public int getSelectionIndex2() {
return selectionIndex2;
}
public void setSelectionIndex2(int selectionIndex2) {
this.selectionIndex2 = selectionIndex2;
}
public int getSelectionIndex3() {
return selectionIndex3;
}
public void setSelectionIndex3(int selectionIndex3) {
this.selectionIndex3 = selectionIndex3;
}
}
View items: Main view
<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: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=".MainActivity"
android:orientation="vertical"
android:gravity="center" >
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:dividerHeight="5dp"/>
<Button
android:id="#+id/addButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Add Item"
/>
item renderer for list view
<?xml version="1.0" encoding="utf-8"?>
<Spinner
android:id="#+id/Spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:gravity="left"
android:padding="3dip" />
<Spinner
android:id="#+id/Spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:gravity="left"
android:padding="3dip" />
<Spinner
android:id="#+id/Spinner3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:gravity="left"
android:padding="3dip" />
Long time stack user, first time poster. I have followed several Custom ListView Adapter tutorials and have not been able to get this one to work (I have successfully created these adapters before). I have spent way too many hours trying to get this to work!!! Can someone please have a look and hopefully find the silly mistake that I made?
This is in MainActivity.java onCreate() to call the list
//Declaration for the list
private ArrayList<Consumption> meals = new ArrayList<Consumption>();
private RAdapter rAdapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_meal);
//This is the first of two listviews on the activity
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems);
ListView listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(adapter);
//This is the listview I am having problems with
ListView listView2 = (ListView) findViewById(R.id.resultList);
rAdapter = new RAdapter(this,meals);
listView2.setAdapter(rAdapter);
}
I have triple checked that meals has data, it does.
This is the RAdapter.java (The Custom Adapter) code:
public class RAdapter extends ArrayAdapter<Consumption>{
static class holder{
TextView rName;
EditText quan;
ImageButton delete;
}
private ArrayList<Consumption> meals;
private final Context context;
public RAdapter(Context context) {
super(context, R.layout.result_row);
this.context = context;
}
public RAdapter(Context context, ArrayList<Consumption> meals) {
super(context, R.layout.result_row);
this.context = context;
this.meals = meals;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
holder h = null;
if (v == null) {
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
v = inflater.inflate(R.layout.result_row, parent, false);
h = new holder();
h.rName = (TextView) v.findViewById(R.id.resultName);
h.quan = (EditText) v.findViewById(R.id.resultGrams);
h.delete = (ImageButton) v.findViewById(R.id.resultDelete);
v.setTag(h);
}else{
h = (holder) v.getTag();
}
Consumption i = meals.get(position);
h.rName.setText(i.getShortName());
h.quan.setText(i.getQuantity());
return v;
}
}
Here is result_row.xml (The specific row to populate):
<?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="wrap_content" >
<ImageButton
android:id="#+id/resultDelete"
android:layout_marginLeft="10dp"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="#android:drawable/ic_notification_clear_all"
android:contentDescription="#string/meal_remove" />
<TextView
android:id="#+id/resultName"
android:layout_marginLeft="10dp"
android:layout_width="150dp"
android:layout_height="40sp"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/resultDelete" />
<EditText
android:id="#+id/resultQuantity"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_width="50dp"
android:layout_height="40sp"
android:inputType="numberDecimal"
android:layout_toRightOf="#+id/resultName"
android:layout_centerVertical="true" />
<TextView
android:id="#+id/resultGrams"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/resultQuantity"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="#string/grams" />
</RelativeLayout>
Here is activity_meal.xml (which has the listview "resultList" in it):
<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/semislantedbacktransparent"
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=".MealActivity" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/translateButton"
android:layout_width="100dp"
android:layout_height="65dp"
android:background="#drawable/addtorecipe"
android:contentDescription="#string/dummy_button"
android:text="#string/button_add_recipe" />
</LinearLayout>
<SearchView
android:id="#+id/searchView1"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#drawable/search"
android:focusable="true"
android:iconifiedByDefault="false"
android:onClick="enterTomInput"
android:queryHint="Search for food items"
android:showAsAction="always"
android:textColor="#000000" >
</SearchView>
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_alignLeft="#+id/searchView1"
android:layout_below="#+id/searchView1"
android:cacheColorHint="#color/black_overlay" >
</ListView>
<ListView
android:id="#+id/resultList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/linearLayout1"
android:layout_below="#+id/listView1" />
<ImageButton
android:id="#+id/completeButton"
android:layout_width="100dp"
android:layout_height="65dp"
android:layout_alignRight="#+id/resultList"
android:layout_alignTop="#+id/linearLayout1"
android:background="#drawable/completemeal"
android:contentDescription="#string/dummy_button" />
</RelativeLayout>
I can post a logcat, but there is no error message to report. I have tried putting System prints everywhere, but cannot figure the problem out.
Any help would be much appreciated!!
Just change following code and fill your arraylist with data.
public RAdapter(Context context, ArrayList<Consumption> meals) {
super(context, R.layout.result_row);
this.context = context;
this.meals = meals;
}
to
public RAdapter(Context context, ArrayList<Consumption> meals) {
super(context, R.layout.result_row,meals);
this.context = context;
this.meals = meals;
}
in your adapter class.
change your adapter like this
public class RAdapter extends ArrayAdapter<Consumption> {
private Activity activity;
private ArrayList<Consumption> meals;
private LayoutInflater inflater = null;
public RAdapter (Activity act, int resource, ArrayList<Consumption> arrayList) {
super(act, resource, arrayList);
this.activity = act;
this.meals= arrayList;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = inflater.inflate(R.layout.result_row, parent, false);
}
//remaining code
return view;
}
}
and call this adapter
rAdapter = new RAdapter(MainActivity.this,R.layout.result_row,meals);
listView2.setAdapter(rAdapter);
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