我创建了一个名为Student的类,我想将一些学生的对象添加到arrayList中。 我在java Android中有一个活动,我想通过单击添加按钮来创建每个对象。同时我想在该活动上方的自定义列表视图中显示学生姓名和地址,这意味着我想在布局中列出学生arrayList。 我应该如何为这个arraylist制作适配器? 任何人都可以完成这些代码吗?
Button btnadd = (Button) findViewById(R.id.btnAdd);
btnadd.setOnClickListener(new OnClickListener(){
public void onClick(View arg0){
EditText etname = (EditText) findViewById(R.id.etName);
EditText etaddress = (EditText) findViewById(R.id.etAddress);
RadioGroup typeRadio = (RadioGroup)findViewById(R.id.radioType);
List<Student> rlist = new ArrayList<Student>();
rlist.add(new Student(etname.getText().toString(),
etaddress.getText().toString(),
typeRadio.toString()));
}
}
);
}
private class MyArrayAdapter<String> extends ArrayAdapter<String>{
?
?
?
}
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.list_layout, parent, false);
ImageView iv = (ImageView) row.findViewById(R.id.resType);
TextView tv1 = (TextView) row.findViewById(R.id.resName);
TextView tv2 = (TextView) row.findViewById(R.id.resAddress);
?
?
?
?
return row;
}
答案 0 :(得分:2)
这是工作代码。您可以在custom_row.xml
中为每行添加所需内容。
MainActivity.class
public class MainActivity extends ActionBarActivity {
ListView listView;
CustomAdapter adapter;
ArrayList<Student> studentArrayList;
EditText ename, eaddress;
Button enter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView);
ename = (EditText)findViewById(R.id.eName);
eaddress = (EditText)findViewById(R.id.eAddress);
enter = (Button) findViewById(R.id.enter);
studentArrayList = new ArrayList<Student>();
adapter = new CustomAdapter(this,studentArrayList);
listView.setAdapter(adapter);
enter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String got_name = ename.getText().toString();
String got_address = eaddress.getText().toString();
if (got_name!=null && got_address!=null)
{
Student student = new Student(got_name, got_address);
studentArrayList.add(student);
ename.setText(" ");
eaddress.setText(" ");
adapter.notifyDataSetChanged();
}
}
});
}
private class CustomAdapter extends BaseAdapter {
Context context;
ArrayList<Student> studentArrayList;
public CustomAdapter(MainActivity activity, ArrayList<Student> studentArrayList) {
this.context = activity;
this.studentArrayList = studentArrayList;
}
@Override
public int getCount() {
return studentArrayList.size();
}
@Override
public Object getItem(int i) {
return i;
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View v = view;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.custom_row, null);
}
TextView studentName = (TextView)v.findViewById(R.id.studentName);
TextView studentAddress = (TextView)v.findViewById(R.id.studentAddress);
studentName.setText(studentArrayList.get(i).getName());
studentAddress.setText(studentArrayList.get(i).getAddress());
return v;
}
}
}
activity_main.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" tools:context=".MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="200dp"
android:id="@+id/listView"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Enter Name : "
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="22dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Enter Address : "
android:id="@+id/textView2"
android:layout_below="@+id/textView"
android:layout_alignLeft="@+id/textView"
android:layout_alignStart="@+id/textView"
android:layout_marginTop="20dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/eName"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/listView"
android:layout_alignEnd="@+id/listView"
android:layout_toRightOf="@+id/textView2"
android:layout_toEndOf="@+id/textView2" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/eAddress"
android:layout_alignBottom="@+id/textView2"
android:layout_toRightOf="@+id/textView2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:id="@+id/enter"
android:layout_below="@+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="32dp" />
</RelativeLayout>
Student.class
public class Student {
String name, address;
public Student(String name, String address) {
this.name = name;
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
custom_row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="New Text"
android:id="@+id/studentName"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Medium Text"
android:id="@+id/studentAddress"
android:layout_below="@+id/studentName"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
输出: