Reputation: 33
So here is a summary of my problem. I'm creating a test app just to try to better my skills in android, and i created a particular activity that lists a bus schedule. So basically, the entire page in just displays a trip #, time and button for each trip. See image to get an idea of what it looks like;
i wanted the list to be designed in a particular way, so i used a custom design with a class that extends arrayadapter which has its own custom layout for the individual rows.
See code below; My goal is to be able to click the add button, to send the time listed in the corresponding row into an array to save for use in another activity. I just need help making clicking the button, lets say in row 1, save the TIME in row one to an array.
public class TripsActivity extends AppCompatActivity {
private Toolbar toolbar;
private String schedule, route;
private ListView listView_A, listView_B;
private String[] trips_A, trips_B, times_A, times_B;
private ArrayAdapter<String> trips_A_adapter, trips_B_adapter;
private TabHost tabhost;
private TextView route_textView;
Time[] date_formatted_times_A, date_formatted_times_B;
Time[] convertedTimes_A, convertedTimes_B;
private SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_trips);
//set actionbar to custom toolbar layout and enable it
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
//enable home (back) button at the top left of the actionbar
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Bundle extras = getIntent().getExtras();
if (extras != null) {
schedule = extras.getString("schedule");
route = extras.getString("route");
}
//Assign the view elements to variables
listView_A = (ListView) findViewById(R.id.listView_A);
listView_B = (ListView) findViewById(R.id.listView_B);
route_textView = (TextView) findViewById(R.id.route_TextView);
trips_A = getResources().getStringArray(R.array.weekday_bus_routes_array);
trips_B = getResources().getStringArray(R.array.weekend_bus_routes_array);
//trips_A_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, trips_A);
trips_B_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, trips_B);
//listView_A.setOnItemSelectedListener();
switch (schedule) {
//do if the Schedule selected previously is weekend
case "Weekday":
//Update the times_ and times_B variables with the A and B trips for the appropriate routes on the weekend
switch (route) {
case "--- Select Route ---":
times_A = getResources().getStringArray(R.array.WD_selectRoute_A);
times_B = getResources().getStringArray(R.array.WD_selectRoute_B);
route_textView.setText("Select Route First");
break;
case "Grand Anse":
times_A = getResources().getStringArray(R.array.WD_grandAnse_A);
times_B = getResources().getStringArray(R.array.WD_grandAnse_B);
route_textView.setText("Grand Anse");
date_formatted_times_A = TimeConverterA(times_A);
date_formatted_times_B = TimeConverterB(times_B);
break;
case "Grand View Inn":
times_A = getResources().getStringArray(R.array.WD_grandViewInn_A);
times_B = getResources().getStringArray(R.array.WD_grandViewInn_B);
route_textView.setText("Grand View Inn");
date_formatted_times_A = TimeConverterA(times_A);
date_formatted_times_B = TimeConverterB(times_B);
break;
case "L 'Anse Aux Epines":
times_A = getResources().getStringArray(R.array.WD_lAnseAuxEpines_A);
times_B = getResources().getStringArray(R.array.WD_lAnseAuxEpines_B);
route_textView.setText("L' Anse Aux Epines");
date_formatted_times_A = TimeConverterA(times_A);
date_formatted_times_B = TimeConverterB(times_B);
break;
case "Mont Tout":
times_A = getResources().getStringArray(R.array.WD_montTout_A);
times_B = getResources().getStringArray(R.array.WD_montTout_B);
route_textView.setText("Mont Tout");
date_formatted_times_A = TimeConverterA(times_A);
date_formatted_times_B = TimeConverterB(times_B);
break;
case "Point Salines / Frequente":
times_A = getResources().getStringArray(R.array.WD_pointSalinesFrequente_A);
times_B = getResources().getStringArray(R.array.WD_pointSalinesFrequente_B);
route_textView.setText("Point Salines / Frequente");
date_formatted_times_A = TimeConverterA(times_A);
date_formatted_times_B = TimeConverterB(times_B);
break;
case "True Blue Inn":
times_A = getResources().getStringArray(R.array.WD_trueBlueInn_A);
times_B = getResources().getStringArray(R.array.WD_trueBlueInn_B);
route_textView.setText("True Blue Inn");
date_formatted_times_A = TimeConverterA(times_A);
date_formatted_times_B = TimeConverterB(times_B);
break;
case "Campus Shuttle":
times_A = getResources().getStringArray(R.array.WD_campusShuttle_A);
times_B = getResources().getStringArray(R.array.WD_campusShuttle_B);
route_textView.setText("Campus Shuttle");
date_formatted_times_A = TimeConverterA(times_A);
date_formatted_times_B = TimeConverterB(times_B);
break;
case "Extra Night Bus":
times_A = getResources().getStringArray(R.array.WD_extraNightBus_A);
times_B = getResources().getStringArray(R.array.WD_extraNightBus_B);
route_textView.setText("Extra Night Bus");
date_formatted_times_A = TimeConverterA(times_A);
date_formatted_times_B = TimeConverterB(times_B);
break;
}
break;
case "Weekend":
//Update the times_A and times_B variables with the A and B trips for the appropriate routes on the weekend
switch (route) {
case "--- Select Route ---":
times_A = getResources().getStringArray(R.array.WE_selectRoute_A);
times_B = getResources().getStringArray(R.array.WE_selectRoute_B);
route_textView.setText("Select a Route First!");
break;
case "Grand Anse":
times_A = getResources().getStringArray(R.array.WE_grandAnse_A);
times_B = getResources().getStringArray(R.array.WE_grandAnse_B);
route_textView.setText("Grand Anse");
date_formatted_times_A = TimeConverterA(times_A);
date_formatted_times_B = TimeConverterB(times_B);
break;
case "Grand View Inn":
times_A = getResources().getStringArray(R.array.WE_grandViewInn_A);
times_B = getResources().getStringArray(R.array.WE_grandViewInn_B);
route_textView.setText("Grand View Inn");
date_formatted_times_A = TimeConverterA(times_A);
date_formatted_times_B = TimeConverterB(times_B);
break;
case "L 'Anse Aux Epines":
times_A = getResources().getStringArray(R.array.WE_lAnseAuxEpines_A);
times_B = getResources().getStringArray(R.array.WE_lAnseAuxEpines_B);
route_textView.setText("L 'Anse Aux Epines");
date_formatted_times_A = TimeConverterA(times_A);
date_formatted_times_B = TimeConverterB(times_B);
break;
case "Mont Tout":
times_A = getResources().getStringArray(R.array.WE_montTout_A);
times_B = getResources().getStringArray(R.array.WE_montTout_B);
route_textView.setText("Mont Tout");
date_formatted_times_A = TimeConverterA(times_A);
date_formatted_times_B = TimeConverterB(times_B);
break;
case "Point Salines / Frequente":
times_A = getResources().getStringArray(R.array.WE_pointSalinesFrequente_A);
times_B = getResources().getStringArray(R.array.WE_pointSalinesFrequente_B);
route_textView.setText("Point Salines / Frequente");
date_formatted_times_A = TimeConverterA(times_A);
date_formatted_times_B = TimeConverterB(times_B);
break;
case "Shuttle / True Blue Inn":
times_A = getResources().getStringArray(R.array.WE_trueBlueInnAndCampusShuttle_A);
times_B = getResources().getStringArray(R.array.WE_trueBlueInnAndCampusShuttle_B);
route_textView.setText("Campus Shuttle / True Blue Inn");
date_formatted_times_A = TimeConverterA(times_A);
date_formatted_times_B = TimeConverterB(times_B);
break;
}
break;
case "--- Select Schedule ---":
break;
}
/* ArrayAdapter<Date> date_adapter = new ArrayAdapter<Date>(this, android.R.layout.simple_dropdown_item_1line, date_formatted_times_A);
listView_A.setAdapter(date_adapter);*/
//Set the listviews on both tabs to the corresponding Custom Adapters
CustomListAdapterA customListAdapterA = new CustomListAdapterA(this, times_A);
listView_A.setAdapter(customListAdapterA);
CustomListAdapterB customListAdapterB = new CustomListAdapterB(this, times_B);
listView_B.setAdapter(customListAdapterB);
//Setup Tabs
tabhost = (TabHost) findViewById(R.id.tabHost);
tabhost.setup();
//Tab 1
TabHost.TabSpec tab1 = tabhost.newTabSpec("Tab One");
tab1.setContent(R.id.tab1);
tab1.setIndicator("From True Blue");
tabhost.addTab(tab1);
//Tab 2
TabHost.TabSpec tab2 = tabhost.newTabSpec("Tab Two");
tab2.setContent(R.id.tab2);
tab2.setIndicator("To True Blue");
tabhost.addTab(tab2);
}
public Time[] TimeConverterA(String[] times_A) {
convertedTimes_A = new Time[times_A.length];
for (int i = 0; i < times_A.length; i++) {
try {
Date d1 = (Date) formatter.parse(times_A[i]);
convertedTimes_A[i] = new Time(d1.getTime());
} catch (Exception e) {
Log.e("Exception is ", e.toString());
}
}
return convertedTimes_A;
}
public Time[] TimeConverterB(String[] times_B) {
convertedTimes_B = new Time[times_B.length];
for (int i = 0; i < times_B.length; i++) {
try {
Date d1 = (Date) formatter.parse(times_B[i]);
convertedTimes_B[i] = new Time(d1.getTime());
} catch (Exception e) {
Log.e("Exception is ", e.toString());
}
}
return convertedTimes_B;
}
}
class CustomListAdapterA extends ArrayAdapter<String> {
Context context;
String[] times_array;
private int row_position;
CustomListAdapterA(Context c, String[] times) {
super(c, R.layout.trips_listview_layout, R.id.times_textView, times);
this.context = c;
this.times_array = times;
}
@Override
public View getView(int position, View convertView, android.view.ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.trips_listview_layout, parent, false);
TextView numbers_textView = (TextView) row.findViewById(R.id.numbers_textView);
TextView times_textView = (TextView) row.findViewById(R.id.times_textView);
//Set text to the array position + 1
numbers_textView.setText(Integer.toString(position + 1));
times_textView.setText(times_array[position]);
return row;
}
}
class CustomListAdapterB extends ArrayAdapter<String> {
Context context;
String[] times_array;
CustomListAdapterB(Context c, String[] times )
{
super(c, R.layout.trips_listview_layout, R.id.times_textView, times);
this.context=c;
this.times_array=times;
}
@Override
public View getView(int position, View convertView, android.view.ViewGroup parent){
LayoutInflater inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.trips_listview_layout, parent, false);
TextView numbers_textView= (TextView) row.findViewById(R.id.numbers_textView);
TextView times_textView = (TextView) row.findViewById(R.id.times_textView);
//Set text to the array position + 1
numbers_textView.setText(Integer.toString(position+1));
times_textView.setText(times_array[position]);
return row;
}
}
My layout code for the rows are as follows;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="10sp"
android:paddingRight="10sp"
android:paddingTop="5sp"
android:paddingBottom="5sp">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/darker_gray"/>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="10">
<TextView
android:id="@+id/numbers_textView"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center"
android:text="#"
android:textSize="20dp"
android:textColor="#FFFFFF"/>
<TextView
android:id="@+id/times_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="6"
android:gravity="center"
android:text="Departure Time"
android:textSize="20dp"
android:textColor="#FFFFFF"/>
<Button
android:id="@+id/add_alert_button"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center"
android:text="+" />
</TableRow>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/darker_gray"/>
</LinearLayout>
And the layout for the activity itself is as follows;
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.chadedwards.skyeye2.TripsActivity"
tools:showIn="@layout/activity_trips"
android:orientation="vertical"
>
<include
android:id="@+id/app_bar"
layout="@layout/app_bar"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Route"
android:gravity="center"
android:textSize="20dp"
android:padding="10dp"
android:id="@+id/route_TextView"
android:textColor="#FFFFFF"
android:background="#000000"/>
<TabHost
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/tabHost"
android:layout_centerHorizontal="true"
android:visibility="visible">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"></TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="visible"
>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="10"
android:paddingLeft="10sp"
android:paddingRight="10sp"
android:paddingTop="5sp"
android:paddingBottom="5sp">
<TextView
android:id="@+id/numberTitle"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center"
android:text="#"
android:textSize="20dp"
android:textColor="#FFFFFF"/>
<TextView
android:id="@+id/departureTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="6"
android:gravity="center"
android:text="Departure Time"
android:textSize="20dp"
android:textColor="#FFFFFF"/>
<TextView
android:id="@+id/alertTitle"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center"
android:text="ALT"
android:textSize="20dp"
android:textColor="#FFFFFF"/>
</TableRow>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView_A"
android:textColor="#FFFFFF"/>
</LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="10"
android:paddingLeft="10sp"
android:paddingRight="10sp"
android:paddingTop="5sp"
android:paddingBottom="5sp">
<TextView
android:id="@+id/numberTitle2"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center"
android:text="#"
android:textSize="20dp"
android:textColor="#FFFFFF"/>
<TextView
android:id="@+id/departureTitle2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="6"
android:gravity="center"
android:text="Departure Time"
android:textSize="20dp"
android:textColor="#FFFFFF"/>
<TextView
android:id="@+id/alertTitle2"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center"
android:text="ALT"
android:textSize="20dp"
android:textColor="#FFFFFF"/>
</TableRow>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView_B" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
Upvotes: 1
Views: 38
Reputation: 57043
I find it easiest to add the onClick
to the row/entry view(s), which defines the method to be invoked e.g. :-
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/standard_listview_row_height"
>
<TextView
android:id="@+id/rsle_frequency_entry"
style="@style/textview_listentry_style"
android:textColor="@color/colorNormalButton"
android:textStyle="bold"
android:layout_weight="0.1"
/>
<TextView
android:id="@+id/rsle_addrulebutton_entry"
style="@style/textviewbutton"
android:layout_width="@dimen/standard_button_width"
android:text="@string/standardaddbuttontext"
android:onClick="onCLickAddButton"
/>
<TextView
android:id="@+id/rsle_dismissrulebutton_entry"
style="@style/textviewbutton"
android:width="@dimen/standard_button_width"
android:text="@string/standardskipbuttontext"
android:onClick="onClickSkipButton"
/>
<TextView
android:id="@+id/rsle_disablerulebutton_entry"
style="@style/textviewwarnbutton"
android:width="@dimen/standard_button_width"
android:text="@string/standarddisablebuttontext"
android:onClick="onClickDisableButton"
/>
</LinearLayout>
There is a minor issue and that is detecting which row/entry/element has been clicked. This is easily overcome by using the Tag property. Setting it to position in the adapter e.g.
@Override
public View getView(int position, View contentview, ViewGroup parent) {
View view = super.getView(position, contentview, parent);
Context context = view.getContext();
//Set button (TextViews) tag
view.findViewById(R.id.rsle_addrulebutton_entry).setTag(position);
view.findViewById(R.id.rsle_dismissrulebutton_entry).setTag(position);
view.findViewById(R.id.rsle_disablerulebutton_entry).setTag(position);
return view;
}
And in the respective onclick method retrieve the tag which is the position e.g. (Note this is from cursor rather than array but is basically the same as you access by the position/index)
public void onCLickAddButton(View view) {
Integer tag = (Integer) view.getTag();
csr.moveToPosition(tag);
//DO your stuff here.
}
Upvotes: 0
Reputation: 2022
Since all you want to do is perform a click on the "add" button, you should first include the element inside your customAdapter
then set a click listener on the element.
CustomListAdapterA(Context c, String[] times) {
super(c, R.layout.trips_listview_layout, R.id.times_textView, times);
this.context = c;
this.times_array = times;
}
@Override
public View getView(int position, View convertView, android.view.ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.trips_listview_layout, parent, false);
TextView numbers_textView = (TextView) row.findViewById(R.id.numbers_textView);
TextView times_textView = (TextView) row.findViewById(R.id.times_textView);
Button button = (Button) row.findViewById(R.id.add_alert_button);
//Set text to the array position + 1
numbers_textView.setText(Integer.toString(position + 1));
times_textView.setText(times_array[position]);
//set onClickListener
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//do what you want here
}
}
return row;
}
In your example though, you shouldn't create two custom adapters for your design. This can be achieved using one adapter.
Upvotes: 1