ListView ,OnItemClickListener gives no response - android

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.

Related

Android : Unable to delete ListView item from menu

I am new here so please bear with my naivety. I am working on a music player that will display a menu on clicking 'dots' buttton(given in code below) in listview item. Inside the menu there will be a button to delete. The listview is getting populated by a custom adapter. Here is the code of adapter...
private class hashmapAdapter extends BaseAdapter implements Filterable {
HashMap<Integer, NowPlayingActivity.Details> NewDetails = new HashMap<>();
private hashmapAdapter(HashMap<Integer, NowPlayingActivity.Details> hashMap) {
if(NewDetails.size()==0) {
NewDetails.putAll(hashMap);
}
}
#Override
public int getCount() {
return NewDetails.size();
}
#Override
public Object getItem(int position) {
return NewDetails.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final View result;
if(convertView == null) {
result = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_item,parent,false);
} else {
result = convertView;
}
((TextView)result.findViewById(R.id.Name)).setText(NewDetails.get(position).getName());
((TextView)result.findViewById(R.id.artist)).setText(NewDetails.get(position).getArtist());
((TextView)result.findViewById(R.id.duration)).setText(NewDetails.get(position).getDuration()+" || ");
dots = (Button)result.findViewById(R.id.dots);
dots.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
name = (TextView)result.findViewById(R.id.Name);
LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View popup_view = inflater.inflate(R.layout.option_layout,null);
RelativeLayout item = (RelativeLayout) popup_view.findViewById(R.id.popup);
final float width= (getResources().getDimension(R.dimen.width_entry_in_dp));
final float height= getResources().getDimension(R.dimen.height_entry_in_dp);
final PopupWindow popupWindow = new PopupWindow(popup_view,Math.round(width),Math.round(height),true);
popupWindow.showAtLocation(item, Gravity.END, 0, 0);
popupWindow.showAsDropDown(dots);
play_next_btn = (Button)popup_view.findViewById(R.id.play_next);
add_to_playlist = (Button)popup_view.findViewById(R.id.add_to_playlist);
share = (Button)popup_view.findViewById(R.id.share);
delete = (Button)popup_view.findViewById(R.id.delete);
delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for(int i=0;i<NewDetails.size();i++) {
if(NewDetails.get(i).getName().equals(name.getText().toString())) {
for(int j=0;j<hashMap.size();j++) {
if(hashMap.get(j).getName().equals(name.getText().toString())) {
if (true) {
NewDetails.remove(i);
hashMap.remove(j);
}
}
}
}
}
populate1(NewDetails);
popupWindow.dismiss();
}
});
}
});
return result;}
Now the problem is that after deleting element when i call populate1(NewDetails), it gives a null pointer exception on NewDetails.get(position).getName(). So on repopulating listview i am getting NPE error. Following is the populate1() function code...
public void populate1(HashMap<Integer,NowPlayingActivity.Details> hashMap) {
adapter = new hashmapAdapter(hashMap);
library.setAdapter(adapter);
adapter.notifyDataSetChanged();
library.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
name = (TextView)view.findViewById(R.id.Name);
String SongName = name.getText().toString();
Intent intent = new Intent(getApplicationContext(),NowPlayingActivity.class);
intent.putExtra("SongName",SongName);
setResult(555,intent);
finish();
}
});
}
I have already tried notifydatasetchanged() and listview.invalidateviews(), both didnt work. File is getting deleted but the adapter is not refreshing the list to remove element. Here is the xml layout of single_item for listview....`
`<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="wrap_content"
android:layout_height="70dp"
android:paddingRight="8dp"
android:paddingEnd="8dp"
android:background="#drawable/element_shape_list"
android:descendantFocusability="blocksDescendants">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/v1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:foreground="?android:attr/selectableItemBackground">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/v11"
android:orientation="horizontal" android:layout_width="wrap_content"
android:layout_height="match_parent">
<ImageView
android:id="#+id/art_work"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#mipmap/ic_launcher"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"/>
<TextView
android:id="#+id/Name"
android:textSize="17sp"
android:textColor="#000"
android:textStyle="bold"
android:gravity="bottom"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:layout_marginRight="30dp"
android:layout_marginEnd="30dp"
android:layout_toRightOf="#id/art_work"
android:layout_toEndOf="#+id/art_work"
android:singleLine="true"
/>
<RelativeLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:layout_marginRight="30dp"
android:layout_marginEnd="30dp"
android:layout_marginBottom="10dp"
android:layout_toRightOf="#+id/art_work"
android:layout_toEndOf="#id/art_work"
android:layout_below="#id/Name">
<TextView
android:id="#+id/duration"
android:textSize="12sp"
android:textColor="#000"
android:layout_width="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_height="20dp"
android:singleLine="true"
android:gravity="clip_horizontal"
android:ellipsize="end"/>
<TextView
android:id="#+id/artist"
android:textSize="12sp"
android:textColor="#000"
android:layout_toRightOf="#+id/duration"
android:layout_toEndOf="#+id/duration"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:gravity="fill_horizontal"
android:layout_height="20dp"
android:singleLine="true" />
</RelativeLayout>
</RelativeLayout>
</FrameLayout>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/holder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:background="#android:color/transparent">
<Button
android:id="#+id/dots"
android:layout_width="40dp"
android:textStyle="bold"
android:background="?android:attr/selectableItemBackground"
android:layout_height="70dp"
android:text="#string/vertical_ellipsis"
android:textSize="25sp" />
</FrameLayout>
</RelativeLayout>

