how to get checked item from listview - android

this is my code, receives the array list from another activity and displays it into the list view.
public class ItemFavouriteList extends Activity{
ListView favouritelist;
LazyAdapter3 adapter;
ArrayList<HashMap<String, String>> itemfavouriteALL;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_favourite_list);
favouritelist = (ListView) findViewById(R.id.listView1);
itemfavouriteALL = ItemsView.itemfavourite;
adapter = new LazyAdapter3(ItemFavouriteList.this,itemfavouriteALL);
favouritelist.setAdapter(adapter);
}
}
here is the code for the layout "xml" for each line of the list.
<?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" >
<!-- ListRow Left sied Thumbnail image -->
<!-- Title Of Song -->
<TextView
android:id="#+id/item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="category name"
android:textColor="#040404"
android:textSize="25dip"
android:textStyle="bold"
android:typeface="sans" />
<TextView
android:id="#+id/itemId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/item_name"
android:layout_marginTop="1dip"
android:textColor="#android:color/transparent"
android:textSize="10dip" />
<!-- Artist Name -->
<TextView
android:id="#+id/item_desc"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/item_name"
android:layout_marginTop="1dip"
android:layout_toLeftOf="#+id/ck_final_lits"
android:text="Just "
android:textColor="#343434"
android:textSize="20dip" />
<!-- Rightend Duration -->
<!-- Rightend Arrow -->
<CheckBox
android:id="#+id/ck_final_lits"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/item_name"
android:layout_alignBottom="#+id/item_name"
android:layout_alignParentRight="true"
android:text="#string/empty" />
<TextView
android:id="#+id/item_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/item_desc"
android:layout_alignBottom="#+id/item_desc"
android:layout_alignParentRight="true"
android:text="#string/wwww1"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
and this is the adapter i am using.
public class LazyAdapter3 extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public LazyAdapter3(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.list_item3, null);
TextView itemid = (TextView)vi.findViewById(R.id.itemId);
TextView itemname = (TextView)vi.findViewById(R.id.item_name);
TextView itemdesc = (TextView)vi.findViewById(R.id.item_desc);
TextView itemprice = (TextView)vi.findViewById(R.id.item_price);
HashMap<String, String> showdata = new HashMap<String, String>();
showdata = data.get(position);
// Setting all values in listview
itemid.setText(showdata.get(ItemsView.TAG_ITEM_ID));
itemname.setText(showdata.get(ItemsView.TAG_ITEM_NAME_EN));
itemdesc.setText(showdata.get(ItemsView.TAG_ITEM_DESC));
itemprice.setText(showdata.get(ItemsView.TAG_ITEM_PRICE));
return vi;
}
}
how do i get the item id for each checked checkbox ?

Add an OnCheckedChangeListener on your CheckBox, you can do this in the getView. Then create an ArrayList and add/remove the checked/unchecked item to the new ArrayList.

Related

Custom List Adapter not Binding values to TextView

