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)
Related
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..
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.
I have a custom list View that contains 7 text views..I have created a custom linear layout for that..And then I'm populating it...What i want to do is set a custom color...When the user clicks on the list item and keep it highlighted till he clicks another item...How will i do that??...
This is the custom layout for listitems...
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LLtv"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/colors"
android:cacheColorHint="#00000000" >
<TextView
android:id="#+id/TvVLdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:paddingLeft="8dp"
android:paddingTop="8dp"
android:textColor="#000000"
android:textSize="15dp" />
<TextView
android:id="#+id/TvVLtime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/TvVLdate"
android:paddingLeft="8dp"
android:paddingTop="8dp"
android:textColor="#000000"
android:textSize="15dp" />
<TextView
android:id="#+id/TvVLcardno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/TvVLtime"
android:paddingLeft="8dp"
android:paddingTop="8dp"
android:textColor="#000000"
android:textSize="15dp" />
<TextView
android:id="#+id/TvVLsaletext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/TvVLdate"
android:paddingLeft="8dp"
android:paddingTop="8dp"
android:text="Sale transaction:Rs"
android:textColor="#000000"
android:textSize="15dp" />
<TextView
android:id="#+id/TvVLamnt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/TvVLsaletext"
android:layout_toRightOf="#+id/TvVLsaletext"
android:paddingTop="8dp"
android:textColor="#000000"
android:textSize="15dp" />
<TextView
android:id="#+id/TvVLtexttip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/TvVLamnt"
android:layout_toRightOf="#+id/TvVLamnt"
android:paddingLeft="8dp"
android:paddingTop="8dp"
android:text="Tip:Rs"
android:textColor="#000000"
android:textSize="15dp" />
<TextView
android:id="#+id/TvVLtip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/TvVLtexttip"
android:layout_toRightOf="#+id/TvVLtexttip"
android:paddingTop="8dp"
android:textColor="#000000"
android:textSize="15dp" />
</RelativeLayout>
And my list layout..
<?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:background="#E0E0E0"
android:orientation="vertical" >
<include
android:id="#+id/Hvoid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
layout="#layout/header" />
<RelativeLayout
android:id="#+id/RLvoid"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/Hvoid"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="12dp"
android:text="Main Menu"
android:textColor="#000000"
android:textSize="15dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:src="#drawable/ivoid" />
</RelativeLayout>
<View
android:id="#+id/Vvoidtop"
android:layout_width="fill_parent"
android:layout_height="2dp"
android:layout_below="#+id/RLvoid"
android:background="#android:color/darker_gray" />
<ListView
android:id="#+id/Lvvoid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/Bconfirm"
android:layout_below="#+id/Vvoidtop"
android:background="#E0E0E0"
android:cacheColorHint="#00000000"
android:divider="#android:color/transparent"
android:dividerHeight="20dp" >
</ListView>
<Button
android:id="#+id/Bconfirm"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_above="#+id/Vvoidbot"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="#drawable/cbconfirm" />
<View
android:id="#+id/Vvoidbot"
android:layout_width="fill_parent"
android:layout_height="2dp"
android:layout_above="#+id/Tvfooter"
android:background="#android:color/darker_gray" />
<TextView
android:id="#+id/Tvfooter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:text="© India Transact Services Ltd."
android:textColor="#000000"
android:textSize="15dp" />
</RelativeLayout>
And the drawable colors xml file...
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selected Item -->
<item android:drawable="#android:color/darker_gray" android:state_selected="true"/>
<item android:drawable="#android:color/darker_gray" android:state_focused="true"/>
<!-- Default Item -->
<item android:drawable="#android:color/white" />
</selector>
And my code....
public class Void extends Activity {
ListView lmenu;
View Vlistselector;
String s;
View layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.voidlist);
Menu Menu_data[] = new Menu[] {
new Menu("29/09/2012", "05:40", "2234 xxxx xxxx 1990", "100",
"10"),
new Menu("09/12/2012", "00:34", "2234 xxxx xxxx 1990", "99",
"12"),
new Menu("29/09/2012", "12:20", "2234 xxxx xxxx 1990", "111",
"0"),
new Menu("31/01/2012", "13:40", "2234 xxxx xxxx 1990", "105",
"19"),
new Menu("13/10/2012", "01:40", "2234 xxxx xxxx 1990", "203",
"1"),
new Menu("23/11/2012", "07:40", "2234 xxxx xxxx 1990", "44",
"5") };
MenuAdapter adapter = new MenuAdapter(this, R.layout.voidlisttext,
Menu_data);
lmenu = (ListView) findViewById(R.id.Lvvoid);
lmenu.setAdapter(adapter);
// lmenu.setSelector(R.drawable.listcolorselector);
lmenu.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> ada, View v, int position,
long id) {
// TODO Auto-generated method stub
/*
* v.setSelected(true);
*/
v.setBackgroundColor(Color.parseColor("#FCD5B5"));
if (!(Vlistselector == null) && Vlistselector != v)
Vlistselector.setBackgroundColor(Color
.parseColor("#EEEEEE"));
Vlistselector = v;
Toast.makeText(getApplicationContext(),
String.valueOf(position + 1), Toast.LENGTH_LONG).show();
}
});
findViewById(R.id.BLogout).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
}
public class Menu {
public String date;
public String time;
public String cardno;
public String amnt;
public String tip;
public Menu() {
super();
}
public Menu(String date, String time, String cardno, String amnt,
String tip) {
super();
this.date = date;
this.time = time;
this.cardno = cardno;
this.amnt = amnt;
this.tip = tip;
}
}
public class MenuAdapter extends ArrayAdapter<Menu> {
Context context;
int layoutResourceId;
Menu data[] = null;
public MenuAdapter(Context context, int layoutResourceId, Menu[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
MenuHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context)
.getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new MenuHolder();
holder.date = (TextView) row.findViewById(R.id.TvVLdate);
holder.time = (TextView) row.findViewById(R.id.TvVLtime);
holder.cardno = (TextView) row.findViewById(R.id.TvVLcardno);
holder.amnt = (TextView) row.findViewById(R.id.TvVLamnt);
holder.tip = (TextView) row.findViewById(R.id.TvVLtip);
row.setTag(holder);
} else {
holder = (MenuHolder) row.getTag();
}
Menu Menu = data[position];
holder.date.setText(Menu.date);
holder.time.setText(Menu.time);
holder.cardno.setText(Menu.cardno);
holder.amnt.setText(Menu.amnt);
holder.tip.setText(Menu.tip);
return row;
}
public class MenuHolder {
TextView date;
TextView time;
TextView cardno;
TextView amnt;
TextView tip;
}
}
}
What i want is instead of the color darker gray set a new custom color....
Have i done it the right way or is there any other way to do it?????
View listColor;//declare this at the top
Do it onclick of listviewitem
#Override
protected void onListItemClick(ListView list, View view, int position, long id) {
if(listColor!=null){
listColor.setBackgroundColor(Color.BLACK);
listColor=view;
}else{
listColor=view;
}
listColor.setBackgroundColor(Color.BLUE); //intead of blue you can change it to something you want like darkergray
//show toast on the clicked item
String selectedValue = (String) getListAdapter().getItem(position);
Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show();
}
For this scenario you should use setBackground() at runtime instead of relying on selector.
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
I have a list view with a custom baseadapter ,the code for the class and the adapter is as follows
public class LazyAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.custom_row_view1, null);
TextView title = (TextView)vi.findViewById(R.id.linkname); // merchnts name
TextView artist = (TextView)vi.findViewById(R.id.imagename); // address
//TextView duration = (TextView)vi.findViewById(R.id); // distance
// ImageView thumb_image=(ImageView)vi.findViewById(R.id.mClogo); // logo
HashMap<String, String> jsn = new HashMap<String, String>();
jsn = data.get(position);
// Setting all values in listview
title.setText(jsn.get(Second.Li_nk));
artist.setText(jsn.get(Second.Image_name));
//duration.setText(song.get(CustomizedListView.KEY_DURATION));
//imageLoader.DisplayImage(jsn.get(NearBy.KEY_THUMB_URL), thumb_image);
return vi;
}
}
and the class implementing it is as follows
HashMap<String,String> map=new HashMap<String,String>();
map.put("Id",String.valueOf(i));
map.put(Li_nk,cutsec);
map.put(Image_name,j4.getString("image_name"));
mylist.add(map);
}
}
catch(JSONException e){
Log.e("loG_tag","Error parsing"+e.toString());
}
list=(ListView)findViewById(R.id.lv1);
list.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(),
"Click ListItem Number " + position, Toast.LENGTH_LONG)
.show();
}
});
LazyAdapter adapter = new LazyAdapter(this,mylist);
list.setAdapter(adapter);
list.setItemsCanFocus(false);
}
the layout containing the listview is
<?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:background="#FFFFFF"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/zsm" />
<ListView
android:id="#+id/lv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/imageView1"
android:clickable="True"
>
</ListView>
</RelativeLayout>
and the code for the custom layout for the list is
<?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" >
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="70dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:clickable="True"
android:focusable="false"
android:background="#drawable/list_selector"
>
<ImageView
android:id="#+id/imageView1"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="21dp"
android:focusable="false"
android:src="#drawable/merchntlogotitle" />
<TextView
android:id="#+id/imagename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView1"
android:layout_marginLeft="15dp"
android:layout_toRightOf="#+id/imageView1"
android:clickable="false"
android:focusable="false"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000" />
<TextView
android:id="#+id/linkname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/imagename"
android:layout_alignLeft="#+id/imagename"
android:clickable="false"
android:focusable="false"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000" />
</RelativeLayout>
</RelativeLayout>
Now setting the onitemclick listener gives no response ,what am I doing wrong ???
I have read plenty of xamples regarding this about setting focus false and others,what am I doing wrong??
Any help would be greatly apprreciated...
Remove the following from all the view items in your row's layout, because this stuff is distracting ListView's own item clicking events.
android:clickable
android:focusable
for example:
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="70dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#drawable/list_selector"
>
<ImageView
android:id="#+id/imageView1"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="21dp"
android:src="#drawable/merchntlogotitle" />
<TextView
android:id="#+id/imagename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView1"
android:layout_marginLeft="15dp"
android:layout_toRightOf="#+id/imageView1"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000" />
<TextView
android:id="#+id/linkname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/imagename"
android:layout_alignLeft="#+id/imagename"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000" />
</RelativeLayout>
Do not set android:clickable="True" in your layouts any where and try it. All click able views are bound with there parents so their parents will not able to get the click event.