exam-01

exam-01
**自定义Adpter时,出现了闪退,不知道为什么,检查了很久都没有发现问题^_^
/res/menu/main.xml**

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
   <item
       android:id="@+id/id_red"
       android:title="红色" />
    <item
        android:id="@+id/id_green"
        android:title="绿色" />
    <item
        android:id="@+id/id_blue"
        android:title="蓝色"/>
    <item
        android:id="@+id/id_white"
        android:title="白色"/>

</menu>

/res/layout/item_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/id_listview_item"
        android:layout_width="match_parent"
        android:layout_height="35dp" />
</LinearLayout>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:orientation="horizontal">
        <EditText
            android:id="@+id/id_EditText"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="4"
            android:hint="请输入姓名" />
        <Button
            android:id="@+id/id_btnAdd"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="添加" />

    </LinearLayout>
    <ListView
        android:id="@+id/id_listview"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

    </ListView>
    <TextView
        android:id="@+id/id_showTextView"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:text="你的选择是:"/>

</LinearLayout>

MyAdapter.java

package com.example.myapplication;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MyAdapter extends BaseAdapter {

    private List<String> list = new ArrayList<>();
    private Context context;

   public MyAdapter(Context context, List<String> obj){
       this.context = context;
       this.list = obj;
   }

    @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) {
        View view;
        ViewHolder viewHolder;
        if(convertView!=null){
            view = LayoutInflater.from(context).inflate(R.layout.item_layout,null);
            viewHolder = new ViewHolder();
            viewHolder.textView = (TextView) view.findViewById(R.id.id_listview_item);
            view.setTag(viewHolder);//将ViewHolder存储在View中
        }else{
            view = convertView;
            viewHolder = (ViewHolder) view.getTag(); //重新获取ViewHolder
        }
        TextView textView = view.findViewById(R.id.id_showTextView);
        textView.setText("你的选择是:"+list.get(position));
        return view;
    }

    //采用内部类ViewHolder对控件的实例进行缓存,优化ListView的运行效率
    class ViewHolder{
       TextView textView;
    }
}

MainActivity.java

package com.example.myapplication;

import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private TextView textView_show;
    private EditText editText_add;
    private Button btn_add;
    private ListView listView;
    private List<String> datas = new ArrayList<>();
    private MyAdapter myAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化View
        initView();

        datas.add("text1");
        datas.add("text2");
        datas.add("text3");
        //初始化适配器
        //myAdapter = new MyAdapter(this,datas);
       final TestAdapter adapter = new TestAdapter(this,datas);
        //listView绑定适配器
        //final  ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,datas);
        listView.setAdapter(adapter);
        //Button的点击事件
        btn_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                datas.add(editText_add.getText()+"");
                editText_add.setText("");
                adapter.notifyDataSetChanged();
            }
        });
        //listView的item点击事件
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("提醒");
                builder.setMessage("是否删除");
                builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        datas.remove(position);
                        adapter.notifyDataSetChanged();
                    }
                });
                builder.setNegativeButton("否", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder.show();
                return true;
            }
        });
    }

    public void initView(){
        editText_add= (EditText) findViewById(R.id.id_EditText);
        //textView_show = (TextView) findViewById(R.id.id_showTextView);
        btn_add = (Button) findViewById(R.id.id_btnAdd);
        listView = (ListView) findViewById(R.id.id_listview);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.id_red:
                listView.setBackgroundColor(Color.RED);
                break;
            case R.id.id_blue:
                listView.setBackgroundColor(Color.BLUE);
                break;
            case R.id.id_green:
                listView.setBackgroundColor(Color.GREEN);
                break;
            case R.id.id_white:
                listView.setBackgroundColor(Color.WHITE);
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    static class TestAdapter extends BaseAdapter {
        private List<String> list;
        private Context context;
        public TestAdapter(Context context, List<String> objects){
            this.context = context;
            this.list = objects;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int i) {
            return list.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            View v;
            if (view == null)
            {
                v = LayoutInflater.from(context).inflate(R.layout.item_layout,null);

            }
            else {
                v = view;
            }
            String s = list.get(i);
            TextView textView = v.findViewById(R.id.id_showTextView);
            textView.setText(s+"");
            return v;
        }
    }
}
上一篇:MySql


下一篇:汇编语言学习笔记(4)——汇编基本指令集