下拉选择框加listview删除

时间:2022-02-16 17:00:17
package com.downselect;

import java.util.ArrayList;

import android.R.array;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView; public class MainActivity extends Activity implements OnClickListener { private EditText edittext;
private ImageView select; private ArrayList<String> list = new ArrayList<String>();
private ListView listview;
private int popupheight=300;
private PopupWindow popup; @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState); initView();
initListener();
initData();
} private void initView() {
setContentView(R.layout.activity_main);
edittext = (EditText) findViewById(R.id.edittext);
select = (ImageView) findViewById(R.id.select); } private void initListener() {
select.setOnClickListener(this); } private void initData() { for (int i = 0; i < 15; i++) {
list.add(90000 + i + "");
} initlistview();
} private void initlistview(){
listview = new ListView(this);
listview.setBackgroundResource(R.drawable.timeline_card_bottom_background_highlighted);
listview.setVerticalScrollBarEnabled(false);//不显示滚动条 listview.setOnItemClickListener(new OnItemClickListener() { @Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) { edittext.setText(list.get(position));
popup.dismiss();
}
}); listview.setAdapter(new ListviewAdapter());
} private void showselectlistview(){ popup = new PopupWindow(listview, edittext.getWidth()
, popupheight); popup.setFocusable(true);//让其中的view获取焦点
//还必须设置背景图片
popup.setBackgroundDrawable(new BitmapDrawable());
popup.setOutsideTouchable(true); popup.showAsDropDown(edittext, 0, 0); }
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.select:
showselectlistview();
break; default:
break;
}
} class ListviewAdapter extends BaseAdapter { @Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
} @Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
} @Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
} @Override
public View getView(final int position, View convertView, ViewGroup parent) {
final View view = convertView.inflate(MainActivity.this, R.layout.listitem, null); TextView text = (TextView) view.findViewById(R.id.text); ImageView iv_delect = (ImageView) view.findViewById(R.id.iv_delect); text.setText(list.get(position)); iv_delect.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
list.remove(position);
notifyDataSetChanged(); int listheight=view.getHeight()*list.size();
popup.update(edittext.getWidth(), listheight>popupheight?
popupheight:listheight); if(list.size()==0){
popup.dismiss();
select.setVisibility(View.GONE);
} }
}); return view;
} }
下拉选择框加listview删除
}