所以这里是我的问题的总结。我正在创建一个测试应用程序只是为了提高我在 android 方面的技能,并且我创建了一个列出公交时刻表的特定 Activity 。所以基本上,整个页面只显示每次旅行的旅行#、时间和按钮。查看图像以了解它的外观;
我希望以特定方式设计列表,因此我使用了自定义设计,其中包含一个扩展 arrayadapter 的类,该类对各个行都有自己的自定义布局。
见下面的代码;我的目标是能够单击添加按钮,将相应行中列出的时间发送到数组中以保存以供在另一个 Activity 中使用。我只需要帮助来点击按钮,假设在第 1 行中,将第一行中的时间保存到一个数组中。
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;
}
}
我的行布局代码如下;
<?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>
Activity 本身的布局如下;
<?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>
最佳答案
由于您要做的只是点击“添加”按钮,您应该首先在 customAdapter
中包含该元素,然后在该元素上设置点击监听器。
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;
}
但在您的示例中,您不应为您的设计创建两个自定义适配器。这可以使用一个适配器来实现。
关于Android - 单击 CustomAdapter 类中的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40581199/