title: automatically select first item in list view, and do some other things being called in OnItemClickListener() without having to click
body: <p>total noob to android and java here.</p>
  
  <p>I found some code on github for connecting a USB GPS device with a u-blox chip. it is here: <a href="https://github.com/mikey0000/Ublox-GPS-receiver-android" rel="nofollow">https://github.com/mikey0000/Ublox-GPS-receiver-android</a>   (It took me about 2 days to figure out how to compile this, so yes I am that much of a noob).</p>
  
  <p>I have the device connected in a car android unit, and I'd like it to automatically connect when it boots.</p>
  
  <p>I would like help with automatically selecting the first item in the ListView. I believe the code where this might be do-able is in DeviceListActivity.java:</p>
  
  <pre><code>package com.hoho.android.usbserial.examples;
  
  import android.app.Activity;
  import android.app.PendingIntent;
  import android.content.Context;
  import android.content.Intent;
  import android.hardware.usb.UsbDevice;
  import android.hardware.usb.UsbManager;
  import android.os.AsyncTask;
  import android.os.Bundle;
  import android.os.Handler;
  import android.os.Message;
  import android.os.SystemClock;
  import android.util.Log;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.AdapterView;
  import android.widget.ArrayAdapter;
  import android.widget.ListView;
  import android.widget.ProgressBar;
  import android.widget.TextView;
  import android.widget.Toast;
  import android.widget.TwoLineListItem;
  
  import com.hoho.android.usbserial.driver.CdcAcmSerialDriver;
  import com.hoho.android.usbserial.driver.ProbeTable;
  import com.hoho.android.usbserial.driver.UsbSerialDriver;
  import com.hoho.android.usbserial.driver.UsbSerialPort;
  import com.hoho.android.usbserial.driver.UsbSerialProber;
  import com.hoho.android.usbserial.util.HexDump;
  
  import java.util.ArrayList;
  import java.util.List;
  
  import src.com.hoho.android.usbserial.examples.BackgroundService;
  
  /**
   * Shows a {@link ListView} of available USB devices.
   *
   * @author mike wakerly (opensource@hoho.com)
   */
  public class DeviceListActivity extends Activity {
  
      private final String TAG = DeviceListActivity.class.getSimpleName();
  
      private UsbManager mUsbManager;
      private ListView mListView;
      private TextView mProgressBarTitle;
      private ProgressBar mProgressBar;
      private UsbSerialProber prober;
  
      private static final int MESSAGE_REFRESH = 101;
      private static final long REFRESH_TIMEOUT_MILLIS = 5000;
  
      private final Handler mHandler = new Handler() {
          @Override
          public void handleMessage(Message msg) {
              switch (msg.what) {
                  case MESSAGE_REFRESH:
                      refreshDeviceList();
                      //mHandler.sendEmptyMessageDelayed(MESSAGE_REFRESH, REFRESH_TIMEOUT_MILLIS);
                      break;
                  default:
                      super.handleMessage(msg);
                      break;
              }
          }
  
      };
  
      private List&lt;UsbSerialPort&gt; mEntries = new ArrayList&lt;UsbSerialPort&gt;();
      private ArrayAdapter&lt;UsbSerialPort&gt; mAdapter;
  
      static class ViewHolder {
  
          TextView textViewItem;
          TextView textViewItem2;
      }
  
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
  
          mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
          mListView = (ListView) findViewById(R.id.deviceList);
          mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
          mProgressBarTitle = (TextView) findViewById(R.id.progressBarTitle);
  
          ProbeTable customTable = new ProbeTable();
          customTable.addProduct(0x1546, 0x01a7, CdcAcmSerialDriver.class);
          prober = new UsbSerialProber(customTable);
  
          mAdapter = new ArrayAdapter&lt;UsbSerialPort&gt;(this,
                  android.R.layout.simple_expandable_list_item_2, mEntries) {
              @Override
              public View getView(int position, View convertView, ViewGroup parent) {
                  ViewHolder holder;
                  if (convertView == null){
                      LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                      convertView = inflater.inflate(R.layout.two_list_item, null);
  
                      holder = new ViewHolder();
                      holder.textViewItem = (TextView) convertView.findViewById(R.id.text1);
                      holder.textViewItem2 = (TextView) convertView.findViewById(R.id.text2);
                      convertView.setTag(holder);
                  } else {
                      holder = (ViewHolder) convertView.getTag();
                  }
  
                  final UsbSerialPort port = mEntries.get(position);
                  final UsbSerialDriver driver = port.getDriver();
                  final UsbDevice device = driver.getDevice();
  
                  final String title = String.format("Vendor %s Product %s",
                          HexDump.toHexString((short) device.getVendorId()),
                          HexDump.toHexString((short) device.getProductId()));
                  holder.textViewItem.setText(title);
  
                  final String subtitle = driver.getClass().getSimpleName();
                  holder.textViewItem2.setText(subtitle);
  
                  return convertView;
              }
  
          };
          mListView.setAdapter(mAdapter);
  
          mListView.setOnItemClickListener(new ListView.OnItemClickListener() {
              @Override
              public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) {
                  Log.d(TAG, "Pressed item " + position);
                  if (position &gt;= mEntries.size()) {
                      Log.w(TAG, "Illegal position.");
                      return;
                  }
  
                  final UsbSerialPort port = mEntries.get(position);
                  mHandler.removeMessages(MESSAGE_REFRESH);
                  showConsoleActivity(port);
              }
          });
      }
  
      @Override
      protected void onResume() {
          super.onResume();
          mHandler.sendEmptyMessage(MESSAGE_REFRESH);
      }
  
      @Override
      protected void onPause() {
          super.onPause();
          mHandler.removeMessages(MESSAGE_REFRESH);
      }
  
      private void refreshDeviceList() {
          showProgressBar();
  
          new AsyncTask&lt;Void, Void, List&lt;UsbSerialPort&gt;&gt;() {
              @Override
              protected List&lt;UsbSerialPort&gt; doInBackground(Void... params) {
                  Log.d(TAG, "Refreshing device list ...");
                  SystemClock.sleep(1000);
  
                  final List&lt;UsbSerialDriver&gt; drivers =
                          prober.findAllDrivers(mUsbManager);
  
                  final List&lt;UsbSerialPort&gt; result = new ArrayList&lt;UsbSerialPort&gt;();
                  for (final UsbSerialDriver driver : drivers) {
                      final List&lt;UsbSerialPort&gt; ports = driver.getPorts();
                      Log.d(TAG, String.format("+ %s: %s port%s",
                              driver, Integer.valueOf(ports.size()), ports.size() == 1 ? "" : "s"));
                      result.addAll(ports);
                  }
  
                  return result;
              }
  
              @Override
              protected void onPostExecute(List&lt;UsbSerialPort&gt; result) {
                  mEntries.clear();
                  mEntries.addAll(result);
                  mAdapter.notifyDataSetChanged();
                  mProgressBarTitle.setText(
                          String.format("%s device(s) found",Integer.valueOf(mEntries.size())));
                  hideProgressBar();
                  Log.d(TAG, "Done refreshing, " + mEntries.size() + " entries found.");
              }
  
          }.execute((Void) null);
      }
  
      private void showProgressBar() {
          mProgressBar.setVisibility(View.VISIBLE);
          mProgressBarTitle.setText(R.string.refreshing);
      }
  
      private void hideProgressBar() {
          mProgressBar.setVisibility(View.INVISIBLE);
      }
  
      private void showConsoleActivity(UsbSerialPort port) {
          Intent startIntent = new Intent(getApplicationContext(), BackgroundService.class);
  
          PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0, startIntent, PendingIntent.FLAG_CANCEL_CURRENT);
          mUsbManager.requestPermission(port.getDriver().getDevice(), pendingIntent);
          Toast.makeText(this, "Started Background Service for GPS", Toast.LENGTH_LONG).show();
          //SerialConsoleActivity.show(this, port);
      }
  
  }
  </code></pre>
  
  <p>Is anyone able to help? I haven't even the first clue where I could go about implementing this. TIA.</p>
  
  <p>EDIT: So it looks like</p>
  
  <pre><code>mListView.setSelection(0);
  </code></pre>
  
  <p>is the way to go however using </p>
  
  <pre><code>mListView.getSelectedView().setSelected(true);
  </code></pre>
  
  <p>as well seems to make it crash.</p>
  
  <p>Any idea how I can do the rest of what setOnItemClickListener is doing after automatically selecting the item in the list? </p>
  
  <p>Something like:</p>
  
  <pre><code>mListView.setSelection(0);
  final UsbSerialPort port = mEntries.get(0);
  mHandler.removeMessages(MESSAGE_REFRESH);
  showConsoleActivity(port);
  </code></pre>
  
  <p>?</p>
  
  <p>Apologies for the earlier ambiguity, but what I wanted this app to do was to do what it does already without me having to tap \ click on anything i.e. on boot automatically select the USB GPS chip that is plugged in so I'd like it not to use the OnItemClickListener or automate that part alternatively. I don't really want the app to sit there waiting for input since the car unit loads all sorts on boot and I keep missing the screen where it prompts as per current behaviour. Hope that's a bit clearer. Thanks for the help so far. Totally new to java and android as you can likely tell haha.</p>
  
  <p>Update: See my answer. Sorry I wasn't clear with my original question. Thanks to everyone for the replies. </p>