Custom List Adapter not Binding values to TextView - android

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..

Related

java.lang.NullPointerException with ListView and BaseAdapter

I tried to use a ListView with a BaseAdapter. I had a java.lang.NullPointerException on this line:
titolo.setText(getResources().getString(R.string.nome_cognome_utente, p.nome));
Where I'm doing wrong?
This is my main activity:
public class ListaUtenti extends Activity {
private ListView listView;
private ListAdapter adapter;
private List<Persona> persona = new LinkedList<Persona>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lista_utenti);
listView = (ListView)findViewById(R.id.listaUtenti);
adapter = new BaseAdapter() {
#Override
public int getCount() {
return persona.size();
}
#Override
public Persona getItem(int position) {
return persona.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View view, ViewGroup viewGroup) {
if(view == null){
view = getLayoutInflater().inflate(R.layout.list_item_persona, null);
}
final TextView titolo = (TextView)findViewById(R.id.firstLine);
final TextView descrizione = (TextView)findViewById(R.id.secondLine);
final Persona p = getItem(position);
titolo.setText(getResources().getString(R.string.nome_cognome_utente, p.nome));
descrizione.setText(getResources().getString(R.string.descrizione, p.cognome));
return view;
}
};
listView.setAdapter(adapter);
}
#Override
protected void onStart(){
super.onStart();
persona.clear();
persona.add(new Persona("nome1","cognome1"));
persona.add(new Persona("nome2","cognome2"));
persona.add(new Persona("nome3","cognome3"));
listView.setAdapter(adapter);
}
}
This is the ListView in the xml file activity_lista_utenti:
<ListView
android:id="#+id/listaUtenti"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
tools:listitem="#android:layout/simple_list_item_1" />
This is the xml file list_item_persona:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white">
<ImageView
android:id="#+id/icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_margin="5dp"
android:src="#drawable/ic_contact_picture"
android:contentDescription="#string/immagine_profilo" />
<TextView
android:id="#+id/firstLine"
android:layout_width="fill_parent"
android:background="#color/yellow2"
android:layout_height="30dp"
android:layout_alignParentTop="true"
android:layout_marginTop="5dp"
android:layout_toRightOf="#id/icon"
android:layout_toEndOf="#id/icon"
android:ellipsize="marquee"
android:singleLine="true"
android:gravity="center_vertical"
android:textSize="20sp" />
<TextView
android:id="#+id/secondLine"
android:layout_width="fill_parent"
android:background="#color/yellow"
android:layout_height="20dp"
android:layout_alignParentTop="true"
android:layout_marginTop="35dp"
android:layout_toRightOf="#id/icon"
android:layout_toEndOf="#id/icon"
android:ellipsize="marquee"
android:singleLine="true"
android:gravity="center_vertical"
android:textSize="15sp" />
</RelativeLayout>
Get textview from view's findViewById,i.e.
Change
final TextView titolo = (TextView)findViewById(R.id.firstLine);
final TextView descrizione = (TextView)findViewById(R.id.secondLine);
to
final TextView titolo = (TextView)view.findViewById(R.id.firstLine);
final TextView descrizione = (TextView)view.findViewById(R.id.secondLine);
Since they are part of list_item_persona view.

how to get checked item from listview

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.

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

ListView ,OnItemClickListener gives no response

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.

Categories

Resources