1

I'm working on an Android project that has quite a few screens with upwards of 200 rows that will be clickable per screen. The issue I am trying to figure out is how to make them all able to be clicked without adding 200 lines of

TableRow r1 = (TableRow) findViewById(R.id.table_row_1);
TableRow r2 = (TableRow) findViewById(R.id.table_row_2);
TableRow r3 = (TableRow) findViewById(R.id.table_row_3);
TableRow r4 = (TableRow) findViewById(R.id.table_row_4);

r1.setOnClickListener(listener);
r2.setOnClickListener(listener);
r3.setOnClickListener(listener);
r4.setOnClickListener(listener);

Eventually the rows will take their id's and search the database for the value (I'm going to use each table row as a key for a value in the database to populate a column in the row) but for now I'm just trying to change the background color of the row when each one is clicked.

Questions: How can I handle a large number of clickable rows without thousands of lines of redundant code? Do I need to set an OnClickListener for each row or is there a better method that I am over looking? Is there a way to do it in the XML maybe?

ArtKorchagin
  • 4,801
  • 13
  • 42
  • 58
Dommol
  • 191
  • 1
  • 17
  • Are you limited to using `TableRow`, or is it possible that you use a `ListView` to show your database content? – Marcus Jan 23 '15 at 23:45
  • I started using `TableRow` and haven't finalized the design yet. I hadn't heard of `ListView` yet. Does it have a more elegant way of handling large numbers of clickables? – Dommol Jan 23 '15 at 23:48
  • I suggest you have a look at http://developer.android.com/guide/topics/ui/layout/listview.html and also http://stackoverflow.com/questions/8166497/custom-adapter-for-list-view. In short, using a `ListView` allows you to fetch values from a database and display them in a scrollable view. Then for your `ListView` you can use the callback method `setOnItemClickListener()` which will trigger when you click on an item in the list. – Marcus Jan 23 '15 at 23:53
  • I can write you a quick demo if you'd like – Marcus Jan 23 '15 at 23:57
  • That would be great, thanks. – Dommol Jan 23 '15 at 23:58
  • Was my answer helpful at any level? – Marcus Jan 24 '15 at 10:04
  • Yes very helpful thanks a lot, `ListView` is the best way to go – Dommol Jan 24 '15 at 13:55
  • You are always welcome :-) – Marcus Jan 24 '15 at 14:31
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/69509/discussion-between-dommol-and-marcus). – Dommol Jan 24 '15 at 14:36

1 Answers1

2

Solution using a ListView with a custom ListAdapter

MainActivity.java:

public class MainActivity extends ListActivity implements AdapterView.OnItemClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setUpComponents();
    }

    private void setUpComponents(){
        ArrayList<String> myValuesToDisplay = getDatabaseContent();
        MyCustomListAdapter adapter = new MyCustomListAdapter(this, myValuesToDisplay);
        setListAdapter(adapter);
        getListView().setOnItemClickListener(this);
    }

    private ArrayList<String> getDatabaseContent(){
        /*
        This is where you would like to connect to your database and fetch the content.
        In this example, we simulate the result by returning an ArrayList<String>
         */

        ArrayList<String> values = new ArrayList<String>();
        values.add("Value1");
        values.add("Value2");
        values.add("Value3");
        return values;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //When you click on an item in the list view, you fetch the position in the list
        System.out.println("Clicked on item with position: " + position);
    }

}

MyCustomListAdapter.java:

public class MyCustomListAdapter extends ArrayAdapter<String> {

    private ArrayList<String> yourArray;

    public MyCustomListAdapter(Context ctx, ArrayList<String> yourArray){
        super(ctx, R.layout.my_custom_list_item, yourArray);
        this.yourArray = yourArray;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //Re-use rows to save battery
        View row;
        if (convertView == null) {
            //We inflate our custom view for the ListView item
            LayoutInflater inflater = LayoutInflater.from(getContext());
            row = inflater.inflate(
                    R.layout.my_custom_list_item, null);
        } else {
            row = convertView;
        }

        //Get the current item of the array
        String arrayItem = yourArray.get(position);
        //Get the text view in the layout of which we want to display the value
        TextView tvListItem = (TextView) row.findViewById(R.id.tv_list_item);
        //Set the text
        tvListItem.setText(arrayItem);
        //Return the row to the ListView
        return row;
    }
}

ActivityMain.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <ListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@android:id/list" />
</RelativeLayout>

my_custom_list_item.xml:

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/tv_list_item" />
</LinearLayout>

This solution will create a scrollable ListView and populate it with your database values. Your implementation of the ListAdapter can vary. You can choose what and how you would like to display by changing the layout in my_custom_list_item.xml.

The result:

Result

Clicking on a row will print out its position in the list. You can for example use that information to start another activity displaying detailed information about that entry.

Community
  • 1
  • 1
Marcus
  • 6,697
  • 11
  • 46
  • 89