Last active
August 29, 2015 14:17
-
-
Save Kun-Yao-Lin/731cc65bca921545e013 to your computer and use it in GitHub Desktop.
TestAdapter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lib.arnold.com.tw.sample; | |
import android.app.Activity; | |
import android.os.Bundle; | |
import android.view.ViewGroup; | |
import android.widget.LinearLayout; | |
import android.widget.ListView; | |
import android.widget.Toast; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* Created by arnold_lin on 2015/3/16. | |
*/ | |
public class MainPane2 extends Activity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
ListView listView = new ListView(this); | |
listView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); | |
setContentView(listView); | |
TestAdapter adapter = new TestAdapter(this); | |
listView.setAdapter(adapter); | |
adapter.setData(getData()); | |
adapter.setOnDeleteLisenter(new TestAdapter.OnDeleteLisenter() { | |
@Override | |
public void onDelete(int position) { | |
Toast.makeText(MainPane2.this,"hihi : "+position,Toast.LENGTH_SHORT).show(); | |
} | |
}); | |
} | |
private List<String> getData(){ | |
List<String> d = new ArrayList<String>(); | |
for(int i = 0;i <5;i++){ | |
d.add(""+i); | |
} | |
return d; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:orientation="horizontal" android:layout_width="match_parent" | |
android:layout_height="50dp"> | |
<TextView | |
android:gravity="center|left" | |
android:text="123123" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:layout_weight="1" | |
android:id="@+id/test" /> | |
<ImageButton | |
android:scaleType="fitCenter" | |
android:src="@drawable/ic_launcher" | |
android:layout_width="wrap_content" | |
android:layout_height="match_parent" | |
android:id="@+id/delete" /> | |
</LinearLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lib.arnold.com.tw.sample; | |
import android.content.Context; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.BaseAdapter; | |
import android.widget.ImageButton; | |
import android.widget.TextView; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* Created by arnold_lin on 2015/3/28. | |
*/ | |
public class TestAdapter extends BaseAdapter { | |
public static interface OnDeleteLisenter { | |
public void onDelete(int position); | |
} | |
private final List<String> data = new ArrayList<String>(); | |
private OnDeleteLisenter onDeleteLisenter; | |
private Context context; | |
public static class ViewHolder{ | |
TextView textView; | |
ImageButton imageButton; | |
} | |
public TestAdapter(Context context) { | |
this.context = context; | |
} | |
@Override | |
public int getCount() { | |
return data.size(); | |
} | |
@Override | |
public Object getItem(int position) { | |
return null; | |
} | |
@Override | |
public long getItemId(int position) { | |
return 0; | |
} | |
@Override | |
public View getView(final int position, View convertView, ViewGroup parent) { | |
ViewHolder holder = null; | |
if(convertView == null){ | |
convertView = View.inflate(context,R.layout.test_item,null); | |
holder = new ViewHolder(); | |
holder.textView = (TextView)convertView.findViewById(R.id.test); | |
holder.imageButton = (ImageButton)convertView.findViewById(R.id.delete); | |
convertView.setTag(holder); | |
}else { | |
holder = (ViewHolder)convertView.getTag(); | |
} | |
holder.textView.setText(data.get(position)); | |
holder.imageButton.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
data.remove(position); | |
notifyDataSetChanged(); | |
if(onDeleteLisenter != null){ | |
onDeleteLisenter.onDelete(position); | |
} | |
} | |
}); | |
return convertView; | |
} | |
public void setData(List<String> data) { | |
if(data != null){ | |
this.data.clear(); | |
this.data.addAll(data); | |
notifyDataSetChanged(); | |
} | |
} | |
public void setOnDeleteLisenter(OnDeleteLisenter onDeleteLisenter) { | |
this.onDeleteLisenter = onDeleteLisenter; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment