我正在从数组列表中获取团队,并在列表视图中显示他们的徽标。它工作没有问题。但是,当我长时间单击带有是 - 否警报对话框的列表视图项目时,我想删除一个项目。这是我的代码和自定义适配器。
package com.mesutemre.takimlarlistview;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class TakimBaseAdapter extends BaseAdapter {
Context context;
private LayoutInflater inflater = null;
private TextView lblAd, lblAciklama;
private ImageView imgTakim;
private ArrayList<Takim> items;
public TakimBaseAdapter(Context context, ArrayList<Takim> items) {
this.context = context;
this.items = items;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int position) {
return items.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.activity_main, null);
lblAd = (TextView) vi.findViewById(R.id.textView1);
lblAd.setTextColor(Color.BLUE);
lblAciklama = (TextView) vi.findViewById(R.id.textViewAciklama);
imgTakim = (ImageView) vi.findViewById(R.id.takimImage);
lblAd.setText(items.get(position).getTakim_ad());
lblAciklama.setText(items.get(position).getTakim_aciklama());
int logoID = context.getResources().getIdentifier(
items.get(position).getImage(), "drawable",
context.getPackageName());
imgTakim.setImageResource(logoID);
return vi;
}
}
我将ArrayList
团队的项目放在MainActivity中,它就在这里;
public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*final ArrayAdapter<Takim> adapter = new TakimAdapter(this,
R.layout.activity_main, getTakimlar());*/
final BaseAdapter adapter = new TakimBaseAdapter(MainActivity.this, getTakimlar());
setListAdapter(adapter);
getListView().setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long arg3) {
Takim stakim = (Takim) parent.getItemAtPosition(position);
Toast.makeText(getBaseContext(),
"Takım : " + stakim.getTakim_ad(), Toast.LENGTH_SHORT)
.show();
}
});
getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
final int position, long arg3) {
final Takim stakim = (Takim) parent.getItemAtPosition(position);
// burada AlertDialog.Builder'ın constructor'ına dikkat edin.
// Listactivitymizin context'ini atıyoruz.
AlertDialog.Builder builder = new AlertDialog.Builder(
MainActivity.this);
builder.setMessage("Bu takımı silmek istediğinizden emin misiniz?");
builder.setPositiveButton("Evet",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
getTakimlar().remove(which);
adapter.notifyDataSetChanged();
}
});
builder.setNegativeButton("Hayır",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
adapter.notifyDataSetChanged();
}
});
builder.show();
return false;
}
});
}
@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;
}
// Takimlar ekleniyor
private ArrayList<Takim> getTakimlar() {
ArrayList<Takim> takimList = new ArrayList<Takim>();
takimList.add(new Takim("Galatasaray", "19", "galatasaray"));
takimList.add(new Takim("Fenerbahçe", "18", "fenerbahce"));
takimList.add(new Takim("Beşiktaş", "13", "bjk"));
takimList.add(new Takim("Trabzonspor", "6", "trabzon"));
takimList.add(new Takim("Bursaspor", "1", "bursaspor"));
return takimList;
}
}
我是ArrayOutofBound Exception
因为getTakimlar().remove(which);
. 在这种情况下,如何从我的物品中移除物品ArrayList
?