SetOnItemClickListener not responding in custom ListView of android

I am new to android,I am currently making a listview with custom list items,I want to do an action on Listview's item click event,I have searched so many similar threads with no luck,Can anyone help me to fix this.my code is as below,
<?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="match_parent"
android:layout_height="match_parent"
android:background="#fff">
<TextView
android:id="#+id/tv_hdr"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:layout_alignParentTop="true"
android:background="#color/orange"
android:gravity="center"
android:text="SELECT YOUR CITY"
android:textColor="#color/white"
android:textSize="22dp" />
<EditText
android:id="#+id/et_search"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_hdr"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:background="#drawable/et_selector"
android:drawableLeft="#drawable/search"
android:hint="City Or Postal Code"
android:padding="10dp"
android:textSize="16dp" />
<TextView
android:id="#+id/tv_loc"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/et_search"
android:background="#color/grey_light"
android:drawableLeft="#drawable/ic_loc"
android:gravity="center_vertical"
android:padding="10dp"
android:paddingLeft="5dp"
android:text=" My Location"
android:textColor="#color/black"
android:textSize="16dp"
android:textStyle="bold" />
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scr_location"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/tv_loc"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tv_populcar_city"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:gravity="center_vertical"
android:padding="10dp"
android:paddingLeft="5dp"
android:text=" Popular Cities"
android:textColor="#color/orange"
android:textSize="16dp"
android:textStyle="bold" />
<ListView
android:id="#+id/lv_city"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_populcar_city" />
<TextView
android:id="#+id/tv_states"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/lv_city"
android:background="#color/white"
android:gravity="center_vertical"
android:padding="10dp"
android:paddingLeft="5dp"
android:text="States"
android:textColor="#color/orange"
android:textSize="16dp"
android:textStyle="bold" />
<ListView
android:id="#+id/lv_state"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_states" />
</RelativeLayout>
</ScrollView>
</RelativeLayout>
adapter
public class CityAdapter extends BaseAdapter {
private Context mContext;
private final ArrayList<City> city;
Integer selected_position = -1;
public CityAdapter(Context c, ArrayList<City> city) {
mContext = c;
this.city = city;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return city.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v;
if (convertView == null) { // if it's not recycled, initialize some attributes
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.raw_city, parent, false);
} else {
v = (View) convertView;
}
TextView tv_city = (TextView) v.findViewById(R.id.tv_city);
tv_city.setText(city.get(position).getCity());
return v;
}
}
activity
lv_city.setOnItemClickListener( new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Pref.setValue( SelectCity.this, Const.PREF_CITY_ID, cityList.get( position ).getCity_id() );
finish();//finishing activity
}
} );
You are wrong class in listview item click
lv_city.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Pref.setValue( SelectCity.this, Const.PREF_CITY_ID,
cityList.get( position ).getCity_id() );
finish();//finishing activity
}
});
#quick learner's was correct, But i put some idea for your ref..
If you use custom adapter class, In my suggestion use setOnClickListener in custom adapter class this is better way to do this type of case!!
For example,
mViewHolder.MyUI.setOnClickListener

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

