Gambar 1. ListView Dinamis ( Model 1 ) |
*Pada MenambahKontakPerusahaanActivity.java menggunakan notifyDataSetChanged() yang berguna untuk meng-update listview setelah drawer diberi masukan nilai baru. Dalam class LazyAdapter juga menggunakan notifyDataSetChanged() yang berguna mengupdate listview setelah nilai dalam drawer ada yang dihapus.
*Bila belum pernah membuat custom listview lebih baik membuatnya terlebih dahulu sehingga anda sudah mengenali cara kerja/fungsi LazyAdapter.
MenambahKontakPerusahaanActivity.java (MainActivity)
import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.ImageButton; import android.widget.ListView; import android.widget.Toast; import java.util.ArrayList; /** * Created by Windows on 19/07/2016. */ public class MenambahKontakPerusahaanActivity extends Activity { ArrayList<String> perusahaan; navDrawerItems test; ListView listView; ArrayList<navDrawerItems> drawer; int batas=0; LazyAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_menambahkontakperusahaan); ImageButton imageButtonAddPerusaahan= (ImageButton)findViewById(R.id.imageButtonAddPerusaahan); listView= (ListView) findViewById(R.id.listView2); perusahaan = new ArrayList<String>(); perusahaan.add("perusahaan A"); perusahaan.add("perusahaan B"); perusahaan.add("perusahaan C"); perusahaan.add("perusahaan D"); listView.requestLayout(); drawer=new ArrayList<navDrawerItems>(); adapter= new LazyAdapter(MenambahKontakPerusahaanActivity.this, drawer); listView.setAdapter(adapter); imageButtonAddPerusaahan.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { test = new navDrawerItems(); test.setNamaPerusahaan(perusahaan.get(batas)); drawer.add(test); adapter.notifyDataSetChanged(); batas++; } } ); }}
LazyAdapter.java
import android.app.Activity; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.TextView; import android.widget.ImageView.ScaleType; import android.widget.Toast; public class LazyAdapter extends BaseAdapter { private Context context; ArrayList<navDrawerItems> drawer; public LazyAdapter(Context context, ArrayList<navDrawerItems> drawer) { this.context = context; this.drawer=drawer; } @Override public View getView(final int position, View convertView, ViewGroup parent) { if (convertView == null) { LayoutInflater mInflater = (LayoutInflater) context .getSystemService(Activity.LAYOUT_INFLATER_SERVICE); convertView = mInflater.inflate(R.layout.listviewkontakperusahaan, null); } //ImageView imgIcon = (ImageView) convertView.findViewById(R.id.list_image); TextView textView13 = (TextView) convertView.findViewById(R.id.textView13); ImageButton buttonASD = (ImageButton) convertView.findViewById(R.id.buttonASD); textView13.setText(drawer.get(position).getNamaPerusahaan()); buttonASD.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { drawer.remove(position); notifyDataSetChanged(); } }); /* try { Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(drawer.getIcon()).getContent()); imgIcon.setImageBitmap(bitmap); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }*/ return convertView; } @Override public int getCount() { // TODO Auto-generated method stub return drawer.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return drawer.get(position); } @Override public long getItemId(int position) { return position; }}
activity_menambahkontakperusahaan,xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:paddingRight="5dp" android:layout_height="match_parent"> <LinearLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:divider="@null" android:clickable="true" android:id="@+id/listView2" /> </LinearLayout> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageButtonAddPerusaahan" android:src="@drawable/ic_add_circle_outline_white_24dp" android:background="@android:color/holo_blue_dark" android:layout_gravity="right" /> </LinearLayout>
listviewkontakperusahaan.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:padding="5dp" android:layout_height="match_parent"> <LinearLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="Medium Text" android:id="@+id/textView13" /> <ImageButton android:src="@drawable/ic_remove_circle_outline_white_24dp" android:layout_width="wrap_content" android:layout_marginLeft="5dp" android:id="@+id/buttonASD" android:background="@android:color/holo_blue_dark" android:layout_height="wrap_content" android:layout_alignTop="@+id/textView13" android:layout_alignBottom="@+id/textView13" /> <Button android:layout_width="wrap_content" android:layout_height="0dp" android:visibility="invisible" android:id="@+id/imageButton" android:src="@drawable/ic_add_circle_outline_white_24dp" android:background="@android:color/holo_blue_dark" /> </LinearLayout> </RelativeLayout>
Sekian dan Trimakasih..
0 komentar:
Post a Comment