YXTS122
YXTS122
2017-04-30 05:55
采纳率: 100%
浏览 1.6k

程序运行后,不崩溃但一片空白

 package com.example.leadus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
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.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

public class SjcsActivity extends Activity {
    private ListView lis;
    private List<String> list;
    private MyAdapter myadapter;
    private List<Map<String, Object>> datalist;
    private  ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.shijianceshi);

            list =new ArrayList<String>();
            list.add("北京");
            list.add("上海");
            list.add("广州");
            list.add("深圳");

            adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,list);
            //adapter设置一个下拉列表的样式
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

            lis = (ListView) findViewById(R.id.listView);
            datalist=new ArrayList<Map<String,Object>>();
            myadapter = new MyAdapter(this,datalist);
            lis.setAdapter(myadapter);
           // lis.setOnItemClickListener((AdapterView.OnItemClickListener) this);
           // lis.setOnScrollListener((AbsListView.OnScrollListener) this);
    }

    private List<Map<String, Object>> getData() {
        for (int i = 1; i <7; i++) {
            String[]arr={"","A","B","C","D","E","F"};
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("listView_text1",i);
            map.put("listView_text2", "主"+arr[i] );
            map.put("listView_spinner1",i);
            datalist.add(map);

        }
        return datalist;
    }

    public class MyAdapter extends BaseAdapter {
        private Context context;
        private ArrayList<Map<String,Object>> datalist;
        private LayoutInflater inflater;

        public MyAdapter(Context context,List<Map<String, Object>> datalist) {
            this.context=context;
            this.inflater=LayoutInflater.from(context);
            this.datalist=(ArrayList<Map<String,Object>>)datalist;

        }

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

        @Override
        public Object getItem(int position)
        {
            return datalist.get(position);
        }

        @Override
        public long getItemId(int position)
        {
            return position;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = null;
            ViewHolder viewHolder = null;
            if (convertView == null) {
                view =inflater.inflate(R.layout.shijianceshi_listviewlayout, null);
                viewHolder = new ViewHolder();
                viewHolder.tv1=(TextView)view.findViewById(R.id.listView_text1);
                viewHolder.tv2=(TextView)view.findViewById(R.id.listView_text2);
                viewHolder.spinner1= (Spinner)view.findViewById(R.id.listView_spinner1);
                view.setTag(viewHolder);
            } else {
                view = convertView;
                viewHolder=(ViewHolder)view.getTag();
            }

            viewHolder.tv1.setVisibility(View.VISIBLE);
            viewHolder.tv2.setVisibility(View.VISIBLE);
            viewHolder.tv1.setText(String.valueOf(datalist.get(position).get("listView_text1")));
            viewHolder.tv2.setText(String.valueOf(datalist.get(position).get("listView_text2")));
            viewHolder.spinner1.setAdapter(adapter);
            return view;
        }

        public class ViewHolder {
            TextView tv1;
            TextView tv2;
            Spinner spinner1;
            //你的view中的控件
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.sjcs, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

图片说明
图片说明

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

6条回答 默认 最新

  • fight_in_dl
    战在春秋 2017-04-30 11:36
    已采纳

    代码逻辑如下,具体值根据你的情况而定。

    datalist=new ArrayList<Map<String,Object>>();
    
    Map<String, Object> map1 = new HashMap<String, Object>();
    map1.put("name", "Y");
    map1.put("num", 1);
    
    Map<String, Object> map2 = new HashMap<String, Object>();
    map2.put("name", "A");
    map2.put("num", 3);
    
    Map<String, Object> map3 = new HashMap<String, Object>();
    map3.put("name", "D");
    map3.put("num", 2);
    
    
    datalist.add(map1);
    datalist.add(map2);
    datalist.add(map3);
    
    点赞 评论
  • oSanYeCao1234567
    RoidCoder 2017-04-30 08:34

    datalist=new ArrayList>();
    myadapter = new MyAdapter(this,datalist);
    lis.setAdapter(myadapter);
    MyAdapter中的datalist并没有给他添加数据,当然是空白的

    点赞 评论
  • oSanYeCao1234567
    RoidCoder 2017-04-30 08:51

    datalist.add 添加数据啊

    点赞 评论
  • qq_24531461
    Luyifei666 2017-04-30 09:41

    datalist是空的,你new了之后并没有给他赋值,赋值了之后就有了

    点赞 评论
  • YXTS122
    YXTS122 2017-04-30 11:50

    解决了

     package com.example.leadus;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import android.app.Activity;
    import android.content.Context;
    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.ArrayAdapter;
    import android.widget.BaseAdapter;
    import android.widget.ListView;
    import android.widget.Spinner;
    import android.widget.TextView;
    
    public class SjcsActivity extends Activity {
        private ListView lis;
        private List<String> list;
        private MyAdapter myadapter;
        private List<Map<String, Object>> datalist;
        private  ArrayAdapter<String> adapter;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.shijianceshi);
    
                list =new ArrayList<String>();
                list.add("北京");
                list.add("上海");
                list.add("广州");
                list.add("深圳");
    
                adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,list);
                //adapter设置一个下拉列表的样式
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    
                lis = (ListView) findViewById(R.id.listView);
                datalist=new ArrayList<Map<String,Object>>();
                datalist=getData();
                myadapter = new MyAdapter(this,datalist);
                lis.setAdapter(myadapter);
               // lis.setOnItemClickListener((AdapterView.OnItemClickListener) this);
               // lis.setOnScrollListener((AbsListView.OnScrollListener) this);
        }
    
        private List<Map<String, Object>> getData() {
            List<Map<String, Object>> datalist1=new ArrayList<Map<String,Object>>();
            for (int i = 1; i <7; i++) {
                String[]arr={"","A","B","C","D","E","F"};
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("listView_text1",i);
                map.put("listView_text2", "主"+arr[i] );
                map.put("listView_spinner1",i);
                datalist1.add(map);
    
            }
            return datalist1;
        }
    
        public class MyAdapter extends BaseAdapter {
            private Context context;
            private ArrayList<Map<String,Object>> datalist;
            private LayoutInflater inflater;
    
            public MyAdapter(Context context,List<Map<String, Object>> datalist) {
                this.context=context;
                this.inflater=LayoutInflater.from(context);
                this.datalist=(ArrayList<Map<String,Object>>)datalist;
    
            }
    
            @Override
            public int getCount()
            {
                return datalist.size();
            }
    
            @Override
            public Object getItem(int position)
            {
                return datalist.get(position);
            }
    
            @Override
            public long getItemId(int position)
            {
                return position;
            }
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View view = null;
                ViewHolder viewHolder = null;
                if (convertView == null) {
                    view =inflater.inflate(R.layout.shijianceshi_listviewlayout, null);
                    viewHolder = new ViewHolder();
                    viewHolder.tv1=(TextView)view.findViewById(R.id.listView_text1);
                    viewHolder.tv2=(TextView)view.findViewById(R.id.listView_text2);
                    viewHolder.spinner1= (Spinner)view.findViewById(R.id.listView_spinner1);
                    view.setTag(viewHolder);
                } else {
                    view = convertView;
                    viewHolder=(ViewHolder)view.getTag();
                }
    
                viewHolder.tv1.setVisibility(View.VISIBLE);
                viewHolder.tv2.setVisibility(View.VISIBLE);
                viewHolder.tv1.setText(String.valueOf(datalist.get(position).get("listView_text1")));
                viewHolder.tv2.setText(String.valueOf(datalist.get(position).get("listView_text2")));
                viewHolder.spinner1.setAdapter(adapter);
                return view;
            }
    
            public class ViewHolder {
                TextView tv1;
                TextView tv2;
                Spinner spinner1;
                //你的view中的控件
            }
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.sjcs, menu);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
            if (id == R.id.action_settings) {
                return true;
            }
            return super.onOptionsItemSelected(item);
        }
    }
    
    

    图片说明

    点赞 评论
  • qq_40232166
    小小小白鼠 2017-11-29 01:43

    datalist是空的

    点赞 评论

相关推荐