How to Display two dimensional Array in ListView?

后端 未结 3 1508
悲&欢浪女
悲&欢浪女 2021-02-06 18:22

I Have 2D Array and this 2D Array has Strings. I would like to know How to Display the Strings in ListView?how to scroll both vertically and horizontally?

Strin         


        
3条回答
  •  太阳男子
    2021-02-06 18:43

    It is to display two-d array in list view.Here's my source code in which i have implemented 2-d array in list view

    My Adapter class:-

    public class MyArrayAdapter extends ArrayAdapter{
    
    
        QuickActionDemo quickActionDemo;
        public Activity context;
        public List list;
        int CAMERA_PIC_REQUEST=10;
        private int selectedPos = -1;
        int clickPosition,rowPosition;
        Camera camera;
        private static final String TAG = "CameraDemo";
        public MyArrayAdapter(Activity context,List list) {
            super(context,R.layout.attach_pic,list);
            this.context = context;
            this.list = list;
        }
    
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return list.size();
        }
    
        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position+1;
        }
    
        static class ViewHolder {
            public TextView tv1,tv2,tv3;
    
                }
    
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
    
            View rowView = null;
            final ViewHolder holder = new ViewHolder();
            if (convertView == null) {
    
                LayoutInflater inflator = context.getLayoutInflater();
                rowView = inflator.inflate(R.layout.attach_pic, null);
    
                holder.tv1 = (TextView) rowView.findViewById(R.id.defectpic);
                holder.tv2 = (TextView) rowView.findViewById(R.id.no_of_uploded_pics);
                holder.tv3 = (TextView) rowView.findViewById(R.id.camera);
    
                holder.tv3.setOnClickListener(new View.OnClickListener() {
    
    
                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub  
                //      Intent in = new Intent(getContext(),QuickActionDemo.class);
                //      context.startActivityForResult(in,0);
    
    
    
                        }
                });
    
                rowView.setTag(holder);
                List itemVal1 = (List)getItem(position);
                String st1 = (String)itemVal1.get(0);
                holder.tv1.setText(st1);
    
                List itemVal2 = (List)getItem(position);
                String st2 = (String)itemVal2.get(1);
                holder.tv2.setText(st2);
    
            } else {
                rowView = convertView;
                ((ViewHolder) rowView.getTag()).tv1.setTag(list.get(position));
                ((ViewHolder) rowView.getTag()).tv2.setTag(list.get(position));
                ((ViewHolder) rowView.getTag()).tv3.setTag(list.get(position));
            }
    
            return rowView;
        }
    
        @Override
        public int getItemViewType(int position) {
            // TODO Auto-generated method stub
            return position;
        }
    
        @Override
        public int getViewTypeCount() {
            // TODO Auto-generated method stub
            return list.size();
        }
    
    }
    

    Here's my activity class:-

    public class MyActivity extends ListActivity {
    
        Context context;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
        //  requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); // to hide the virtual keyboard
            setContentView(R.layout.defect_pic_listview);
    
            try{
            ArrayAdapter adapter = new MyArrayAdapter(this,makeList());
            setListAdapter(adapter);    
            }
                }
    private List makeList(){
            List all = new ArrayList();
    
        String[] newArray1 = {"Defect Picture1", "2"};
        List newListObject1 = Arrays.asList(newArray1);
    
        String[] newArray2 = {"Defect Picture2","1"};
        List newListObject2 = Arrays.asList(newArray2);
        String[] newArray3 = {"Defect Picture3","4"};
        List newListObject3 = Arrays.asList(newArray3);
        String[] newArray4 = {"Defect Picture4","1"};
        List newListObject4 = Arrays.asList(newArray4);
        String[] newArray5 = {"Defect Picture5","3"};
        List newListObject5 = Arrays.asList(newArray5);
    
    
        all.add(newListObject1);
        all.add(newListObject2);
        all.add(newListObject3);
        all.add(newListObject4);
        all.add(newListObject5);
    
    
        return all;
    
    }
    
    }
    

提交回复
热议问题