创建一个简单的注册表单并将值存储在数据库中。用户完成注册后,存储的数据应显示在主页上。在主页上,我创建了一个ListView,其中我想在一行中显示用户给出的用户名以及与其相邻的按钮,点击该按钮后用户应该可以在另一页上看到注册条目。
为此,我尝试使用simpleAdapter,但我认为没有工作或没有正确编程。我还想过使用Expandable ListView但不知道如何使用它。
请帮助我,建议您在ListView中显示注册内容以及如何在ListView中插入按钮执行点击事件。
我的代码是:
HomeActivity.java
package com.nsa.project;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.SubMenu;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class HomeActivity extends Activity {
Button btnDetails;
EditText edtusername;
ListView details;
ArrayList<HashMap<String, String>> arraylist = new ArrayList<HashMap<String, String>>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
edtusername = (EditText) findViewById(R.id.edtuser);
btnDetails = (Button) findViewById(R.id.btndetails);
details = (ListView) findViewById(R.id.regsdisplay);
}
public void displayDetails()
{
DataBase d = new DataBase(HomeActivity.this);
arraylist = d.readDetails();
//Expandable ListView
details.setAdapter(new SimpleAdapter(this, arraylist, R.layout.mylayout, new String[] {CT.USER_NAME}, new int[] {R.id.textView1, R.id.btndetails}));
}
@Override
public void onResume()
{
// TODO Auto-generated method stub
super.onResume();
displayDetails();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
int groupId = 0;
int order = 0;
int itemId = 0 ;
String title = "My menu";
// Inflate the menu; this adds items to the action bar if it is present.
menu.add(title);
MenuItem mNext = menu.add(groupId, itemId, order, " New User").setIcon(R.drawable.add_user);
mNext.setOnMenuItemClickListener(new OnMenuItemClickListener(){
@Override
public boolean onMenuItemClick(MenuItem item) {
// TODO Auto-generated method stub
Intent in = new Intent(HomeActivity.this, Registration.class);
startActivity(in);
return false;
}
});
MenuItem mExit = menu.add(groupId, itemId+1, order+1, "Exit").setIcon(R.drawable.exit);
mExit.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
// TODO Auto-generated method stub
finish();
return false;
}
});
SubMenu sb = menu.addSubMenu("Create User");
sb.add(groupId, itemId, order, "First");
sb.add(groupId, itemId+1, order+1, "2");
return true;
}
}
现在,布局
activity_home.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".HomeActivity" >
<ListView
android:id="@+id/regsdisplay"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</RelativeLayout>
mylayout.xml
<?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" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="22dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#CCBBAA" />
<Button
android:id="@+id/btndetails"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#FFAAFF"
android:text="View Details"></Button>
</LinearLayout>
LOgcat错误
01-26 23:11:42.643: E/AndroidRuntime(410): FATAL EXCEPTION: main
01-26 23:11:42.643: E/AndroidRuntime(410): java.lang.ArrayIndexOutOfBoundsException
01-26 23:11:42.643: E/AndroidRuntime(410): at android.widget.SimpleAdapter.bindView(SimpleAdapter.java:160)
01-26 23:11:42.643: E/AndroidRuntime(410): at android.widget.SimpleAdapter.createViewFromResource(SimpleAdapter.java:126)
01-26 23:11:42.643: E/AndroidRuntime(410): at android.widget.SimpleAdapter.getView(SimpleAdapter.java:114)
01-26 23:11:42.643: E/AndroidRuntime(410): at android.widget.AbsListView.obtainView(AbsListView.java:1315)
01-26 23:11:42.643: E/AndroidRuntime(410): at android.widget.ListView.measureHeightOfChildren(ListView.java:1198)
01-26 23:11:42.643: E/AndroidRuntime(410): at android.widget.ListView.onMeasure(ListView.java:1109)
01-26 23:11:42.643: E/AndroidRuntime(410): at android.view.View.measure(View.java:8171)
01-26 23:11:42.643: E/AndroidRuntime(410): at android.widget.RelativeLayout.measureChild(RelativeLayout.java:563)
01-26 23:11:42.643: E/AndroidRuntime(410): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:378)
01-26 23:11:42.643: E/AndroidRuntime(410): at android.view.View.measure(View.java:8171)
01-26 23:11:42.643: E/AndroidRuntime(410): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
01-26 23:11:42.643: E/AndroidRuntime(410): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
01-26 23:11:42.643: E/AndroidRuntime(410): at android.view.View.measure(View.java:8171)
01-26 23:11:45.684: I/Process(410): Sending signal. PID: 410 SIG: 9
答案 0 :(得分:2)
ListView details
未初始化。在setContentView()
{/ 1}}之后添加以下内容:
onCreate()
后续details = (ListView)findViewById(R.id.regsdisplay);
来到这里:
ArrayIndexOfBoundException
只有一个details.setAdapter(new SimpleAdapter(this, arraylist, R.layout.mylayout, new String[] {CT.USER_NAME}, new int[] {R.id.textView1, R.id.btndetails}));
列名称和两个from
视图标识符。
答案 1 :(得分:2)
我没有看到您初始化ListView details;
的任何地方,而且
details.setAdapter(new SimpleAdapter(this, arraylist, R.layout.mylayout, new String[] {CT.USER_NAME}, new int[] {R.id.textView1, R.id.btndetails}));
初始化details
。
details = (ListView)findViewById(R.id.regsdisplay);
此外,listview位于activity_home.xml
,但我没有看到EditText
和Button
。
mylayout.xml
中的按钮不在activity_home.xml
<Button
android:id="@+id/btndetails"
您发布了一个不完整的布局,或者在引用充气布局的ID时搞砸了。