How to add/remove item from listview in android when click button in item listview

I'm a newbie android.I have some problem about my mini app.
You can see figure below:
http://i481.photobucket.com/albums/rr175/viethungit/android.png
Firstly,row 1(layout 1) appear listview when click button add in row 1, row 2(layout 2) appear, and continue...click button add in row 1 ,layout 2 appear in listview....
I try search from Mr.Google but i don't find...
Anybody could help me!
This is layout, and activity I don't know how to implement
add_item.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:background="#EEEEEE"
android:orientation="horizontal"
android:padding="5dip">
<!-- Image Item-->
<ImageButton
android:id="#+id/imgItem"
android:layout_width="40dip"
android:layout_height="40dip"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="2dp"
android:contentDescription="#string/imgView" />
<!-- Name item -->
<EditText
android:id="#+id/edtItem"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#+id/imgItem"
android:background="#drawable/bg"
android:hint="#string/txtTitle"
android:textSize="20dip" />
<!-- Button add -->
<Button
android:id="#+id/btnAdd"
android:background="#drawable/add"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"/>
<!-- item -->
<TextView
android:id="#+id/txtSubtitle"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_toLeftOf="#+id/btnAdd"
android:gravity="right|center_vertical"
android:layout_centerVertical="true"
android:textSize="20dip"
android:text="#string/txtSubtitle" />
</RelativeLayout>
plus_item.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:background="#EEEEEE"
android:orientation="horizontal"
android:padding="5dip" >
<!-- Image Item -->
<ImageView
android:id="#+id/imgItem"
android:src="#drawable/chomsao"
android:layout_width="40dip"
android:layout_height="40dip"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="2dp"
android:contentDescription="#string/imgView" />
<!-- Name Item -->
<TextView
android:id="#+id/edtItem"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#+id/imgItem"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:text="#string/txtTitle"
android:textSize="20dip" />
<!-- Quantity Item -->
<TextView
android:id="#+id/txtQtyItem"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_toRightOf="#id/edtItem"
android:layout_marginLeft="20dp"
android:gravity="center_vertical"
android:textSize="20dip"
android:text="#string/QuantityItem"/>
<!-- Button plus -->
<Button
android:id="#+id/btnPlus"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="#drawable/plus" />
<!-- Button minus -->
<Button
android:id="#+id/btnMinus"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_alignBaseline="#id/btnPlus"
android:layout_toLeftOf="#+id/btnPlus"
android:background="#drawable/minus" />
<!-- Price Item -->
<TextView
android:id="#+id/txtSubtitle"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/btnMinus"
android:gravity="right|center_vertical"
android:text="#string/txtSubtitle"
android:textSize="20dip" />
</RelativeLayout>
activity_main.xml
<RelativeLayout
android:id="#+id/relative1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/relative"
android:layout_above="#id/relative2"
android:background="#EEEEEE"
android:layout_marginLeft="1.5dp"
android:layout_marginRight="1.5dp"
android:layout_marginTop="1.5dp">
<ListView
android:id="#+id/listSale"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</RelativeLayout>
public class AndroidCustomListViewActivity extends Activity {
private ListView myList;
private MyAdapter myAdapter;
private ImageView myImage;
public static String upload= " ";
public static String GalleryImage;
public ArrayList<ListItem> myItems = new ArrayList<ListItem>();
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listandimage);
myList = (ListView) findViewById(R.id.MyList);
myImage= (ImageView)findViewById(R.id.image1);
myList.setItemsCanFocus(true);
myAdapter = new MyAdapter();
ListItem listItem = new ListItem();
listItem.textdata="#";
listItem.caption = "";
myItems.add(listItem);
myList.setAdapter(myAdapter);
myAdapter.notifyDataSetChanged();
}
public class MyAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public MyAdapter() {
mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return myItems.size();
}
public ListItem getItem(int position) {
return myItems.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.item, null);
holder.text=(TextView )convertView.findViewById(R.id.textView1);
holder.captionEditText = (EditText) convertView.findViewById(R.id.ItemCaption);
holder.addOrDeleteButton = (Button) convertView.findViewById(R.id.buttonAdd);
holder.captionEditText.setFocusable(true);
holder.captionEditText.requestFocus();
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// Fill EditText with the value you have in data source
// holder.captionEditText.setId(position);
holder.text.setTag(position);
holder.captionEditText.setTag(position);
holder.captionEditText.setText(getItem(position).caption);
holder.addOrDeleteButton.setTag(position);
// / this updates tag of
// the button view as we
// scroll ///
holder.addOrDeleteButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// TODO Auto-generated method stub
int tag = (Integer) view.getTag();
if (tag != (myItems.size() - 1)) {
myItems.remove(tag);
Log.d("GCM", "Item removed from " + tag);
myAdapter.notifyDataSetChanged();
} else {
ListItem listItem = new ListItem();
listItem.textdata="#";
listItem.caption = "";
myItems.add(listItem);
/*
* Log.d("GCM", holder.captionEditText.getText()
* .toString()); myItems.get((Integer)
* view.getTag()).caption = holder.captionEditText
* .getText().toString();
*/
myAdapter.notifyDataSetChanged();
myList.setSelection(myAdapter.getCount() - 1);
// holder.captionEditText.setFocusable(true);
// holder.captionEditText.requestFocus();
}
}
});
holder.captionEditText.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start,
int before, int count) {
// TODO Auto-generated method stub
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
// TODO Auto-generated method stub
}
public void afterTextChanged(Editable s) {
// if(position < myItems.size())
// getItem(position).caption = s.toString();
myItems.get((Integer) holder.captionEditText.getTag()).caption = holder.captionEditText
.getText().toString();
}
});
if (position != (myItems.size() - 1)) {
holder.addOrDeleteButton.setBackgroundResource(R.drawable.fruttarecloseicon);
} else {
holder.addOrDeleteButton.setBackgroundResource(R.drawable.fruttareaddicon);
holder.text.setFocusable(true);
holder.captionEditText.setFocusable(true);
holder.text.requestFocus();
holder.captionEditText.requestFocus();
}
return convertView;
}
}
class ViewHolder {
TextView text;
EditText captionEditText;
Button addOrDeleteButton;
}
class ListItem {
String textdata;
String caption;
}
}
use this code edit it according to your need.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
>
<TextView
android:id="#+id/textView1"
android:layout_width="15dp"
android:layout_height="50dp"
android:text=" #"
/>
<EditText
android:id="#+id/ItemCaption"
android:layout_width="270dp"
android:layout_height="50dp"
android:layout_margin="3dip"
android:imeOptions="actionDone|flagNoExtractUi"
android:inputType="textNoSuggestions"
android:singleLine="true" >
</EditText>
<Button
android:id="#+id/buttonAdd"
android:layout_width="75dp"
android:layout_height="50dp"
android:layout_margin="3dip"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fff"
android:orientation="horizontal" >
<ListView
android:id="#+id/MyList"
android:layout_width="370dp"
android:layout_height="160dp"
android:layout_gravity="center|top"
android:layout_marginLeft="150dp"
android:descendantFocusability="beforeDescendants"
>
</ListView>
<ImageView
android:id="#+id/image1"
android:layout_width="80dp"
android:layout_height="80dp"
/>
</LinearLayout>
mark the answer right if it was useful!!
You can create Elements (like Buttons, Views, and so on) and add them to an existing View like:
myListView.addChild(CreatedButton);

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