我有ListView
我在其中设置了ImageView
,2 TextView
和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。你可以参考Previous post来了解我想拥有什么。我试图在删除按钮的onClick()中调用notdifyDataSetChanged(),但没有得到如何做到这一点。
我可以通过任何其他方法来实现我的目标
问候..
Sourabh
答案 0 :(得分:1)
您使用ArrayAdapter的方法不对。不要保留自己的项目列表(私有列表项),因为ArrayAdapter里面有自己的项目列表。要向listView添加和删除项,请使用ArrayAdapter的add / addAll并删除方法。如果要使项目正常工作,则应始终使用ArrayAdapters方法管理项目。
如果您不需要ArrayAdapter的功能,请扩展BaseAdapter。
P.S。您还有itemIndex / holder变量的问题。 P.P.S.你使用Holder错误的方式。 (每次getView调用都会执行findViewByIds)
您的代码中存在大量错误......