有几个例子我已经构建了这个应用程序,我想在单击图像按钮时删除该项目(在行项目 xml 上声明)我试图实现在按下图像按钮时调用的回调,但是在如何删除 /删除项目?怎么能这样做?谢谢大家!
这是我的活动:
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;
}
}