Note that there are some explanatory texts on larger screens.

plurals
  1. POOnClickListner() for Button in ListActivity header is not working
    primarykey
    data
    text
    <p>In my application, i have a layout which displays bus-stop details in the header of ListActivity while displays all available arrival predictions as a list. List header contains two buttons, first for saving bus-stop to database and second for refreshing the activity. But OnClickListner() is not working. I have tested my code without onClickListner() and it works fine.<br> Remember! in my code, buttons are part of fixed list header I have tried the suggestion already discussed here but those doesn't worked for me. Help me Please... My code is as follows: </p> <pre><code>public class MainBusStopByIdActivity extends ListActivity implements OnClickListener{ private final String TAG = getClass().getSimpleName(); ReadingBusStopByIdData readingData = new ReadingBusStopByIdData(); LinkedList&lt;BusStop&gt; stopList = new LinkedList&lt;BusStop&gt;(); LinkedList&lt;Predictions&gt; predictionsList = new LinkedList&lt;Predictions&gt;(); private AdapterForBusStopById mAdapter; String userInput; Button saveButton; Button refreshButton; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Initialising buttons saveButton = (Button) findViewById(R.id.save_button); refreshButton = (Button) findViewById(R.id.refresh_button); // Creating new adapter instance mAdapter = new AdapterForBusStopById(); // Retrieving data (user Input) sent from previous page Intent newIntent = getIntent(); userInput = newIntent.getStringExtra("input"); // Retrieving data on background thread new loadBusStopDetails().execute(userInput); new loadBusPredictions().execute(userInput); // Setting up onClickListner() for both buttons refreshButton.setOnClickListener(this); saveButton.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.save_button: boolean didItWork = true; try { if (stopList.size() != 0) { String stopId = null; String stopName = null; for (BusStop stop : stopList) { stopId = stop.getStopId(); stopName = stop.getStopName().toString(); } Toast.makeText(MainBusStopByIdActivity.this, stopId + "\n" + stopName, Toast.LENGTH_LONG).show(); DatabaseAccess entry = new DatabaseAccess( MainBusStopByIdActivity.this); entry.open(); entry.createEntry(stopId, stopName); entry.close(); } else { Toast.makeText(MainBusStopByIdActivity.this, "No Bus-stop Information Found!!!", Toast.LENGTH_LONG).show(); } } catch (Exception e) { didItWork = false; String error = e.toString(); Dialog d = new Dialog(this); d.setTitle("Error!!!"); TextView tv = new TextView(this); tv.setText(error); d.setContentView(tv); d.show(); } finally { if (didItWork) { Dialog d = new Dialog(this); d.setTitle("Entry to Favourites"); TextView tv = new TextView(this); tv.setText("Scuccessfully Added Bus-Stop to Favourites..."); d.setContentView(tv); d.show(); } } break; case R.id.refresh_button: break; } } // Adapter Class private class AdapterForBusStopById extends BaseAdapter { private static final int TYPE_PREDICTION = 0; private static final int TYPE_BUSSTOP_HEADER = 1; private ArrayList&lt;String&gt; mData = new ArrayList&lt;String&gt;(); private LayoutInflater mInflater; private TreeSet&lt;Integer&gt; mSeparatorsSet = new TreeSet&lt;Integer&gt;(); public AdapterForBusStopById() { mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); } public void addItem(final String item) { mData.add(item); notifyDataSetChanged(); } public void addSeparatorItem(final String item) { mData.add(item); // save separator position mSeparatorsSet.add(mData.size() - 1); notifyDataSetChanged(); } @Override public int getItemViewType(int position) { return mSeparatorsSet.contains(position) ? TYPE_BUSSTOP_HEADER : TYPE_PREDICTION; } public int getCount() { return mData.size(); } public String getItem(int position) { return mData.get(position); } public long getItemId(int position) { return position; } public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; int type = getItemViewType(position); System.out.println("getView " + position + " " + convertView + " type = " + type); if (convertView == null) { holder = new ViewHolder(); switch (type) { case TYPE_PREDICTION: convertView = mInflater.inflate(R.layout.predictions, null); holder.textView = (TextView) convertView .findViewById(R.id.predictionText); break; case TYPE_BUSSTOP_HEADER: convertView = mInflater.inflate(R.layout.bus_stop_header, null); holder.textView = (TextView) convertView .findViewById(R.id.textSeparator); break; } convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.textView.setText(mData.get(position)); return convertView; } } public static class ViewHolder { public TextView textView; } // Downloading Bus-stop Information // AsyncTask Class for downloading Bus-Stop data in Background public class loadBusStopDetails extends AsyncTask&lt;String, Integer, LinkedList&lt;BusStop&gt;&gt; { @Override protected LinkedList&lt;BusStop&gt; doInBackground(String... params) { // TODO Auto-generated method stub stopList = readingData.readStopDetailsByBusStopId(userInput); // Retrieving &amp; Showing bus-stop data for (BusStop stop : stopList) { String stopData = " NAME: " + stop.getStopName().toString() + " (" + stop.getStopPointer().toString() + ")\n STOP ID: " + stop.getStopId().toString() + "\n STOP STATUS: " + stop.getStopStatus().toString() + "\n TOWARDS: " + stop.getTowards().toString(); Log.d(TAG, "::::::::::::::::::::DATA:::::::::::::::::::\n" + "&lt;&lt;&lt;&lt;&lt;&lt;" + stopData + "&gt;&gt;&gt;&gt;&gt;&gt;\n"); mAdapter.addSeparatorItem(stopData); } Log.d(TAG, "::::::::::::::::::::DATA:::::::::::::::::::\n" + "&lt;&lt;&lt;&lt;&lt;&lt;" + stopList.size() + "&gt;&gt;&gt;&gt;&gt;&gt;\n"); return stopList; } protected void onPostExecute(LinkedList&lt;BusStop&gt; result) { stopList = result; } } // Downloading Bus-predictions Information // AsyncTask Class for downloading Bus-Predictions data in Background public class loadBusPredictions extends AsyncTask&lt;String, Integer, LinkedList&lt;Predictions&gt;&gt; { ProgressDialog dialog; protected void onPreExecute() { dialog = new ProgressDialog(MainBusStopByIdActivity.this); dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); dialog.setMax(100); dialog.show(); } @Override protected LinkedList&lt;Predictions&gt; doInBackground(String... params) { // TODO Auto-generated method stub for (int i = 0; i &lt; 20; i++) { publishProgress(4); try { Thread.sleep(200); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } dialog.dismiss(); // Retrieving &amp; Showing bus-prediction data predictionsList = readingData .readStopPredictionsByBusStopId(userInput); for (Predictions prediction : predictionsList) { int time = prediction.getTime(); String stringTime; if (time == 0) { stringTime = "DUE"; } else { stringTime = String.valueOf(time); } String predictionData = " " + prediction.getRoute().toString() + " " + stringTime + " " + prediction.getDestination().toString() + " \n "; mAdapter.addItem(predictionData); } return predictionsList; } protected void onProgressUpdate(Integer... progress) { dialog.incrementProgressBy(progress[0]); } protected void onPostExecute(LinkedList&lt;Predictions&gt; result) { predictionsList = result; setListAdapter(mAdapter); // Displaying related messages if there's something wrong if (stopList.size() != 0) { if (predictionsList.size() != 0) { } else { Toast.makeText( MainBusStopByIdActivity.this, "There are no Buses to this Bus Stop in Next 30 Minutes.", Toast.LENGTH_LONG).show(); } } else { Toast.makeText( MainBusStopByIdActivity.this, "There is 'NO' Bus Stop Exist with " + userInput + " Bus-Stop ID.", Toast.LENGTH_LONG).show(); } } } } </code></pre> <p>My XML code is given below:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:descendantFocusability="blocksDescendants" &gt; &lt;View android:layout_width="match_parent" android:layout_height="1dp" android:background="@android:color/holo_blue_light" /&gt; &lt;LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" &gt; &lt;ImageView android:id="@+id/tflImage" android:layout_width="50dp" android:layout_height="60dp" android:layout_gravity="center_vertical" android:layout_margin="2.5dp" android:background="@drawable/redtrain128" android:contentDescription="@string/hello_world" /&gt; &lt;TextView android:id="@+id/textSeparator" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#000" android:gravity="left" android:text="text" android:textColor="#FFFFFFFF" android:visibility="visible" /&gt; &lt;/LinearLayout&gt; &lt;View android:layout_width="match_parent" android:layout_height="1dp" android:background="@android:color/holo_blue_light" /&gt; &lt;LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" &gt; &lt;Button android:id="@+id/save_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Add to Favourites" android:focusable="false" android:focusableInTouchMode="false" android:onClick="onClick" /&gt; &lt;Button android:id="@+id/refresh_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Refresh" android:focusable="false" android:focusableInTouchMode="false" android:onClick="onClick" /&gt; &lt;/LinearLayout&gt; &lt;View android:layout_width="match_parent" android:layout_height="1dp" android:background="@android:color/holo_blue_light" /&gt; &lt;TextView android:id="@+id/textSeparator" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#FF0000" android:gravity="left" android:text=" ROUTE TIME DESTINATION" android:textColor="#FFFFFFFF" android:textSize="8pt" android:textStyle="bold" android:typeface="normal" android:visibility="visible" /&gt; &lt;View android:layout_width="match_parent" android:layout_height="1dp" android:background="@android:color/holo_blue_light" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>I have also found following link about this issue but i didn't get how to implement in my case. please have a look, may be you get any idea, link is here: <a href="https://code.google.com/p/android/issues/detail?id=3414" rel="nofollow">for link clik here</a> </p> <p>Thank you...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload