Android - 单击CustomAdapter类中的按钮

时间:2016-11-14 02:45:33

标签: android

所以这是我的问题的摘要。我正在创建一个测试应用程序,试图提高我在android中的技能,我创建了一个列出公交时刻表的特定活动。基本上,整个页面只显示每次旅行的行程#,时间和按钮。查看图片以了解它的外观;

Screenshot

我希望列表以特定的方式设计,所以我使用了一个带有扩展arrayadapter的类的自定义设计,该类具有自己的各个行的自定义布局。

见下面的代码;我的目标是能够单击“添加”按钮,将相应行中列出的时间发送到数组中以保存以用于其他活动。我只需要帮助点击按钮,让我们说在第1行,将第一行中的TIME保存到数组中。

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>

活动本身的布局如下;

<?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>

2 个答案:

答案 0 :(得分:1)

由于您要执行的只是单击“添加”按钮,您应首先在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;
}

在您的示例中,您不应为您的设计创建两个自定义适配器。这可以使用一个适配器来实现。

答案 1 :(得分:0)

我发现将+ InnerException {"The table/view does not have a primary key defined. The Entity is read-only"} System.Exception {System.InvalidOperationException} 添加到行/条目视图是最容易的,它定义了要调用的方法,例如: : -

onClick

存在一个小问题,即检测单击了哪个行/条目/元素。使用Tag属性可以轻松克服这个问题。将其设置在适配器中的位置,例如

<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>

并且在相应的onclick方法中检索标记,该标记是例如位置。 (注意这是从游标而不是数组,但与您通过位置/索引访问的基本相同)

@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;
    }