我ListView
在其中设置ImageView
,2TextView
和 2 ImageButton
。ImageButtons
用于添加和删除 listItem。
这是我的代码...
Helper.java
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
.
.
.
import com.example.customObject.ContactBean;
import com.example.helper.HelperAdaptor;
import com.example.helper.ImageHelper;
public class Helper extends Activity implements OnItemClickListener {
private List<ContactBean> list = new ArrayList<ContactBean>();
private ListView listView;
private Button add_helper;
public HelperAdaptor objAdapter ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_helper);
listView = (ListView) findViewById(R.id.lstview_helper_helperlist);
listView.setOnItemClickListener(this);
add_helper = (Button) findViewById(R.id.btn_helper_addhelper);
add_helper.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),
ContactListActivity.class);
startActivityForResult(intent, SELECT_CONTACT);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_CONTACT) {
ContactBean objContact = new ContactBean();
objContact.setName(helperName);
objContact.setPhoneNo(helperNumber);
objContact.setPic(image);
list.add(objContact);
objAdapter = new HelperAdaptor(Helper.this,
R.layout.activity_helper_rows, list);
listView.setAdapter(objAdapter);
}
}
}
@Override
public void onItemClick(AdapterView<?> listview, View v, int position,
long id) {
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.helper, menu);
return true;
}
}
HelperAdapter.java
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
.
.
.
import com.example.customObject.ContactBean;
import com.example.helpmerestart.ContactListActivity;
import com.example.helpmerestart.R;
public class HelperAdaptor extends ArrayAdapter<ContactBean> {
private Activity activity;
private List<ContactBean> items;
private int row;
private ContactBean objBean;
private ViewHolder holder;
private int itemIndex;
private View view;
public HelperAdaptor(Activity act, int row, List<ContactBean> items) {
super(act, row, items);
this.activity = act;
this.row = row;
this.items = items;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
view = convertView;
itemIndex = position;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(row, null);
holder = new ViewHolder();
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
if ((items == null) || ((position + 1) > items.size()))
return view;
objBean = items.get(position);
holder.add = (ImageButton) view.findViewById(R.id.btn_helper_add);
holder.remove = (ImageButton) view.findViewById(R.id.btn_helper_remove);
if (holder.tvname != null && null != objBean.getName()
&& objBean.getName().trim().length() > 0) {
holder.tvname.setText(Html.fromHtml(objBean.getName()));
}
if (holder.tvPhoneNo != null && null != objBean.getPhoneNo()
&& objBean.getPhoneNo().trim().length() > 0) {
holder.tvPhoneNo.setText(Html.fromHtml(objBean.getPhoneNo()));
}
if (holder.profilepic != null && null != objBean.getPic()) {
holder.profilepic.setImageBitmap(objBean.getPic());
}
holder.add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
addHelper();
}
});
holder.remove.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
items.remove(itemIndex);
//trying to call notifyDataSetChanged() here
}
}
);
return view;
}
public void addHelper() {
Intent intent = new Intent(activity.getApplicationContext(),
ContactListActivity.class);
activity.startActivityForResult(intent, SELECT_CONTACT);
holder.add.setClickable(false);
holder.remove.setClickable(true);
}
public class ViewHolder {
public TextView tvname, tvPhoneNo;
public ImageView profilepic;
public ImageButton add, remove;
}
}
问题是我无法删除单击删除按钮的 listItem。你可以参考上一篇文章来了解我到底想要什么。我试图在删除按钮的 onClick() 中调用 notdifyDataSetChanged() 但不知道如何做到这一点。
我可以通过任何其他方法来实现我的目标。
问候..
Sourabh