0

I have two TextViews tvA and tvB which appear in a ListView lvA. tvA and tvB display data pulled from a Sqlite Database using Cursor.

Cursor cursor = myDb.getAllRows();

    String[] from = new String[]
                    {DBAdapter.KEY_A,DBAdapter.KEY_B};
            int[] to = new int[]
                    {R.id.tvA,R.id.tvB};

    SimpleCursorAdapter myCursorAdapter =new SimpleCursorAdapter(this,R.layout.lvA, cursor,from,to);
 myList = (ListView) findViewById(R.id.lvA);
        myList.setAdapter(myCursorAdapter);
        registerForContextMenu(myList);

The issue is that tvA and tvB doesn't appear as one item in lvA (i.e. they don't appear on the same line) and appear as two different items.

Below is the XML:

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:padding="5dip"
    >

    <TextView
        android:id="@+id/tvA"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center_vertical"/>


    <TextView
        android:id="@+id/tvB"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:layout_toRightOf="@id/tvA"/>

</RelativeLayout>
gambit
  • 107
  • 10

4 Answers4

0

If you want both the TextViews in the same align then you can use LinearLayout with android:layout_weight attribute.

To have equal width TextView, give same weight value.

Community
  • 1
  • 1
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
0

TRY this :

Main Java:

  //DECLARE:

  private Map<String, Map<String, String>> mMenuView = new HashMap<String, Map<String, String>>();

//IN onCreateView

        Adapter mAdapter = new NewAdapter(getActivity().getApplicationContext(), mMenuView);
        mListView.setAdapter(mAdapter);

// ADD onActivityCreated or onStart save data from your DB and store in mMenuView

      Map<String, String> tempMap = new HashMap<String, String>();
      tempMap.put("TEXTVALE_A", datafromdb_A);
      tempMap.put("TEXTVALE_B", datafromdb_B);

      mMenuView.put(Integer.toString(mMenuView.size()), tempMap);

      }

//NEWADAPTER

private class NewAdapter extends BaseAdapter {

    private Context mContext;
    private TextView mTitle1;
    private TextView mTitle2;

    private Map<String, Map<String, String>> mData = new HashMap<String, Map<String, String>>();


    public NewAdapter(Context applicationContext,Map<String, Map<String, String>> mMenuValue) {


        mContext = applicationContext;
        mData =  mMenuValue;

    }

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

    }

    @Override
    public Map<String, String> getItem(int position) {
        return mData.get(Integer.toString(position));
    }


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


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

        final Map<String, String> data = mData.get(Integer.toString(position));
        if (convertView == null) {

            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(mContext.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.linear_layout, null);

            mTitle1 = (TextView) convertView.findViewById(R.id.t1A);
            mTitle2 = (TextView) convertView.findViewById(R.id.t1B);

        }

        if (data != null) {

            mTitle1.setText(data.get("TEXTVALE_A"));
            mTitle2.setText(data.get("TEXTVALE_B"));
        }
        return convertView;

   }

}

linear_layout.xml

  <LinearLayout
            android:id="@+id/content_menu"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:baselineAligned="false">

            <LinearLayout
                android:id="@+id/tab1_wrapper"
                android:layout_width="0dip"
                android:layout_height="50dp"
                android:layout_weight="1"
                android:orientation="vertical"
                android:layout_gravity="center|center_horizontal"
                >

                    <TextView
                        android:id="@+id/t1A"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerVertical="true"
                        android:textSize="13sp"
                        android:gravity="center|center_horizontal"
                        android:textColor="@color/text_color3x"/>

            </LinearLayout>


            <LinearLayout
                android:id="@+id/tab2_wrapper"
                android:layout_width="0dip"
                android:layout_height="50dp"
                android:layout_weight="0.5"
                android:orientation="vertical"
                android:gravity="left|center_vertical|center_horizontal"
                >


                 <TextView
                        android:id="@+id/A"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerVertical="true"
                        android:textSize="13sp"
                        android:gravity="center|center_horizontal"
                        />


            </LinearLayout>

          </LinearLayout>
sherin
  • 1,091
  • 2
  • 17
  • 27
0

You can use TableLayout

<TableLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >
                    <TableRow
                    android:id="@+id/tableRow1"
                    android:layout_height="wrap_content"
                    android:layout_width="match_parent">            
                    <TextView android:id="@+id/tvA"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1" >   
                    </TextView>    
                    <TextView android:id="@+id/tvB"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1" >
                    </TextView> 
                    </TableRow></TableLayout >

or LinearLayout with android:orientation="horizontal" and TextView with android:layout_weight="1"

Samot Kalop
  • 35
  • 2
  • 12
0

Here's how I do it with three items on the same row = This is my item list filled from a cursor

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"  
   android:id="@+id/RelativeLayout01" 
   android:layout_width="wrap_content" 
   android:layout_height="wrap_content">
 <TextView
    android:id="@+id/TextView_StudentName"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="12sp" 
    android:maxWidth="75dp" 
    android:layout_gravity="left"
    android:text="@string/student_id" 
    android:padding="5dp"></TextView>
<TextView
    android:id="@+id/TextView_StudentLocation"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="12sp" 
    android:maxWidth="75dp" 
    android:gravity="center"
    android:text="@string/student_Location" 
    android:padding="5dp"></TextView>
<TextView
    android:id="@+id/TextView_StudentClass"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="12sp" 
    android:maxWidth="75dp" 
    android:layout_alignParentRight="true"
    android:text="@string/student_class" >
 </TextView>
</RelativeLayout>`

I'm in the process of replacing the hard sizes with info from the dimen file, but so far this has worked.

Hank Wilson
  • 509
  • 12
  • 31