I'm trying to create an android list using a custom adapter and the following XML row layout. I have implemented the following classes however when I run it onto my emulator the "android:id="#+id/firstLine" TextView does not appear to be setting the text! The "id/secondLine" Text View does receive the correct text!
List Row Item XML 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="?android:attr/listPreferredItemHeight"
android:padding="6dip" >
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginRight="6dip"
android:contentDescription="#string/TODO"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/secondLine"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_toRightOf="#id/icon"
android:ellipsize="marquee"
android:singleLine="true"
android:text="#string/purchase_date"
android:textSize="12sp" />
/// THIS IS THE TextView that's not working
<TextView
android:id="#+id/firstLine"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#id/secondLine"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:layout_toRightOf="#id/icon"
android:gravity="center_vertical"
android:text="#string/home_depot"
android:textSize="16sp" />
</RelativeLayout>
Adapter Class:
public class SimpleAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
//public ImageLoader imageLoader;
public SimpleAdapter(Activity a, ArrayList<HashMap<String, String>> d){
activity = a;
data = d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.list_item, null);
TextView purchaseDate = (TextView)vi.findViewById(R.id.secondLine);
TextView seller = (TextView)vi.findViewById(R.id.firstLine);
HashMap<String, String> employee = new HashMap<String, String>();
employee = data.get(position);
// Should this not be setting the text for the "seller" TextView?
purchaseDate.setText(employee.get(HistoryListView.KEY_PURCHASE_DATE));
seller.setText(employee.get(HistoryListView.KEY_SELLER));
return vi;
}
}
List Activity:
public class HistoryListView extends Activity {
static final String KEY_SELLER="seller";
static final String KEY_PURCHASE_DATE="purchase_date";
static final String KEY_SALE_AMMOUNT="sale_ammount";
static final String PURCHASE_DATE="Purchase Date: ";
static final String USD="$ ";
ListView list;
SimpleAdapter adapter;
public String[] sellerList = {"Home Depot","7 Eleven","Costco","Outback Steak House","Philz Coffee"};
public String[] purchaseDateList={"1/12/13", "1/23/13", "2/16/13", "2/17/13", "2/18/13"};
public String[] saleAmmountList={"1023.10","243.98","107.54","45.88","21.99"};
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.history_list);
ArrayList<HashMap<String,String>> expenseList=new ArrayList<HashMap<String, String>>();
Intent intent = getIntent();
for(int i=0; i<5; i++){
HashMap<String, String> map = new HashMap<String, String>();
map.put(KEY_SELLER, sellerList[i]);
map.put(KEY_PURCHASE_DATE, PURCHASE_DATE+purchaseDateList[i]);
//map.put(KEY_SALE_AMMOUNT, saleAmmountList[i]);
expenseList.add(map);
}
ListView list=(ListView)findViewById(R.id.list);
adapter=new SimpleAdapter(this, expenseList);
list.setAdapter(adapter);
}//END onCreate
}//END HistoryListView
You have problem in List Row Item try this one
tested and it show icon and two lines
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/firstLine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="#+id/icon"
android:text="TextView" />
<TextView
android:id="#+id/secondLine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/firstLine"
android:layout_below="#+id/firstLine"
android:text="TextView" />
</RelativeLayout>
try this one..
<TextView
android:id="#+id/firstLine"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#id/secondLine"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:layout_toRightOf="#id/icon"
android:gravity="center_vertical"
android:maxLines="5"// to specify how many lines its occupied or else reduce your size of textsize
android:singleLine="false"
android:ellipsize="end"
android:text="#string/home_depot"
android:textSize="16sp" />
thank you..

Populate list of custom view using ListFragment

