HardCoder
HardCoder

Reputation: 3036

How to show more than one text item in an Android/Java listview?

I have a listview with a checkbox, an image and a text field. Now I do want to display more than one items in the text field. The problem is that I want the arguments in neat colums. Can I somehow set tabstops or a format or something like that in the text field or do I have to actually make more text fields and set each value in its own field ?

My list should look somewhat like:

[Image]  [checkbox]   "arg11    arg12    arg13"
[Image]  [checkbox]   "arg21    arg22    arg23"
[Image]  [checkbox]   "arg31    arg32    arg33"

Thanks for any advice.

Edit: Just to clarify things. I have a working listview/adapter with the image/checkbox and one textfield already. I am kind of new to Android so all I want to know if there is an elegant way to handle displaying several text items in the text field. In Windows I would simply use tabstopps and they would look ordered and in a column but I don't know my way well enough around android to know if there is something similar or if individual text fields are required to get the text fields in columns.

Upvotes: 1

Views: 2258

Answers (5)

Raghu Nagaraju
Raghu Nagaraju

Reputation: 3288

You create new xml say row.xml, which we can inflate inside listview for each row.

Design the row however you want, for ex:

row.xml

<LinearLayout android:layout_height="wrap_content"
     android:layout_width="fill_parent"
      android:orientation="horizontal">  
    <ImageView ..../>
    <CheckBox ...../>
    <TextView ...../>
    <TextView ...../>
 </LinearLayout>

Then use CustomAdapter for your listview

like,

listView.setAdapter(new CustomAdapter());

CustomAdapter,

 class CustomAdapter extends BaseAdapter{
  publicCustomAdapter() {
        mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }



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

    @Override
    public String getItem(int position) {
        return xyz.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.row, null);
            holder = new ViewHolder();
       holder.imageView = (ImageViewView)convertView.findViewById(R.id.image);
       holder.checkbox = (CheckBox)convertView.findViewById(R.id.checkbox);
            holder.textView1 = (TextView)convertView.findViewById(R.id.text1);
            holder.textView2 = (TextView)convertView.findViewById(R.id.text2);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }
        //TODO set the values for views and return view
        return convertView;
    }

}

Upvotes: 3

Shubhayu
Shubhayu

Reputation: 13552

If you are sure about the sizes of text then you can use tabstops in your text. If that doesnt work then your are stuck with extending your row layout. as mentioned by Pavandroid

Upvotes: 0

Pavandroid
Pavandroid

Reputation: 1586

You should Re-work on the row xml. I think you may know about the row xml where you can chage the design of each and every row. Take a Relative or Linear Layout and add the views as shown below.

[Image]  [checkbox]   "text1"   "text2   "text3"
[Image]  [checkbox]   "text1"   "text2   "text3"
[Image]  [checkbox]   "text1"   "text2   "text3"

Instead of taking tab spaces, take three different TextView's. keep '1' as weight for each and every TextView and add these to a LinearLayout which is having total weight sum as 3.So all the three TextView will be equally placed in the parent view.

Try this and lemme know whether you fixed it.....

Upvotes: 2

jtt
jtt

Reputation: 13541

From one of my earlier posts:

Whenever you want to do processing with the views in a ListView you need to create a custom adapter that will handle your logic implementation and pass that information to the views as necessary.

Upvotes: 0

Serdar Dogruyol
Serdar Dogruyol

Reputation: 5157

Basically you need to implement your own adapter for your ListView.

First you need to create your own adapter class which either baseAdapter or their derivatives.And then you need to implement your ListView item display logic in it.

Like setting your textViews,imageViews etc.

Check this tutorial for more info.

http://www.vogella.de/articles/AndroidListView/article.html

Upvotes: 1

Related Questions