Struture goes like this :
1. MainActivity.this
public class MainActivity extends Activity implements Callback{ ListView mListview; ArrayList<String>mItems; ListviewAdapter mAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); intiview(); } private void intiview() { mListview=(ListView)findViewById(R.id.mListview); //preparing list for items here mItems=new ArrayList<String>(); for(int i=0;i<15;i++){ mItems.add(""+i); } mAdapter=new ListviewAdapter(mItems, MainActivity.this); mListview.setAdapter(mAdapter); } @Override public void onDeleteClick() { mAdapter.notifyDataSetChanged(); } }2. ListviewAdapter.this
public class ListviewAdapter extends BaseAdapter { ArrayList<String> mItems; Context mContext; Callback mCallback; public ListviewAdapter(ArrayList<String> mItems, Context mContext) { this.mContext = mContext; this.mItems = mItems; mCallback=(Callback) mContext; } @Override public int getCount() { return mItems.size(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @SuppressLint("InflateParams") @Override public View getView(final int position, View convertView, ViewGroup parent) { View mView = convertView; ViewHolder mHolder; // to reference the child views for later actions if (mView == null) { LayoutInflater mInflater = (LayoutInflater) mContext .getSystemService(Context.LAYOUT_INFLATER_SERVICE); mView = mInflater.inflate(R.layout.row_listview, null); // cache view fields into the holder mHolder = new ViewHolder(); mHolder.mName = (TextView) mView.findViewById(R.id.name); mHolder.mDeletebtn = (Button) mView.findViewById(R.id.delbtn); // associate the holder with the view for later lookup mView.setTag(mHolder); } else { // view already exists, get the holder instance from the view mHolder = (ViewHolder) mView.getTag(); } mHolder.mName.setText(mItems.get(position)); mHolder.mDeletebtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mItems.remove(position); mCallback.onDeleteClick(); } }); ; return mView; } static class ViewHolder { TextView mName; Button mDeletebtn; } }3. Callback.java
public interface Callback { void onDeleteClick(); }
XML Layouts :
1. activity_main.xml
<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" > <ListView android:id="@+id/mListview" android:layout_width="wrap_content" android:layout_height="wrap_content" > </ListView> </RelativeLayout>2. row_listview.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <TextView android:id="@+id/name" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight=".6" android:padding="10dp" android:text="Name" /> <Button android:id="@+id/delbtn" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight=".4" android:padding="10dp" android:text="Delete Item" /> </LinearLayout>Output :
No comments:
Post a Comment