weixin_39083019
2017-06-07 10:28
采纳率: 100%
浏览 3.8k
已采纳

求宠物管理系统Java代码

求好心人帮忙写个宠物管理系统,具有增删改查功能,不用数据库,谢谢!!

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • weixin_39083019 2017-06-08 06:24
    已采纳

    布局是什么意思?,可简化为主函数输入吗

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • qq_38690340 2017-06-07 13:48

    private ListView listView;
    private ArrayList list;
    private EditText editText;
    private Listadapter listadapter;
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
    
        listView = (ListView) findViewById(R.id.list1);
        textView = (TextView) findViewById(R.id.text2);
    
        initData();
    
        listadapter = new Listadapter();
    
        listView.setAdapter(listadapter);
    
    
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder aler=new AlertDialog.Builder(MainActivity.this);
                View inflate = LayoutInflater.from(MainActivity.this).inflate(R.layout.one_view, null);
                editText = (EditText) inflate.findViewById(R.id.ed1);
                aler.setTitle("请输增加内容:");
                aler.setView(inflate);
                aler.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });
                aler.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if(editText.getText().toString()!=null){
                            list.add(editText.getText().toString());
                            listadapter.notifyDataSetChanged();
                        }else{
                            Toast.makeText(MainActivity.this,"你输入为空",Toast.LENGTH_LONG);
                        }
                    }
                });
                aler.show();
                aler.create();
            }
        });
    
    
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, final long id) {
                AlertDialog.Builder aler=new AlertDialog.Builder(MainActivity.this);
                View inflate = LayoutInflater.from(MainActivity.this).inflate(R.layout.one_view, null);
                editText = (EditText) inflate.findViewById(R.id.ed1);
                aler.setTitle("请输修改内容:");
                aler.setView(inflate);
                aler.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });
                aler.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if(editText.getText().toString()!=null){
                            list.set(position,editText.getText().toString());
                            listadapter.notifyDataSetChanged();
                        }else{
                            Toast.makeText(MainActivity.this,"你输入为空",Toast.LENGTH_LONG);
                        }
                        }
                });
                aler.show();
                aler.create();
            }
        });
    
    
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
    
                AlertDialog.Builder aler=new AlertDialog.Builder(MainActivity.this);
                aler.setTitle("确定删除?");
                aler.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });
                aler.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        list.remove(position);
                        listadapter.notifyDataSetChanged();
                    }
                });
                aler.show();
                aler.create();
    
                return true;
            }
        });
    
    
    }
    
    private void initData() {
    
    
        list = new ArrayList<String>();
    
        for (int i = 0; i <10 ; i++) {
    
            list.add("宠物"+i);
    
        }
    }
    
    
    
    class Listadapter  extends BaseAdapter{
    
    
        @Override
        public int getCount() {
            return list.size();
        }
    
        @Override
        public Object getItem(int position) {
            return list.get(position);
        }
    
        @Override
        public long getItemId(int position) {
            return position;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if(convertView==null){
                convertView = LayoutInflater.from(MainActivity.this).inflate(R.layout.it_one, null);
                viewHolder=new ViewHolder();
                viewHolder.textView= (TextView) convertView.findViewById(R.id.text1);
                convertView.setTag(viewHolder);
            }else{
                viewHolder= (ViewHolder) convertView.getTag();
            }
               viewHolder.textView.setText(list.get(position));
            return convertView;
        }
        class ViewHolder{
            TextView textView;
        }
    }
    

    布局
    android:layout_width="match_parent"
    android:layout_height="20dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="长按删除,点击修改"
            android:textColor="#000"
            />
    
        <TextView
            android:id="@+id/text3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="+查询"
            android:textSize="10sp"
            android:textColor="#000"
            android:layout_marginLeft="180dp"/>
    
         <TextView
             android:id="@+id/text2"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="+添加"
             android:textSize="10sp"
             android:textColor="#000"
             android:layout_marginLeft="10dp"/>
    </LinearLayout>
    
    
    
    
    <ListView
        android:id="@+id/list1"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </ListView>
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题