如何在ListView中按下按钮启动活动

时间:2015-12-22 15:14:07

标签: java android xml android-layout listview

我做了一些研究,但我发现自己对ListViews的主题感到困惑,并在ListView中创建了一个按钮。

对于我的项目,我在一个layout.xml (称为recap_page.xml)中创建了一个ListView,在另一个layout.xml (row_layout.xml)中创建了我有一个布局视图(row_layout.xml上的布局是在回顾页面上显示的内容)

行使用SQLiteDatabase (DataEntryHome.java)在另一个活动中输入的数据填充(与此无关,但我只是给出一些上下文)

基本上,我想要做的是让每行中的按钮打开一个新活动。就目前而言,当我按下按钮时,应用程序崩溃了。我相信我必须使用一个通过使用onClick链接到按钮的方法。我通过创建一个名为viewOrder的方法尝试了这个,虽然我不确定我必须将该方法放在哪个活动中,因为我因为不同的目的而创建不同的活动而使自己感到困惑。

是否有人能够帮助我并解释:

a)我必须采取哪些步骤才能使这些按钮响应?

b)我需要将哪些活动纳入其中?

我的代码

recap_page.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">


    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/list_view"
        android:textColor="#000000"
        android:clickable="true">
    </ListView>

</RelativeLayout>

row_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="40dp"
    android:background="#30609d">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_marginTop="10dp"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:id="@+id/firstNameText"
            android:text="First name here"
            android:gravity="center"/>

        <TextView
            android:layout_marginTop="10dp"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:id="@+id/surnameText"
            android:text="Surname here"
            android:gravity="center"/>

        <TextView
            android:layout_marginTop="10dp"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:id="@+id/emailText"
            android:text="Email here"
            android:gravity="center"/>

        <TextView
            android:layout_marginTop="10dp"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:id="@+id/phoneText"
            android:text="Phone here"
            android:gravity="center"/>

        <TextView
            android:layout_marginTop="10dp"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:id="@+id/addInfoText"
            android:text="Additional info"
            android:gravity="center"/>

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="53dp"
            android:layout_height="37dp"
            android:text="GO"
            android:id="@+id/goButton"
            android:onClick="viewOrder"
            android:layout_marginTop="7dp"/>

    </LinearLayout>

</ScrollView>

RecapPage.java

package com.example.joe.printedclothing;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class RecapPage extends AppCompatActivity{

    String classNames[] = {"HomeActivity", "SelectDesign"};

    ListView listView;
    SQLiteDatabase sqLiteDatabase;
    UserDbHelper userDbHelper;
    Cursor cursor;
    ListDataAdapter listDataAdapter;
    Button goButtonAction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recap_page);

        goButtonAction = (Button) findViewById(R.id.goButton);
        listView = (ListView) findViewById(R.id.list_view);
        listView.setClickable(true);
        listDataAdapter = new ListDataAdapter(getApplicationContext(),R.layout.row_layout);
        listView.setAdapter(listDataAdapter);
        userDbHelper = new UserDbHelper(getApplicationContext());
        sqLiteDatabase = userDbHelper.getReadableDatabase();
        cursor = userDbHelper.getInformation(sqLiteDatabase);
        if(cursor.moveToFirst()) {
            do {

                String first_name, surname, email, phone, add_info;
                first_name = cursor.getString(0);
                surname = cursor.getString(1);
                email = cursor.getString(2);
                phone = cursor.getString(3);
                add_info = cursor.getString(4);
                DataProvider dataProvider = new DataProvider(first_name,surname,email,phone,add_info);
                listDataAdapter.add(dataProvider);

            } while (cursor.moveToNext());
        }

        Intent arrayItems = getIntent();
        Bundle arrayItemsBundle = arrayItems.getExtras();

    }


}

ListDataAdapter.java

package com.example.joe.printedclothing;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;


public class ListDataAdapter extends ArrayAdapter {
    List list = new ArrayList();
    public ListDataAdapter(Context context, int resource) {
        super(context, resource);
    }

    static class LayoutHandler {

        TextView FIRSTNAME, SURNAME, EMAIL, PHONE, ADDINFO;

    }

