list_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="5dp">
<TextView
android:id="@+id/mytitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="5dip"
android:layout_marginTop="5dp"
android:layout_toRightOf="@+id/icon"
android:background="@drawable/textbox1"
android:paddingBottom="10dp"
android:text="Title"
android:textColor="#CC0033"
android:textSize="20dp" />
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:layout_alignParentLeft="true"
android:layout_below="@+id/mytitle"
android:layout_marginLeft="20dp"
android:scaleType="fitXY"
android:paddingLeft="10dp"
android:paddingRight="10dp" />
<TextView
android:id="@+id/descri"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/mytitle"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/icon"
android:layout_marginTop="16dp"
android:background="@drawable/textbox1"
android:text="Description"
android:textColor="#3399FF"
android:textSize="14dp" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/mytitle"
android:layout_toLeftOf="@+id/mytitle"
android:layout_marginLeft="5dp"
android:background="@drawable/buttontextview"
android:text=" Click To Share"
android:textColor="@android:color/white"
android:textSize="13sp" />
This is my RowItem class
public class RowItem {
private String imagepath;
private String title;
private String desc;
public boolean isclicked;
public RowItem(String imagepath,String title,String desc) {
System.out.println("ImagePath is:---"+imagepath);
this.imagepath = imagepath;
System.out.println("Title is:---"+title);
this.title = title;
System.out.println("Description is:---"+desc);
this.desc = desc;
isclicked = false;
}
public String getImagepath() {
return imagepath;
}
public boolean isIsclicked() {
return isclicked;
}
public void setIsclicked(boolean isclicked) {
this.isclicked = isclicked;
}
public void setImagepath(String imagepath) {
this.imagepath = imagepath;
}
public String getTitle() {
return title;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public String toString() {
return title + "\n" + desc;
}
}
This is my onCreate()
Method from the Activity
{ int k = 0;
if(i>0){
rowItems = new ArrayList<RowItem>();
for (int j = 0;j<i ; j++) {
System.out.println("Loop Value:--"+j);
System.out.println("Location Details:-----"+location.get(j));
System.out.println("Description Details:------"+description.get(j));
System.out.println("Image Details:------"+images.get(j));
RowItem item = new RowItem(images.get(j),location.get(j),description.get(j));
rowItems.add(item);
}
listView = (ListView) findViewById(R.id.list);
adapter = new CustomListViewAdapter(PicActivity.this, rowItems);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
}
This is my listitem onClicklistner
method
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
System.out.println("My List Item Clicked");
mylistid = position;
Button deleteBtn = (Button) findViewById(R.id.delete);
deleteBtn.setOnClickListener(this);
File myimagefile = new File(images.get(position));
lastlistid = mydb.getlastlistid();
int list = Integer.valueOf(lastlistid.get(0));
listno=0 ;
listno = list-position;
SharedPreferences mylistpref = getSharedPreferences(Constants.listiddetails, 0);
Editor edit= mylistpref.edit();
edit.putInt("listid", listno);
edit.commit();
System.out.println("First Position Value:-"+position);
Toast toast = Toast.makeText(getApplicationContext(),
"Title " + (position + 1) + ": " + rowItems.get(position),
Toast.LENGTH_SHORT);
toast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
Intent intermediate = new Intent (PicActivity.this,InterMediateActivity.class);
intermediate.putExtra("savenshare", "notshare");
intermediate.putExtra("flag", "false");
startActivity(intermediate);
finish();
}
The Checkbox
is coming in the listview but onItemclicklistner
is not working.
My deleteButton onClicklistner
is like this
case R.id.delete:
System.out.println("My List item:-"+mylistid);
((CustomListViewAdapter)listView.getAdapter()).notifyDataSetChanged();
break;
Here mylistid is a global integer which i am setting while on listitemonclick.
Why my list item onclicklistner not working if i add checkbox in my list and also how can i delete the row and update the listitem.I have followed many tutorials but unable to implement.
Please help !!!!
UPDATE
This is my customListViewAdapter Class
public class CustomListViewAdapter extends BaseAdapter {
private List<RowItem> arr = new ArrayList<RowItem>();
ArrayList<Integer> checks=new ArrayList<Integer>();
private Context context;
Bitmap mybitmap;
private Activity activity;
ViewHolder holder = null;
int mypos=0;
File f;
RowItem rowItem;
public CustomListViewAdapter(Context context,List<RowItem> items) {
//super();
this.arr = items;
this.context=context;
}
private class ViewHolder {
//ImageView imageView;
TextView txtTitle;
TextView txtDesc;
ImageView image;
// CheckBox mychk;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return arr.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
//mypos=arg0;
return arr.get(arg0);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
rowItem = (RowItem) getItem(position);
f = new File(rowItem.getImagepath());
System.out.println("My list item Position from GetView:--"+position);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.txtDesc = (TextView) convertView.findViewById(R.id.descri);
holder.txtTitle = (TextView) convertView.findViewById(R.id.mytitle);
// holder.mychk = (CheckBox) convertView.findViewById(R.id.checkBox1);
holder.image = (ImageView) convertView.findViewById(R.id.icon);
// holder.mychk.setOnCheckedChangeListener(this);
// holder.mychk.setTag(arr.get(position));
// holder.mychk.setChecked(false);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
// holder.mychk.setOnCheckedChangeListener(this);
System.out.println("My Description is:--"+holder.txtDesc.getText());
System.out.println("My Title is:--"+holder.txtTitle.getText());
holder.txtDesc.setText(" "+rowItem.getDesc());
holder.txtTitle.setText(" "+rowItem.getTitle());
if(f.exists())
{
mybitmap = BitmapFactory.decodeFile(f.getAbsolutePath());
Bitmap myeditedbitmap = Bitmap.createScaledBitmap(mybitmap, 180, 150, false);
holder.image.setImageBitmap(myeditedbitmap);
//holder.mychk.setChecked(true);
//setImageResource(rowItem.getImagepath());
}
//holder.txtTitle.setText(rowItem.getTitle());
/* for(int i=0;i<ArrayId.size();i++)
{
rowItem.remove(ArrayId[i]);
}
*/
return convertView;
}
}
}
UPDATE 2
I have updated my ListItem xml to some code and now onItemClick is working..
android:descendantFocusability="blocksDescendants"
Now The other part is..delete row item..which is still not working.and also if i select one checkbox then others are also getting selected.like if i select 1..then 3..5..7 is getting selected..How to avoid this ??
My deleteItem row code..
deleteBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
SparseBooleanArray checkedItemPositions = listView.getCheckedItemPositions();
int itemCount = listView.getCount();
for(int i=itemCount-1; i >= 0; i--){
if(checkedItemPositions.get(i)){
// adapter.remove(rowItems.get(i)); This line is giving me error.first i want to delete row temp and give a toast message.then i will update the database later.but as i am using customarrayadapter class.so i dont have the method remove.Can someone help me regarding the remove method ?
}
}
checkedItemPositions.clear();
adapter.notifyDataSetChanged();
}
});
UPDATE 3
case R.id.delete:
SharedPreferences del = getSharedPreferences(Constants.checkedid,0);
int checkmyid = 0;
checkmyid = del.getInt("check", 0);
int mycount = listView.getCount();
System.out.println("My List item COUNT FROM DELETE METHOD:-"+mycount);
System.out.println("My List item FROM DELETE METHOD:-"+checkmyid);
rowItems.remove(checkmyid);
mydb.delete(checkmyid);
((CustomListViewAdapter)listView.getAdapter()).notifyDataSetChanged();
break;
and my Delete method from database is
public void delete(int id){
String sql = "delete from picdetails where id="+id;
Cursor mych;
mych = myDataBase.rawQuery(sql, null);
}
But when i am reopening the database the data is showing once again.How to avoid ? Also When i am selecting one checkbox to delete that particular checkbox is not getting deleted.the bottom row is getting deleted.
UPDATE 4
i have updated the delete button code..now the row item is getting deleted..but for the first selection its not deleted.its getting deleted from bottom.but the checkitemid is perfect
SharedPreferences del = getSharedPreferences(Constants.checkedid,0);
int checkmyid = 0;
checkmyid = del.getInt("check", 0);
int mycount = listView.getCount();
System.out.println("My List item COUNT FROM DELETE METHOD:-"+mycount);
SparseBooleanArray checked = listView.getCheckedItemPositions();
System.out.println("*****My SELECTED List item FROM DELETE METHOD:-*****"+checkmyid);
rowItems.remove(checkmyid);