gpt4 book ai didi

java - 带按钮和 EditText 的 ListView

转载 作者:行者123 更新时间:2023-12-01 14:56:03 25 4
gpt4 key购买 nike

我有一个 XML 布局,其中包含一个 EditText,用户可以在其中输入人名,然后是一个按钮,用于在数据库中搜索此内容,然后将结果显示在紧邻其下方的 ListView 中。

我在 OnClickListener 上遇到错误:

searchButton.setOnClickListener(this);

我已读到需要使用不同的“OnClickListener”导入?但我已经改变了它,但它仍然给我一个 NullPointerException 错误。

谁能告诉我如何使这个布局协同工作?

这是我的 XML 布局:

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

<TextView
android:id="@+id/txtsearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Type Name To Search:"
android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
android:id="@+id/inputName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >

<requestFocus />
</EditText>

<Button
android:id="@+id/btnSearchName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Search" />

<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>

</LinearLayout>

我的搜索类别:

package com.example.flybase2;

import android.app.Activity;
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.View;

// Imported OnClickListener
import android.view.View.OnClickListener;

import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;


//Extends ListActivity, Implements OnClickListener. Above Import for OnClickListener in imports.
public class search extends ListActivity implements OnClickListener {

Button searchButton;
EditText searchName;
ListView searchedListResults;


@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.search);

searchButton = (Button) findViewById(R.id.btnSearch);
searchName = (EditText) findViewById(R.id.inputName);

//ERROR: OnClickListener is causing a crash.
searchButton.setOnClickListener(this);

}


@Override
public void onClick(View clickedSearchButton) {

String searchedName = searchName.getText().toString();

searchedListResults = (ListView)findViewById(android.R.id.list);

DBHandler DBsearchRef = new DBHandler(this, null, null);

DBHandler search = new DBHandler(this, null, null);

search.open();
Cursor cursor = search.searchOnName(searchedName);

startManagingCursor(cursor);

@SuppressWarnings("static-access")
String [] from = new String [] {DBsearchRef.KEY_NAME, DBsearchRef.KEY_TEL, DBsearchRef.KEY_EMAIL, DBsearchRef.KEY_COMMENTS};
int [] to = new int [] {R.id.txtNameList, R.id.txtTelList, R.id.txtEmailList, R.id.txtCommentsList};

@SuppressWarnings("deprecation")
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.search, cursor, from, to);
searchedListResults.setAdapter(cursorAdapter);




}

}

最佳答案

在你的 onCreate 中你必须改变

searchButton = (Button) findViewById(R.id.btnSearch);  

searchButton = (Button) findViewById(R.id.btnSearchName);  

关于java - 带按钮和 EditText 的 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14303753/

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