1

How can I place 2 items in the same row in listview?

e.g

item 1 | item 2

item 3 | item 4

item 5 | item 6

and so on. I don't have a problem with drawing them and there are a lot of various ways to draw them.

My problem: I cannot find a propper way to listen when I clicked on item 1 or item 2.

Also, I am considering to simply make 2 listview, populate them independently and rotate simultaneously, but I hope there is better way.

Gravitoid
  • 1,294
  • 1
  • 20
  • 20
Yarh
  • 4,459
  • 5
  • 45
  • 95

5 Answers5

1

You should use a custom adapter for your listView. In the adapter you inflate your own layout for each row and listen for events for each view. This is an example of a custom Adapter: Custom Adapter for List View

Hope it helps you!!

Community
  • 1
  • 1
0

for each item add this to listview row layout:

 android:focusable="false"

and then in getView find each item and then assign it the appropriate click listener.

mmlooloo
  • 18,937
  • 5
  • 45
  • 64
  • how to remove listeners after nitifydatasetChanged? – Yarh Aug 19 '14 at 13:06
  • i do not understood what you mean but you can create boolean variable and if you want to assign listener look at that and if for example it is true assign listener else assign nothing. after you change something call notifyDataSetChanged to again create your view and change the listeners. – mmlooloo Aug 19 '14 at 13:11
0

Use CustomAdapter for listview and add items in collection for example an ArrayList.

CustomAdapter adapter=new CustomAdapter(getApplicationContext(),R.id.listview_id,itemlist);

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
  >


 <TextView android:id="@+id/item1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="10dip"
    android:textSize="16sp"
    android:textStyle="bold"
    android:textColor="#000000"
    />     

  <TextView android:id="@+id/item2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="10dip"
    android:textSize="16sp"
    android:textStyle="bold"
    android:textColor="#000000"
    />  

</LinearLayout>

CustomAdapter.java

public class CustomAdapter extends CustomAdapter<String>{

//Declare variables variables here..

public CustomAdapter(Context context, int resource,
        List<String> objects) {
    //initialize and set constructor values here..
}

public static class ViewHolder {
    TextView textitem1;
    TextView textitem2

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View row = convertView;
    ViewHolder holder = null;
    if(null == row) {
        LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.list_item, parent, false);
        holder = new ViewHolder();
        holder.textitem1 = (TextView) row.findViewById(R.id.item1);
        holder.textitem2 = (TextView) row.findViewById(R.id.item2);

        row.setTag(holder);
    } 

    return view;
}
}
  • You can add as many items in list_item.xml. I have added two items(TextViews). – Prasad Mhapankar Aug 19 '14 at 12:55
  • @Yah: Use Getter Setter Method for example MyItems.java and then extend CustomAdapter with public class CustomAdapter extends CustomAdapter and USe List itemlist=new ArrayList(); then add elements to itemlist.. itemlist.add(newitem); – Prasad Mhapankar Aug 19 '14 at 12:58
0

You can add as many TextViews you want. Depending on requirement, you can alter their position/height/width etc.

For listening Click event, For each text view add android:clickable="true" Use a CustomAdapter and set ClickListeners for each TextView in getView method.

Hope This Helps!

srs
  • 647
  • 3
  • 11
0

And i found way wich is suitable for me for 100%
Idea is to use GridView~ instead ofListView`.

 <GridView
            android:id="@+id/gridview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:numColumns="2"
            android:stretchMode="columnWidth" />

In fact I dont even need to change and single line in my original adapter. android:numColums set how much colums you want to display. You can place int there or auoto_fit.

Yarh
  • 4,459
  • 5
  • 45
  • 95