3

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?

String[][] board = new String[][] {{"1","10","100"},{"hi0","1hello","test"},{"test31","test32","test43"}};
4
  • Please provide requirement and ask clearly. Its not good to understand your question. We can display strings in ListView even if it is 1D or 2D. Where you got this requirement please post it, then only we will help you. Commented Dec 29, 2011 at 10:47
  • By "two dimentional Array" you mean that you'll be able to scroll both vertically and horizontally? Commented Dec 29, 2011 at 10:52
  • which items you want to scroll vertically? and which horizontally ? rows or columns ? what is your actual requirement ? Commented Jan 2, 2012 at 5:36
  • Seems pretty clear to me. ListView is a one-dimensional UI for one-dimensional arrays. He wants a two-dimensional UI to display his two-dimensional arrays. Commented Jan 22, 2012 at 18:48

3 Answers 3

1

It seem to be you are asking basic things, How to use ListView. please check it you will get all about ListView.

Android ListView and ListActivity

Sign up to request clarification or add additional context in comments.

Comments

1

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<List>{


    QuickActionDemo quickActionDemo;
    public Activity context;
    public List<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> 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<List> adapter = new MyArrayAdapter(this,makeList());
        setListAdapter(adapter);    
        }
            }
private List<List> makeList(){
        List<List> all = new ArrayList();

    String[] newArray1 = {"Defect Picture1", "2"};
    List<String> newListObject1 = Arrays.asList(newArray1);

    String[] newArray2 = {"Defect Picture2","1"};
    List<String> newListObject2 = Arrays.asList(newArray2);
    String[] newArray3 = {"Defect Picture3","4"};
    List<String> newListObject3 = Arrays.asList(newArray3);
    String[] newArray4 = {"Defect Picture4","1"};
    List<String> newListObject4 = Arrays.asList(newArray4);
    String[] newArray5 = {"Defect Picture5","3"};
    List<String> newListObject5 = Arrays.asList(newArray5);


    all.add(newListObject1);
    all.add(newListObject2);
    all.add(newListObject3);
    all.add(newListObject4);
    all.add(newListObject5);


    return all;

}

}

1 Comment

Thanks for the source, but I'm having difficulty getting it to compile. Could you generalize your source and include your xml file(s) as well? ... If I get to it first, I'll post a more general solution.
0

Creating a model as an inner class always works well.
Good way to store any number of items.

public class ActivityClass extends Activity {
    ...
    ArrayList<ValuesModel> listViewValues = new ArrayList<ValuesModel>();
    listViewValues.add(new ValuesModel("row title", "row details"));

    ListViewAdapter listAdapter = new ListViewAdapter(this, listViewValues);
    ((ListView) findViewById(android.R.id.list)).setAdapter(listAdapter);
    ...

    public class ValuesModel {
        private String rowTitle;
        private String rowDetails;

        public ValuesModel(String rowTitle, String rowDetails) {
            this.rowTitle = rowTitle;
            this.rowDetails = rowDetails;
        }
        public String getRowTitle() {
            return rowTitle;
        }
        public String getRowDetails() {
            return rowDetails();
        }
}

Then inside of your list adapter,

public class ListViewAdapter extends ArrayAdapter<ActivityClass.ValuesModel> {
    private ArrayList<ActivityClass.ValuesModel> mValues;
    ...

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ...
        //here whenever you need to retrieve your values, just say:  
        //  mValues.get(position).getRowTitle();
        //  mValues.get(position).getRowDetails();
        //if you use a viewholder pattern, you can do this:
        viewHolder.rowTitle = (TextView) convertView.findViewById(R.id.row_title_textview);  
        viewHolder.rowTitle.setText(mValues.get(position).getRowTitle());
        ...
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.