gpt4 book ai didi

java - 如何在 ListView 中按下按钮时启动 Activity

转载 作者:行者123 更新时间:2023-12-01 10:40:46 26 4
gpt4 key购买 nike

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

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

这些行填充了用户使用 SQLiteDatabase 在另一个 Activity 中输入的数据(DataEntryHome.java)(与此无关,但我只是提供一些上下文)

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

有谁能帮助我并解释一下:

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

b) 我需要将它们实现到哪些 Activity 中?

我的代码

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;

}

}

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

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

最佳答案

您可以在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;

}

关于java - 如何在 ListView 中按下按钮时启动 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34418890/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com