    @Override
    public void add(Object object) {
        super.add(object);
        list.add(object);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View row = convertView;
        LayoutHandler layoutHandler;
        if (row == null) {

            LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = layoutInflater.inflate(R.layout.row_layout,parent,false);
            layoutHandler = new LayoutHandler();
            layoutHandler.FIRSTNAME = (TextView) row.findViewById(R.id.firstNameText);
            layoutHandler.SURNAME = (TextView) row.findViewById(R.id.surnameText);
            layoutHandler.EMAIL = (TextView) row.findViewById(R.id.emailText);
            layoutHandler.PHONE = (TextView) row.findViewById(R.id.phoneText);
            layoutHandler.ADDINFO = (TextView) row.findViewById(R.id.addInfoText);
            row.setTag(layoutHandler);


        }
        else {

            layoutHandler = (LayoutHandler) row.getTag();

        }
        DataProvider dataProvider = (DataProvider)this.getItem(position);
        layoutHandler.FIRSTNAME.setText(dataProvider.getFirst_name());
        layoutHandler.SURNAME.setText(dataProvider.getSurname());
        layoutHandler.EMAIL.setText(dataProvider.getEmail());
        layoutHandler.PHONE.setText(dataProvider.getPhone());
        layoutHandler.ADDINFO.setText(dataProvider.getAdd_info());

        return row;

    }

}

我不知道为什么,但这是我的家庭活动:

package com.example.joe.printedclothing;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;

public class HomeActivity extends AppCompatActivity {

    private static Button StagHenButton, ReviewOrder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        StagHenButton = (Button) findViewById(R.id.StagHen); // Locates the button that the user presses to move to the next activity
        StagHenButton.setOnClickListener( // When the button is clicked...create an intent to move to next activity
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent newIntent = new Intent("com.example.joe.printedclothing.SelectDesign"); // Intent to choose next activity
                        startActivity(newIntent);
                    }
                }
        );

    }

    public void addContact (View view) {
        Intent intent = new Intent(this,DataEntryHome.class);
        startActivity(intent);
    }

    public void viewContact (View view) {
        Intent intent = new Intent(HomeActivity.this, RecapPage.class);
        startActivity(intent);
    }

    public void viewOrder (View view) {
        Intent intent = new Intent(HomeActivity.this, RecapPage.class);
        startActivity(intent);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_home, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

2 个答案:

答案 0 :(得分:1)

您可以在Adapter.getView方法中设置OnClickListener:

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View row = convertView;
    LayoutHandler layoutHandler;
    if (row == null) {

        LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = layoutInflater.inflate(R.layout.row_layout,parent,false);
        layoutHandler = new LayoutHandler();
        layoutHandler.FIRSTNAME = (TextView) row.findViewById(R.id.firstNameText);
        layoutHandler.SURNAME = (TextView) row.findViewById(R.id.surnameText);
        layoutHandler.EMAIL = (TextView) row.findViewById(R.id.emailText);
        layoutHandler.PHONE = (TextView) row.findViewById(R.id.phoneText);
   layoutHandler.ADDINFO = (TextView) row.findViewById(R.id.addInfoText);
   layoutHandler.goButton = (Button) row.findViewById(R.id.goButton);
        row.setTag(layoutHandler);


    }
    else {

        layoutHandler = (LayoutHandler) row.getTag();

    }
    DataProvider dataProvider = (DataProvider)this.getItem(position);
    layoutHandler.FIRSTNAME.setText(dataProvider.getFirst_name());
    layoutHandler.SURNAME.setText(dataProvider.getSurname());
    layoutHandler.EMAIL.setText(dataProvider.getEmail());
    layoutHandler.PHONE.setText(dataProvider.getPhone());
    layoutHandler.ADDINFO.setText(dataProvider.getAdd_info());

    layoutHandler.goButton.setOnClickListener( 
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //Start your activity here
                }
            }
    );


    return row;

}

答案 1 :(得分:0)

只需将以下代码放在您想要启动新Activity的函数中。这是经过测试的代码。它可能对您有所帮助

// not tested
// the code also requires a C++14 compiler
auto rhs_wrapper = [this]( auto const& x , auto& dxdt , auto t ) {
    this->rhs( x , dxdt , t ); };
auto obs = [this]( auto const& x , auto t ) {
    this->write_rhs( x , t );
integrate_adaptive( stepper , rhs_wrapper , x , max , dt , obs );