我创建了一个列表视图demo.list已成功创建,图像也正确地添加到右侧。现在我想要当我点击列表视图中行的右侧的交叉图像时,那行将被删除。如何做到这一点。
public class MainActivity extends Activity {
EditText editText;
Button Button,Button1;
ListView listView;
ArrayList<String> listItems;
BaseAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
// int pos = editText.getText().length();
//editText.setSelection(pos);
Button = (Button) findViewById(R.id.Button);
listView = (ListView) findViewById(R.id.listview);
//listView.setSelection(listView.getAdapter().getCount()-1);
listItems = new ArrayList<String>();
//listItems.add("First item");
//code to only set the simple text in the listview
//adapter = new ArrayAdapter<String>(this, R.layout., listItems);
//Here is the creation of new anonymous class,because we can not create subclass inside any method so we create anonymous class inside the on create method
adapter =new BaseAdapter()
{
@Override
public View getView(int arg0, View arg1, ViewGroup arg2)
{
// arg1 is the particular row position in list view weather the arg0 is the whole list view
// layout inflater for setting the text or an image on row in list view
LayoutInflater inflater = getLayoutInflater();
arg1 = inflater.inflate(R.layout.custom, null);
TextView textview = (TextView)arg1. findViewById(R.id.textView1);
textview.setText(listItems.get(arg0));
return arg1;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return listItems.size();
}
};
listView.setAdapter(adapter);
Button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
listItems.add(editText.getText().toString());
adapter.notifyDataSetChanged();
// code to display the previous added text in front through edit text in
listView.setSelection(listView.getAdapter().getCount()-1);
//code to clear edit text after enter the text in list view
editText.getText().clear();
}
});
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
Toast.makeText(MainActivity.this, "Item has been added into your list", Toast.LENGTH_LONG)
.show();
}
});
}
}
答案 0 :(得分:6)
listItems.remove(position);
此处的位置是您的列表行ID
notifyDataSetChanged();
答案 1 :(得分:0)
向listItem添加删除按钮,或将onClickListener添加到图像
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = null;
LayoutInflater inflater = getLayoutInflater();
row = inflater.inflate(R.layout.one_result_details_row, parent, false);
ImageView image= (ImageView) row.findViewById(R.id.your_image);
image.setTag(position);
image.setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
Integer index = (Integer) view.getTag();
listItems.remove(index.intValue());
notifyDataSetChanged();
}
}
);
答案 2 :(得分:0)
close.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
values.remove(position);
SaleProductListAdapter.this.notifyDataSetChanged();
}
});