I am trying to show elements in a list view using Fragments. I created my custom view as follow
graphical representation of list_row.xml
list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dip" >
<!-- ListRow Left sied Thumbnail image -->
<LinearLayout
android:id="#+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginRight="5dip"
android:padding="3dip" >
<ImageView
android:id="#+id/list_image"
android:layout_width="50dip"
android:layout_height="50dip"
android:contentDescription="#string/image_desc"/>
</LinearLayout>
<!-- Menu name -->
<TextView
android:id="#+id/menu_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/thumbnail"
android:layout_toRightOf="#+id/thumbnail"
android:text="#string/menu_name"
android:textColor="#040404"
android:textSize="15sp"
android:textStyle="bold"
android:typeface="sans" />
<!-- Description -->
<TextView
android:id="#+id/description"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/thumbnail"
android:layout_below="#id/menu_name"
android:layout_marginTop="1dip"
android:layout_toRightOf="#+id/thumbnail"
android:text="#string/description"
android:textColor="#343434"
android:textSize="10sp"
tools:ignore="SmallSp" />
<!-- Price -->
<TextView
android:id="#+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#id/menu_name"
android:layout_marginRight="5dip"
android:gravity="right"
android:text="#string/price"
android:textColor="#10bcc9"
android:textSize="10sp"
android:textStyle="bold"
tools:ignore="SmallSp" />
</RelativeLayout>
In fragment.xml file, I simply put a list view inside a linear layout
fragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
I have an array of menu objects, that has the necessary fields to populate list_row.xml like menu name, description, price and image. I couldn't find a way to fill fragment.xml's listView with the list_row elements. Any help or idea would be appreciated.
PS: I think in fragment.xml instead of android:id="#+id/listView1" field, I have to write android:id="#id/list" since I'm using ListFragment
I solved my problem (as system32 suggests on comment) creating a CustomArrayAdapter class, and setting it as the adapter for my listView.
First I changed android:id="#+id/listView1" to android:id="#android:id/list" in fragment.xml.
CustomArrayAdapter.java
public class CustomArrayAdapter extends ArrayAdapter<Menu> {
Context context;
public CustomArrayAdapter(Context context, int textViewResourceId, List<Menu> objects) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
this.context = context;
}
/*private view holder class*/
private class ViewHolder {
ImageView imageView;
TextView txtMenuName;
TextView txtMenuDesc;
TextView txtPrice;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Menu rowItem = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_row, null);
holder = new ViewHolder();
holder.txtMenuName = (TextView) convertView.findViewById(R.id.menu_name);
holder.txtMenuDesc = (TextView) convertView.findViewById(R.id.description);
holder.txtPrice = (TextView) convertView.findViewById(R.id.price);
holder.imageView = (ImageView) convertView.findViewById(R.id.list_image);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
holder.txtMenuDesc.setText(rowItem.getDescription());
holder.txtMenuName.setText(rowItem.getName());
holder.txtPrice.setText(String.valueOf(rowItem.getPrice()) + " TL");
//holder.imageView.setImageResource(rowItem.getImageId());
return convertView;
}
}
Then I use it in my Fragment class
public static class Fragment extends ListFragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
private ListView listView;
private ArrayList<Menu> menuItems;
private CustomArrayAdapter mAdapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment,
container, false);
listView = (ListView) rootView.findViewById(android.R.id.list);
return rootView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
int num = getArguments().getInt(ARG_SECTION_NUMBER);
// GlobalList is a class that holds global variables, arrays etc
// getMenuCategories returns global arraylist which is initialized in GlobalList class
menuItems = GlobalList.getMenuCategories().get(num).getMenu();
mAdapter = new CustomArrayAdapter(getActivity(), android.R.id.list, menuItems);
listView.setAdapter(mAdapter);
}
}

setadapter for android not working. empty adapter or listview?

