I want to delete my listview items when I press delete button but the problem is I should first press listview row then press delete button to remove it. I want to delete items immediately after pressing delete button not first press on listview then press delete button
Can you help me in this situation?
and this is my code
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.wholeshop);
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// getting values from selected ListItem
final View view = v;
final int pos =position;
deleteone= (Button)v.findViewById(R.id.bdeleteone);
deleteone.getTag(position);
deleteone.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
deleteone.setTag(view);
productsList.remove(pos);
ListAdapter adapter = new SimpleAdapter(
wholeShop.this, productsList,
R.layout.listshop, new String[] { "name",
"spinn","price"},
new int[] { R.id.wholename, R.id.wholespinn,R.id.wholeprice });
// updating listview
setListAdapter(adapter);
}
});
}
});
}
and this is my xmlfile for listview:
<?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="vertical"
android:background="#ffffff">
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="110dp"
android:layout_marginTop="50dp"
android:descendantFocusability="blocksDescendants"
>
</ListView>
and:
<?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"
android:background="#ffffff"
android:weightSum="100">
<TextView
android:id="#+id/wholename"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight="40"
android:paddingLeft="5dip"
android:text="Flower Name"
android:textColor="#000000" />
<TextView
android:id="#+id/wholespinn"
android:layout_width="66dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="2dp"
android:paddingRight="10dip"
android:text="Flower spinn"
android:textColor="#000000"
android:layout_weight="30"/>
<TextView
android:id="#+id/wholeprice"
android:text="Flower price"
android:layout_gravity="right"
android:paddingRight="10dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:layout_weight="30"/>
<Button
android:id="#+id/bdeleteone"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:clickable="true"
android:text="Del"
android:layout_marginRight="10dp"
android:layout_weight="30"
android:focusable="false"
/>
</LinearLayout>
You are going with wrong way.
You may have to look this Example.
You have create Your Own Adapter like:
public class ListAdapter extends ArrayAdapter<String> {
customButtonListener customListner;
public interface customButtonListener {
public void onButtonClickListner(int position,String value);
}
public void setCustomButtonListner(customButtonListener listener) {
this.customListner = listener;
}
private Context context;
private ArrayList<String> data = new ArrayList<String>();
public ListAdapter(Context context, ArrayList<String> dataItem) {
super(context, R.layout.child_listview, dataItem);
this.data = dataItem;
this.context = context;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(R.layout.child_listview, null);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) convertView
.findViewById(R.id.childTextView);
viewHolder.button = (Button) convertView
.findViewById(R.id.childButton);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
final String temp = getItem(position);
viewHolder.text.setText(temp);
viewHolder.button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (customListner != null) {
customListner.onButtonClickListner(position,temp);
}
}
});
return convertView;
}
public class ViewHolder {
TextView text;
Button button;
}
}
Related
I am trying to implement a Card view inside grid view. The grid view is showing perfectly; even when I touch on a grid view item it's showing nothing. But when I click on the text which is display below its work. I referred This but not working...
Thanks.
My code is as below,
// Contact_Backup.java
public class Contact_Backup extends AppCompatActivity {
#Bind(R.id.gridview)
GridView gridview;
String[] contact_screen_array;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contact_backup);
ButterKnife.bind(this);
contact_screen_array = getResources().getStringArray(R.array.TextScreen);
String[] contact_icon_array = getResources().getStringArray(R.array.IconScreen);
String concat = "\nContacts";
gridview.setAdapter(new Custom_Adapter(getApplicationContext(),contact_icon_array,contact_screen_array,concat));
}
}
//Custom_Adapter
public class Custom_Adapter extends BaseAdapter {
Context contaxt;
String[] Array_text;
String[] Array_icon;
String concat;
LayoutInflater inflater;
public Custom_Adapter(Context context,String[] Array_icon, String[] Array_text, String concat)
{
this.contaxt = context;
this.Array_text = Array_text;
this.Array_icon = Array_icon;
this.concat=concat;
inflater = (LayoutInflater)context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return Array_text.length;
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Bind(R.id.Screen_icon)
Button Screen_icon;
#Bind(R.id.Screen_text)
TextView Screen_text;
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View grid;
LayoutInflater inflater = (LayoutInflater) contaxt
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = new View(contaxt);
grid = inflater.inflate(R.layout.new_app_card_layout, null);
ButterKnife.bind(this, grid);
Screen_text.setText(Array_text[position]+concat);
Screen_icon.setText(Array_icon[position]);
Typeface font = Typeface.createFromAsset(contaxt.getAssets(), "fonts/fontawesome-webfont.ttf" );
Screen_icon.setTypeface(font);
} else {
grid = (View) convertView;
}
grid.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("TAG::",""+position);
}
});
return grid;
}
}
// contact_backup.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="fill_parent"
android:background="#drawable/main_backgorund"
>
<RelativeLayout
android:background="#color/transparent"
android:padding="5.0dip"
android:id="#+id/top_bar_screen"
android:layout_weight="0.2"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:textColor="#color/newclr"
android:id="#+id/tvContacts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/contacts"
android:shadowColor="#000000"
android:shadowDx="1.0"
android:shadowDy="1.0"
android:shadowRadius="2.0"
android:layout_centerVertical="true" />
<TextView
android:textColor="#color/newclr"
android:id="#+id/tvLastBackup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/last_backup"
android:shadowColor="#000000"
android:shadowDx="1.0"
android:shadowDy="1.0"
android:shadowRadius="2.0"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
</RelativeLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:id="#+id/v1"
android:layout_below="#+id/top_bar_screen"
android:background="#color/newclr"/>
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/swipe"
android:layout_below="#+id/v1"
android:paddingTop="20sp"
android:paddingLeft="10sp"
android:paddingRight="10sp"
android:layout_weight="0.6"
>
<GridView
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:layout_height="wrap_content" />
</GridLayout>
<!-- com.startapp.android.publish.banner.Banner
android:id="#+id/startAppBanner1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_weight="0.2"
android:layout_centerHorizontal="true" /-->
</RelativeLayout>
// new_app_card_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/fssr_card"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
card_view:cardBackgroundColor="#color/transparent"
>
<LinearLayout
android:gravity="center"
android:orientation="vertical"
android:id="#+id/Contacts_Button_Layout"
android:background="#drawable/linearlayout_normal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="-1.0dip"
android:layout_weight="1.0"
>
<Button
android:textSize="35.0sp"
android:textColor="#color/newclr"
android:layout_gravity="center"
android:id="#+id/Screen_icon"
android:background="#null"
android:padding="20sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/fa_icon_contact"
android:drawablePadding="5.0dip"
android:textAllCaps="false" />
<TextView
android:textSize="15.0sp"
android:textColor="#color/white"
android:layout_gravity="center"
android:textAlignment="center"
android:id="#+id/Screen_text"
android:background="#color/newclr"
android:padding="5sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/contactsbackup"
android:textAllCaps="false"/>
</LinearLayout>
</android.support.v7.widget.CardView>
use this method for clicking on a gridview item:
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// your action when gridView item is clicked
}
});
Set the listener on the actual gridview object.
gridView.setOnItemClickListener(new OnItemClickListener()...);
Try this,
public class Contact_Backup extends AppCompatActivity {
#Bind(R.id.gridview)
GridView gridview;
String[] contact_screen_array;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contact_backup);
ButterKnife.bind(this);
contact_screen_array = getResources().getStringArray(R.array.TextScreen);
String[] contact_icon_array = getResources().getStringArray(R.array.IconScreen);
String concat = "\nContacts";
gridview.setAdapter(new Custom_Adapter(getApplicationContext(),contact_icon_array,contact_screen_array,concat));
gridview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for(int i=0;i<contact_screen_array.length;i++) {
Log.d("TAG::", "Position"+contact_screen_array[i]);
}
}
});
}
}
Finally, I succeed. here is the code.
Just use Textview instead of Button in // new_app_card_layout.xml.
Just Update it,
<TextView
android:textSize="35.0sp"
android:textColor="#color/newclr"
android:layout_gravity="center"
android:id="#+id/Screen_icon"
android:background="#null"
android:padding="20sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/fa_icon_contact"
android:drawablePadding="5.0dip"
android:textAllCaps="false"
android:textAlignment="center" />
Thank You so much, All of you for help... :)
Try this i have updated your custom Adapter class.please check again with this updated code.
public class Custom_Adapter extends BaseAdapter {
Context contaxt;
String[] Array_text;
String[] Array_icon;
String concat;
LayoutInflater inflater;
public Custom_Adapter(Context context,String[] Array_icon, String[] Array_text, String concat)
{
this.contaxt = context;
this.Array_text = Array_text;
this.Array_icon = Array_icon;
this.concat=concat;
inflater = (LayoutInflater)context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return Array_text.length;
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Bind(R.id.Screen_icon)
Button Screen_icon;
#Bind(R.id.Screen_text)
TextView Screen_text;
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View grid;
LayoutInflater inflater = (LayoutInflater) contaxt
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = new View(contaxt);
grid = inflater.inflate(R.layout.new_app_card_layout, null);
ButterKnife.bind(this, grid);
Screen_text.setText(Array_text[position]+concat);
Screen_icon.setText(Array_icon[position]);
Typeface font = Typeface.createFromAsset(contaxt.getAssets(), "fonts/fontawesome-webfont.ttf" );
Screen_icon.setTypeface(font);
grid.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("TAG::",""+position);
}
});
} else {
grid = (View) convertView;
grid.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("TAG::",""+position);
}
});
}
return grid;
}
}
Hope this helps you out !
I currently have ListFragment with a ArrayAdapter class
The codes are below.
1)I want to implement a checkbox from listview main xml to check all the checkboxes that are present in custom list row xml which is inflated using Array adapter.
2)how to refresh view in this code by clearing all checkboxes when a button is clicked.
listview_main.xml
<?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:orientation="vertical"
android:weightSum="10"
android:background="#fff2fff2" >
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="10"
android:choiceMode="multipleChoice" >
</ListView>
<TextView
android:id="#android:id/empty"
android:layout_width="wrap_content"
/>
<LinearLayout
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="#+id/btnin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="In" />
<Button
android:id="#+id/btndelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/checkAll"
android:checked="false" />
</LinearLayout>
</RelativeLayout>
custom_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/icon"
android:layout_width="50dp"
android:layout_height="50dp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/paackage"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent">
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Adapter class
public class FileAdapter extends ArrayAdapter<String>{
private List<String> filelist = null;
private Context context;
CheckBox checkAll;
public FileAdapter(Context _context, int _resource,List<String> _filelist) {
super(_context,_resource,_filelist);
this.context = _context;
this.filelist = _filelist;
this.itemChecked = new boolean[_filelist.size()];
}
private class ViewHolder {
TextView Name;
TextView pName;
CheckBox ck1;
ImageView iconview;
}
#Override
public int getCount() {
return ((null != filelist) ? filelist.size() : 0);
}
#Override
public String getItem(int position) {
return ((null != filelist) ? filelist.get(position) : null);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
View view = convertView;
if (null == view) {
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.custom_row, null);
holder = new ViewHolder();
holder.Name = (TextView) view.findViewById(R.id.name);
holder.pName = (TextView) view
.findViewById(R.id.paackage);
holder.ck1 = (CheckBox) view.findViewById(R.id.checkBox1);
holder.iconview = (ImageView) view.findViewById(R.id.app_icon);
view.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.appName.setText();
holder.packageName.setText();
holder.iconview.setImageDrawable();
holder.ck1.setChecked(false);
if (itemChecked[position])
holder.ck1.setChecked(true);
else
holder.ck1.setChecked(false);
holder.ck1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (holder.ck1.isChecked()) {
itemChecked[position] = true;
} else {
itemChecked[position] = false;
}
}
});
return view;
}
}
ListFragment class
public class FragmentA extends ListFragment implements View.OnClickListener {
public ArrayList<String> filelist;
Button in, delete;
ListView lv;
CheckBox cb;
private FileAdapter listadaptor = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
filelist = new ArrayList<String>();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View inflatedView = inflater.inflate(R.layout.fragment_app_restore, container, false);
in = (Button) inflatedView.findViewById(R.id.btnin);
delete= (Button) inflatedView.findViewById(R.id.btndelete);
lv = (ListView) inflatedView.findViewById(android.R.id.list);
empty = inflatedView.findViewById(android.R.id.empty);
//calling asynctask to load files here
delete.setOnClickListener(this);
in.setOnClickListener(this);
return inflatedView;
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
cb = (CheckBox) v.findViewById(R.id.checkBox1);//this is the checkbox in row only
cb.performClick();
if (cb.isChecked()) {
} else {
}
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btndelete:
break;
case R.id.btnin:
break;
}
}
private class LoadFiles extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... params) {
filelist = GetFiles(root_path);
if (filelist != null) {
listadaptor = new AppRestoreFileAdapter(getActivity(),
R.layout.custom_row, filelist);
} else {
// lv.setEmptyView(empty);
}
return null;
}
#Override
protected void onPostExecute(Void result) {
lv.setAdapter(listadaptor);
progress.dismiss();
super.onPostExecute(result);
}
}
}
I've read some SO questions like this and this but still couldn't figure out whats wrong with my code.
I have a ListFragment, and each row has a TextView and a CheckBox.
Clicking the CheckBox is working, but clicking on the TextView does nothing, and OnListItemClick doesn't get called. I've also tried to dynamically add an OnClickListener, which make it work, but it's not the correct way to do it, and it also lacks the GUI feedback of the click (item being "highlighted" for a sec).
This is my textview_with_checkbox.XML I'm using for each item:
<?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="wrap_content"
android:clickable="true"
android:focusable="true"
android:gravity="left"
android:descendantFocusability="blocksDescendants"
android:orientation="horizontal" >
<TextView
android:id="#+id/textview_event_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="3dp"
android:paddingTop="5dp"
android:focusable="true"
android:singleLine="false" />
<CheckBox
android:id="#+id/checkbox_for_event_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.05"
android:focusable="false"
android:clickable="false" />
</LinearLayout>
now the code:
#Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
displayEventsLogFiles();
}
#Override
public void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
String chosenItem = (String) getListAdapter().getItem(position);
mCallBack.onEventItemSelected(chosenItem);
}
static class myCustomAdapterViewHolder
{
public TextView eventName;
public CheckBox eventCheckBox;
}
private class myCustomAdapter extends ArrayAdapter<String>
{
private ArrayList<String> sortedList;
private Context m_oContext;
List<String> m_oValues = null;
public myCustomAdapter(Context cont, int viewResId, List<String> objects)
{
super(cont, viewResId, objects);
m_oContext = cont;
m_oValues = objects;
sortedList = new ArrayList<String>();
for (String str : objects)
sortedList.add(str);
java.util.Collections.sort(sortedList);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View l_oRowView = convertView;
myCustomAdapterViewHolder l_oInitializedViewHolder = null;
final int l_nPosition = position;
// Use convertView if possible, otherwise inflate a view:
if (l_oRowView == null)
{
LayoutInflater inflater = (LayoutInflater) m_oContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
l_oRowView = inflater.inflate(R.layout.textview_with_checkbox, parent, false);
// PlaceHolder pattern:
final myCustomAdapterViewHolder l_oViewHolder = new myCustomAdapterViewHolder();
l_oViewHolder.eventName = (TextView) l_oRowView.findViewById(R.id.textview_event_name);
l_oViewHolder.eventCheckBox = (CheckBox) l_oRowView.findViewById(R.id.checkbox_for_event_name);
l_oViewHolder.eventCheckBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CheckBox cb = (CheckBox)v;
//cb.setChecked(!cb.isChecked());
String l_sFilename = l_oViewHolder.eventName.getText().toString();
if (cb.isChecked())
{
if (!m_lstSelectedFilenames.contains(l_sFilename))
m_lstSelectedFilenames.add(l_sFilename);
}
else
{
if (m_lstSelectedFilenames.contains(l_sFilename))
m_lstSelectedFilenames.remove(l_sFilename);
}
}
});
l_oViewHolder.eventCheckBox.setFocusable(false);
//l_oViewHolder.eventCheckBox.setClickable(false);
// "Add" the viewHolder as a tag:
l_oRowView.setTag(l_oViewHolder);
l_oInitializedViewHolder = l_oViewHolder;
}
else
l_oInitializedViewHolder = (myCustomAdapterViewHolder) l_oRowView.getTag();
// By now, the rowView is initialized, just get the viewHolder and then get the views from it, to update:
//myCustomAdapterViewHolder l_oViewHolder = (myCustomAdapterViewHolder) l_oRowView.getTag();
/*l_oInitializedViewHolder.eventName.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCallBack.onEventItemSelected((String)getListAdapter().getItem(l_nPosition));
}
});*/
l_oInitializedViewHolder.eventName.setText(m_oValues.get(position));
return l_oRowView;
//return super.getView();
}
public myCustomAdapter(Context cont, int viewResId, String[] strings)
{
this(cont, viewResId, Arrays.asList(strings));
}
#Override
public String getItem(int position)
{
return sortedList.get(position);
}
#Override
public int getPosition(String item)
{
return sortedList.indexOf(item);
}
}
What am I doing wrong here?
All I want is to be able to select "files" for deletion, using the CheckBoxes
Try only using only onclick for textview and checkbox, not onListItemClick -which you can remove- as well, so you should change some properties for the linear layout as well.
<?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="wrap_content"
android:clickable="false"
android:focusable="false"
android:gravity="left"
android:orientation="horizontal" >
<TextView
android:id="#+id/textview_event_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="3dp"
android:paddingTop="5dp"
android:focusable="true"
android:singleLine="false" />
<CheckBox
android:id="#+id/checkbox_for_event_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.05"
android:focusable="false"
android:clickable="false" />
</LinearLayout>
Implement in the adapter
// PlaceHolder pattern:
final myCustomAdapterViewHolder l_oViewHolder = new myCustomAdapterViewHolder();
l_oViewHolder.eventName = (TextView) l_oRowView.findViewById(R.id.textview_event_name);
l_oViewHolder.eventCheckBox = (CheckBox) l_oRowView.findViewById(R.id.checkbox_for_event_name);
l_oViewHolder.eventName.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// your code for textview click
}
}
l_oViewHolder.eventCheckBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CheckBox cb = (CheckBox)v;
//cb.setChecked(!cb.isChecked());
String l_sFilename = l_oViewHolder.eventName.getText().toString();
if (cb.isChecked())
{
if (!m_lstSelectedFilenames.contains(l_sFilename))
m_lstSelectedFilenames.add(l_sFilename);
}
else
{
if (m_lstSelectedFilenames.contains(l_sFilename))
m_lstSelectedFilenames.remove(l_sFilename);
}
}
});
l_oViewHolder.eventCheckBox.setFocusable(false);
add following attribute:
android:focusable="false"
android:clickable="false"
for example in my xml file:
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeightSmall"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:button="?android:attr/listChoiceIndicatorSingle"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:buttonTint="#color/gray"
android:focusable="false"
android:clickable="false"
/>
and in my code:
public class MyTestFragment extends ListFragment {
.....
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
}
}
This question already has answers here:
ListView and Buttons inside ListView
(5 answers)
Closed 8 years ago.
Currently, my listView is sitting inside a fragment. However, the buttons inside my view are not able to receive the on click event whenever I tap on them.
I am not sure what to do.
Here is my code:
public class ViewInvitationsCursorAdapter extends CursorAdapter {
private Activity activity;
private int layout;
private Cursor cr;
private final LayoutInflater inflater;
private Fragment f;
private boolean displayAcceptDeny;
public ViewInvitationsCursorAdapter(Activity activity, Fragment f,
int layout, Cursor c, boolean displayAcceptDeny) {
super(activity, c);
this.layout = layout;
this.activity = activity;
this.inflater = LayoutInflater.from(activity);
this.cr = c;
this.f = f;
this.displayAcceptDeny = displayAcceptDeny;
}
public static class ViewHolder {
public TextView sender;
public TextView sender_email;
public TextView author_role;
public Button deny;
public Button accept;
public Button cancel;
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
ViewHolder holder = new ViewHolder();
View view = inflater.inflate(layout, parent, false);
holder.sender = (TextView) view.findViewById(R.id.connection_sender);
holder.sender_email = (TextView) view
.findViewById(R.id.connection_sender_email);
holder.author_role = (TextView) view.findViewById(R.id.connection_role);
holder.accept = (Button) view.findViewById(R.id.connection_allow);
holder.deny = (Button) view.findViewById(R.id.connection_deny);
holder.cancel = (Button) view.findViewById(R.id.connection_cancel);
view.setTag(holder);
return view;
}
#Override
public void bindView(View view, Context context, final Cursor cursor) {
// .bindView(view, context, cursor);
final ViewHolder holder = (ViewHolder) view.getTag();
int sender_index = cursor
.getColumnIndexOrThrow(FamilyDBHelper.COLUMN_SENDER_BY_NAME);
int sender_email_index = cursor
.getColumnIndexOrThrow(FamilyDBHelper.COLUMN_SENDER_BY_EMAIL);
String senderString = cursor.getString(sender_email_index);
int author_role_name = cursor
.getColumnIndexOrThrow(FamilyDBHelper.COLUMN_RELATIONTYPE_NAME);
int message_index = cursor
.getColumnIndexOrThrow(FamilyDBHelper.COLUMN_MESSAGE);
holder.sender.setText(cursor.getString(sender_index));
holder.author_role.setText(cursor.getString(author_role_name));
holder.sender_email.setText(cursor.getString(sender_email_index));
holder.cancel.setVisibility(View.VISIBLE);
// setUpCancel(view, cursor);
holder.cancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Log.i("cancel", "cancel clicked!");
}
});
}
protected void setUpAcceptDeny(View view, final Cursor cursor) {
final ViewHolder holder = (ViewHolder) view.getTag();
holder.accept.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Log.i("accept", "accept clicked!");
}
});
holder.deny.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Log.i("deny", "deny clicked!");
}
});
}
}
Here is the xml for each of the items:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/connection_item_row_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="8dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="8dp" >
<LinearLayout
android:id="#+id/connection_item_info_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="#+id/connection_sender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="16sp"
android:textStyle="bold"
tools:text="ANDROID: PREFERENCEFRAGMENTCOMPAT" >
</TextView>
<TextView
android:id="#+id/connection_sender_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/connection_sender"
android:textColor="#000000"
android:textSize="14sp"
tools:text="ANDROID: PREFERENCEFRAGMENTCOMPAT" >
</TextView>
<TextView
android:id="#+id/connection_role"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/connection_sender_email"
android:textColor="#000000"
android:textSize="14sp"
>
</TextView>
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/connection_item_info_layout"
android:orientation="horizontal"
android:paddingBottom="1.0dip"
android:paddingLeft="4.0dip"
android:paddingRight="4.0dip"
android:paddingTop="5.0dip" >
<Button
android:id="#+id/connection_allow"
android:layout_width="0.0dip"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:text="Allow"
android:background="#color/green"
android:textColor="#color/white"
android:visibility="gone" />
<Button
android:id="#+id/connection_deny"
android:layout_width="0.0dip"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:background="#color/red"
android:textColor="#color/white"
android:text="Deny"
android:visibility="gone" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/connection_item_info_layout_request"
android:orientation="horizontal"
android:paddingBottom="1.0dip"
android:paddingLeft="4.0dip"
android:paddingRight="4.0dip"
android:paddingTop="5.0dip" >
<Button
android:id="#+id/connection_cancel"
android:layout_width="0.0dip"
android:background="#color/red"
android:textColor="#color/white"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:text="Cancel Request"
android:visibility="gone" />
</LinearLayout>
</LinearLayout>
Add this line to your list item xml file.
android:descendantFocusability="blocksDescendants"
if you set clicklistener inside newView method like this it should work. I generally user getView() for this method and there has never been a problem..
#Override
public View getView(Context context, View View, ViewGroup parent) {
ViewHolder holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View = inflater.inflate(R.layout.your_layout_file_id, parent,
false);
holder.sender = (TextView) view.findViewById(R.id.connection_sender);
holder.sender_email = (TextView) view
.findViewById(R.id.connection_sender_email);
holder.author_role = (TextView) view.findViewById(R.id.connection_role);
holder.accept = (Button) view.findViewById(R.id.connection_allow);
holder.deny = (Button) view.findViewById(R.id.connection_deny);
holder.cancel = (Button) view.findViewById(R.id.connection_cancel);
view.setTag(holder);
holder.cancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Log.i("cancel", "cancel clicked!");
}
});
return view;
}
I have to apply pagination concept on ListView my list view contains data parsed from web service. below is code given that how I have displayed data in list view as below.
try {
ArrayList<HashMap<String, String>> arl (ArrayList<HashMap<String, String>>) getIntent().getSerializableExtra("arraylist");
System.out.println("...serialized data.."+arl);
lv1 = (ListView) findViewById(R.id.lstlodgingresult);
adapter = new SimpleAdapter(this, arl, R.layout.custom_row_view,
new String[] { "Srno", "Names", "URL", "Address1", "Address2", "Telephone", "Category", "PetH",
"PetInfo" }, new int[] { R.id.txtSrno,R.id.txtname, R.id.txturl, R.id.txtaddress1, R.id.txtaddress2, R.id.txtphone, R.id.txtcategory,
R.id.txtpetpolicyH, R.id.txtpetpolicyC }
);
lv1.setScrollbarFadingEnabled(false);
lv1.refreshDrawableState();
lv1.setAdapter(adapter);
} catch (Exception e) {
e.printStackTrace();
}
you just need to add a Footer View in the Listyou created. Then for the footer view (might be button/image/text) set a ClickListener for that and in Listener add the items into your list and again refresh the activity. I am adding a little tutorial that will help you in this.
I used the following Method for Pagination:
The List Class:
public class customListView extends Activity implements OnClickListener{
private static class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;
Context context;
public EfficientAdapter(Context context) {
this.context = context;
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return add_Names.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.listcontent, null);
holder = new ViewHolder();
holder.text = (TextView) convertView
.findViewById(R.id.txt1);
holder.text2 = (TextView) convertView
.findViewById(R.id.txt2);
holder.text3 = (TextView) convertView
.findViewById(R.id.txt3);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(add_Names.get(position).toString());
holder.text2.setText(location.get(position).toString());
holder.text3.setText(details.get(position).toString());
return convertView;
}
static class ViewHolder {
TextView text;
TextView text2;
TextView text3;
}
}//end of efficient Adapter Class
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
adapter = new EfficientAdapter(this);
l1 = (ListView) findViewById(R.id.ListView01);
View footerView =
((LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.layout_footer, null, false);
l1.addFooterView(footerView);
l1.setAdapter(adapter);
mLayout = (LinearLayout) footerView.findViewById(R.id.footer_layout);
more = (Button) footerView.findViewById(R.id.moreButton);
more.setOnClickListener(this);
l1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast.makeText(getBaseContext(), "You clciked "+add_Names.get(arg2).toString(), Toast.LENGTH_LONG).show();
}
});
}
#Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.moreButton:
//Your code to add some more data into list and then call the following to refresh your lits
adapter.notifyDataSetChanged();
break;
}//end of switch
}//end of onClick
}//end of Custom List view class
layout_footerview.xml:(you can add whatever you link in the footer for the list. I used button you can use Text or image or whatever you want)
<?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="wrap_content"
android:paddingTop="7dip"
android:paddingBottom="7dip"
android:orientation="horizontal"
android:gravity="center">
<LinearLayout
android:id="#+id/footer_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_gravity="center">
<Button
android:text="Get more.."
android:id="#+id/moreButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="14dip"
android:textStyle="bold">
</Button>
</LinearLayout>
</LinearLayout>
listview.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="#+id/ListView01" android:layout_height="wrap_content"
android:layout_width="fill_parent">
</ListView>
</RelativeLayout>
list-content.xml:(modify as u like to be your list row)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView android:id="#+id/image1" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:src="#drawable/icon"></ImageView>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/txt1" android:layout_toRightOf="#+id/image1"
android:text="Test Description" android:textSize="15dip" android:textStyle="bold">
</TextView>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/txt2" android:layout_below="#+id/txt1" android:layout_toRightOf="#+id/image1"
android:text="Address" android:textSize="10dip"></TextView>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/txt3" android:layout_below="#+id/txt2" android:layout_toRightOf="#+id/image1"
android:text="Details" android:textSize="10dip" ></TextView>
</RelativeLayout>
I Hop this will definetly help you.!
Mark this as true and UpVote; if this helps you.
Thanks
sHaH..