Chico3001
Chico3001

Reputation: 1963

Find item clicked on multicolumn ListView

I'm making a two column list view according to this tutorial, its working fine but besides the row clicked i also need to find which column was clicked (column 1 or 2), is it possible to achieve this?

Here is my Main Activity:

public class MainActivity extends AppCompatActivity {

    private ArrayList<HashMap<String, String>> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        final ListView listView=(ListView)findViewById(R.id.hotelsList);

        populateList();

        ListViewAdapter adapter=new ListViewAdapter(this, list);
        listView.setAdapter(adapter);


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, final View view, int position, long id)
            {
                // TODO find which column was clicked
            }

        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void populateList(){
        list=new ArrayList<HashMap<String,String>>();

        HashMap<String,String> temp=new HashMap<String, String>();
        temp.put("Hotel_Name_1", "Hotel 1");
        temp.put("Hotel_Description_1", "Descripcion Hotel 1");
        temp.put("Hotel_Name_2", "Hotel 2");
        temp.put("Hotel_Description_2", "Descripcion Hotel 2");
        list.add(temp);

        HashMap<String,String> temp2=new HashMap<String, String>();
        temp2.put("Hotel_Name_1", "Hotel 3");
        temp2.put("Hotel_Description_1", "Descripcion Hotel 3");
        temp2.put("Hotel_Name_2", "Hotel 4");
        temp2.put("Hotel_Description_2", "Descripcion Hotel 4");
        list.add(temp2);

        HashMap<String,String> temp3=new HashMap<String, String>();
        temp3.put("Hotel_Name_1", "Hotel 5");
        temp3.put("Hotel_Description_1", "Descripcion Hotel 5");

        temp3.put("Hotel_Name_2", "Hotel 6");
        temp3.put("Hotel_Description_2", "Descripcion Hotel 6");
        list.add(temp3);
    }
}

List Item XML:

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

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:layout_marginLeft="@dimen/image_margin_small"
        android:layout_marginRight="@dimen/image_margin_small_half"
        android:layout_marginTop="@dimen/image_margin_small"
        android:layout_marginBottom="@dimen/image_margin_small"
        android:id="@+id/item1">

        <TextView
            android:id="@+id/hotelName1"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:text="Hotel 1"
            android:textAppearance="@android:style/TextAppearance.Large" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/hotel"
            android:id="@+id/hotelImage1"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true" />

        <TextView
            android:text="TextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/hotelDescription1"
            android:layout_weight="1" />

    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:layout_marginLeft="@dimen/image_margin_small_half"
        android:layout_marginRight="@dimen/image_margin_small"
        android:layout_marginTop="@dimen/image_margin_small"
        android:layout_marginBottom="@dimen/image_margin_small"
        android:id="@+id/item2">

        <TextView
            android:id="@+id/hotelName2"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:text="Hotel 2"
            android:textAppearance="@android:style/TextAppearance.Large" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/hotel"
            android:id="@+id/hotelImage2"
            android:scaleType="fitCenter"
            android:adjustViewBounds="true" />

        <TextView
            android:text="TextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/hotelDescription2"
            android:layout_weight="1" />
    </LinearLayout>

</LinearLayout>

Activity Main XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.miempresaenlanube.hoteles360.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />

</android.support.design.widget.CoordinatorLayout>

content_main XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/content_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.miempresaenlanube.hoteles360.MainActivity"
    tools:showIn="@layout/activity_main">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/hotelsList" />

</RelativeLayout>

Upvotes: 0

Views: 881

Answers (4)

Guru raj
Guru raj

Reputation: 826

Modify this in your code

  listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> parent, final View view, int position, long id)
        {
            HashMap<String,String> selectedItem=list.get(position);//you will get the clicked item here you can use further as you want

        }

    });

Upvotes: 0

Manohar
Manohar

Reputation: 23394

Yes it it possible to do it from adapter here is an example of mine

in getview method of adapter ,set onClickListner there like below

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

    convertView = null;

    ViewHolderCategory holder;
    holder = new ViewHolderCategory();

    if (convertView == null) {

        convertView = mInflater.inflate(R.layout.item_category_list, null);

        holder.tv = (TextView) convertView.findViewById(R.id.textView1);
        holder.tv.setText(mTotalCategoriesItems.get(position)
                .getCategoryTitle());

        convertView.setTag(holder);
    }

 holder.tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
    //Do Something Here
}
 });

 //Similary keep on click listner for other views inside listView here
    return convertView;
}

Upvotes: 0

Noorul
Noorul

Reputation: 3444

Based on the link you provided, it's just customized listview with multiple textview's.

There is no complexity on that. Just set the tag to that textview.

Like, tv0.setTag("0"),tv1.setTag("1),tv2.setTag("2"),tv3.setTag("3"). and initialize onClikListener to all textview.

When the user click's you will get the clicked view. like

 tv0.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
    TextView tv=(TextView)v;
    String pos=(String)tv.getTag();

    //you can do like below also.
    //String pos=(String)v.getTag();


  }
 });

Upvotes: 0

Tahmid Rahman
Tahmid Rahman

Reputation: 758

Use a GridView instead and then you can easily work with gridView.setOnItemClickListener() method. Cheers!

Upvotes: 1

Related Questions