im just trying to create a listview using a simple adapter. the setadapter is not working. this is my code. my setcontentview is before this block of code and this is in oncreate.
ArrayList<HashMap<String,String>> arraylist = new ArrayList<HashMap<String,String>>();
HashMap<String,String> map = new HashMap<String,String>();
map.put("CType", "Alarm");
map.put("RType", "Once");
map.put("hour", "1");
map.put("minute", "1");
map.put("second", "30");
arraylist.add(map);
String[] stringarray= new String[] {"CType", "RType", "hour","minute","second"};
int[] intarray = new int[] {R.id.clocktextviewalarmtimer,R.id.clocktextviewrepeatonce,
R.id.clocktextviewhours,R.id.clocktextviewminutes,R.id.clocktextviewseconds};
ListView clocklistview = (ListView)findViewById(R.id.clocklistview);
SimpleAdapter adapter = new SimpleAdapter(this,arraylist,R.layout.list_clock,stringarray,intarray);
clocklistview.setAdapter(adapter);
this is the xml that contains my listview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ListView
android:id="#+id/clocklistview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView></LinearLayout>
this is for my list_clock.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="wrap_content"
android:weightSum="3">
<TextView
android:id="#+id/clocktextviewalarmtimer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/clocktextviewrepeatonce"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/clocktextviewhours"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:layout_weight="0.3"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/clocktextviewminutes"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:layout_weight="0.3"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/clocktextviewseconds"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:layout_weight="0.3"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
Here's an example of a custom adapter. Try this ...
public class ClockListAdapter extends BaseAdapter {
ArrayList<HashMap<String, String>> _arrayList;
ClockListAdapter(Context context, ArrayList<HashMap<String, String>> arrayList) {
this._arrayList = arrayList;
}
#Override
public int getCount() {
return (_arrayList != null) ? _arrayList.size() : 0;
}
#Override
public Object getItem(int position) {
return (_arrayList != null) ? _arrayList.get(position) : null;
}
#Override
public long getItemId(int position) {
return (_arrayList != null) ? _arrayList.indexOf(_arrayList.get(position)) : 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
// REPLACE WITH YOUR LAYOUT FILE vvvvvvvvvv
convertView = layoutInflater.inflate(android.R.layout.simple_list_item_1, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder._textView.setText("set some text here");
return convertView;
}
public class ViewHolder {
// ADD YOUR VIEW(S) vvvvvvvvv
TextView _textView;
ViewHolder(View v) {
// REPLACE WITH YOUR TEXT vvvvvvvvv
_textView = (TextView) v.findViewById(R.id.textView1);
}
}
}

ResourceNotFoundException at custom's listview adapter

I have such a activity with my list:
<?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"
android:orientation="vertical">
<ListView
android:id="#+id/altOrderslist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
/>
</RelativeLayout>
and I want to make custom listview with the items:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dip" >
<!-- ListRow Left sied Thumbnail image -->
<LinearLayout
android:id="#+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginRight="5dip"
android:padding="3dip" >
<TextView
android:id="#+id/altOrderId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rihanna Love the way lie"
android:textColor="#040404"
android:textSize="15dip"
android:textStyle="bold"
android:typeface="sans" />
</LinearLayout>
<!-- Title Of Song -->
<!-- Artist Name -->
<TextView
android:id="#+id/altOrderTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="1dip"
android:layout_toRightOf="#+id/thumbnail"
android:text="Just gona stand there and ..."
android:textColor="#343434"
android:textSize="10dip" />
<!-- Rightend Duration -->
<TextView
android:id="#+id/altOrderStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="5dip"
android:gravity="right"
android:textColor="#10bcc9"
android:textSize="10dip"
android:textStyle="bold" />
<!-- Rightend Arrow -->
<TextView
android:id="#+id/altOrderPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/thumbnail"
android:text="Rihanna Love the way lie"
android:textColor="#040404"
android:textSize="15dip"
android:textStyle="bold"
android:typeface="sans" />
</RelativeLayout>
This is my adapter class:
public class OrderAdapter extends ArrayAdapter<Order>{
Context context;
int layoutResourceId;
List<Order> orders = null;
public OrderAdapter(Context context, int layoutResourceId, List<Order> orders) {
super(context, layoutResourceId, orders);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.orders = orders;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
OrderHolder holder = null;
if(row == null)
{
Log.i("I'm in OrderAdapter",Integer.toString(layoutResourceId));
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(R.layout.dash_alt_item, parent, false);
Log.i("I'm in OrderAdapter",Integer.toString(layoutResourceId));
holder = new OrderHolder();
holder.orderId = (TextView)row.findViewById(R.id.altOrderId);
holder.orderTitle = (TextView)row.findViewById(R.id.altOrderTitle);
holder.orderStatus = (TextView)row.findViewById(R.id.altOrderStatus);
holder.orderPrice = (TextView)row.findViewById(R.id.altOrderPrice);
//row.setTag(holder);
}
else
{
holder = (OrderHolder)row.getTag();
}
Order order = orders.get(position);
holder.orderId.setText(order.getOrderid());
holder.orderTitle.setText(order.getTitle());
holder.orderStatus.setText(order.getProcess_status().getProccessStatusTitle());
holder.orderPrice.setText(Float.toString(order.getPrice()));
return row;
}
static class OrderHolder
{
TextView orderId;
TextView orderTitle;
TextView orderStatus;
TextView orderPrice;
}
}
And how I use it in my Activity:
public class DashboardActivityAlt extends Activity{
static List<Order> orders;
List<Order> forPrint = new ArrayList<Order>();
private ListView listView1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dash_alt);
try {
this.getOrderList("1", "10"); **// filling the forPrint list**
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
OrderAdapter adapter = new OrderAdapter(this,
R.layout.dash_alt_item, **forPrint**);
listView1 = (ListView)findViewById(R.id.altOrderslist);
listView1.setAdapter(adapter);
}
And I get ResourceNotFoundException at the line holder.orderId.setText(order.getOrderid());
What is the reason of it?
Make sure you are not passing an int to that method. Cast it to a String and will start to work as expected.
The reason is setText() is overloaded it has versions:
setText(int resId)
setText(String text)

android - Using AutoCompleTextView to filter Listview with images based on BaseAdapter

I'm pretty new to Android, so please be patient with me!
I want to use AutoCompleTextView to filter a ListView based on custom BaseAdapter. This listview contains both text and images.
This is the layout of listview:
listview_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/backrepeat_dark"
android:padding="0sp" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/options_bar"
android:layout_alignParentTop="true"
android:orientation="vertical" >
<AutoCompleteTextView
android:id="#+id/inputSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Ricerca opere..."
android:inputType="textVisiblePassword"
android:textSize="12dp" >
</AutoCompleteTextView>
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/options_bar"
android:layout_weight="1"
android:cacheColorHint="#80000000"
android:divider="#b5b5b5"
android:dividerHeight="1dp" />
</LinearLayout>
<include
android:id="#+id/options_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="0dp"
android:layout_weight="0"
layout="#layout/menu_main"
android:fadingEdge="horizontal" />
</RelativeLayout>
Here the list row layout:
list_row.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="110dp"
android:background="#drawable/list_selector"
android:orientation="horizontal"
android:padding="5dip" >
<LinearLayout
android:id="#+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginRight="5dip"
android:padding="3dip" >
<ImageView
android:id="#+id/image"
android:layout_width="100dp"
android:layout_height="100dp" />
</LinearLayout>
<TextView
android:id="#+id/pid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="5dip"
android:gravity="right"
android:text="5:45"
android:textColor="#10bcc9"
android:textSize="10dip"
android:textStyle="bold"
android:visibility="gone" />
<!-- Rightend Arrow -->
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="#drawable/arrow" />
<TextView
android:id="#+id/name"
style="?textLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/thumbnail"
android:layout_marginLeft="56dp"
android:layout_toRightOf="#+id/thumbnail"
android:text="TITOLO" />
<TextView
android:id="#+id/autore"
style="?textRegular"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/name"
android:layout_below="#+id/name"
android:text="Autore" />
</RelativeLayout>
This is my custom adapter:
public class LazyAdapterOpere extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater = null;
public ImageLoader imageLoader;
public LazyAdapterOpere(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data = d;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader = new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return data.size();
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.list_row, null);
TextView pid = (TextView) vi.findViewById(R.id.pid);
TextView name = (TextView) vi.findViewById(R.id.name);
TextView autore = (TextView) vi.findViewById(R.id.autore);
ImageView image = (ImageView) vi.findViewById(R.id.image);
HashMap<String, String> opere = new HashMap<String, String>();
opere = data.get(position);
// Setting all values in listview
pid.setText(opere.get(ElencoOpere.TAG_PID));
name.setText(opere.get(ElencoOpere.TAG_NAME));
autore.setText(opere.get(ElencoOpere.TAG_AUTORE));
imageLoader.DisplayImage(opere.get(ElencoOpere.TAG_IMAGE), image);
return vi;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
and this how i fill my custom adapter:
ArrayList<HashMap<String, String>> listaOpere = new ArrayList<HashMap<String, String>>();
// products JSONArray
JSONArray opere = null;
ListView list;
LazyAdapterOpere adapter;
EditText inputSearch;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview_main);
addListenerOnButton();
// Loading products in Background Thread
new LoadAllProducts().execute();
list=(ListView)findViewById(R.id.list);
inputSearch = (EditText) findViewById(R.id.inputSearch);
// getting product details from intent
Intent i = getIntent();
// getting product id (pid) from intent
sala = i.getStringExtra(TAG_SALA);
adapter=new LazyAdapterOpere(this, listaOpere);
adapter.imageLoader.clearCache();
list.setAdapter(adapter);
All works fine, but i don't know how to use filter with this adapter, i just see examples with ArrayAdapter.
Can you please help me?
Thanks in advance
instead of using you can use edittext and listview to do that. you can use textwatcher and custom listview with images

Categories

Resources