我已经构建了这个应用程序的几个例子,当我点击图像按钮(在行项目xml上声明)时,我想删除该项目 我试图实现在按下imagebuttom时调用的回调,但之后如何删除/删除该项? 怎么办? 感谢所有人!
这是我的活动:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Ordine ordine_data[] = new Ordine[]
{
new Ordine("Cloudy1"),
new Ordine("Cloudy2"),
new Ordine("Cloudy3"),
new Ordine("Cloudy4"),
new Ordine("Cloudy5")
};
final ArrayList<Ordine> list = new ArrayList<Ordine>();
for (int i = 0; i < ordine_data.length; ++i) {
list.add(ordine_data[i]);
}
final OrdineAdapter adapter = new OrdineAdapter(this,R.layout.order_list_row, ordine_data);
ListView listview = (ListView) findViewById(R.id.listview);
View header = (View)getLayoutInflater().inflate(R.layout.listview_header, null);
listview.addHeaderView(header);
listview.setAdapter(adapter);
//final StableArrayAdapter adapter = new StableArrayAdapter(this,android.R.layout.simple_list_item_1, list);
//listview.setAdapter(adapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
final String item = (String) parent.getItemAtPosition(position);
view.animate().setDuration(1000).alpha(0)
.withEndAction(new Runnable() {
@Override
public void run() {
list.remove(item);
adapter.notifyDataSetChanged();
view.setAlpha(1);
}
});
}
});
}
private class StableArrayAdapter extends ArrayAdapter<String> {
HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();
public StableArrayAdapter(Context context, int textViewResourceId,
List<String> objects) {
super(context, textViewResourceId, objects);
for (int i = 0; i < objects.size(); ++i) {
mIdMap.put(objects.get(i), i);
}
}
@Override
public long getItemId(int position) {
String item = getItem(position);
return mIdMap.get(item);
}
@Override
public boolean hasStableIds() {
return true;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
这是我的适配器:
public class OrdineAdapter extends ArrayAdapter<Ordine>{
Context context;
int layoutResourceId;
Ordine data[] = null;
public OrdineAdapter(Context context, int layoutResourceId, Ordine[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
OrdineHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new OrdineHolder();
holder.txtTitle = (TextView)row.findViewById(R.id.txtUserName);
row.setTag(holder);
ImageButton btnRemove = (ImageButton)row.findViewById(R.id.remove_product);
btnRemove.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
int i;
i=0; //this is for testing only!!!
}
});
}
else
{
holder = (OrdineHolder)row.getTag();
}
Ordine ordine = data[position];
holder.txtTitle.setText(ordine.title);
return row;
}
static class OrdineHolder
{
TextView txtTitle;
}
}
答案 0 :(得分:1)
您将ordine_data
传递给OridneAdapter
;您的list
变量与ordine_data
之间没有任何关联。您正在使用Ordines
将ArrayList
添加到ordine_data
,但这并不意味着您传递给OridneAdapter
的数据与{{}之间存在关联1 {} list
。您需要在Ordines
(案例中为remove()
变量)中使用ViewAdapter
来传递要删除的parent
,然后调用Ordine
。您可以在notifyDataSetChanged()
:
run()
此外,我认为您需要致电ArrayAdapter<Ordines> adapter = parent.getAdapter();
adapter.remove(adapter.getItem(position));
adapter.notifyDataSetChanged();
让start()
投放。