Android 一个listview中有两个适配器
我为提出一个已经存在的问题提前道歉,但我似乎无法让它发挥作用(我对安卓工作室也比较陌生) 我正在创建一个家庭作业应用程序,它通过弹出窗口接收用户输入,并将用户输入存储为字符串数组,然后转换为适配器。然后使用适配器通过列表视图将数组的值打印到用户屏幕上 我遇到的问题是我有两列:一列用于类名,另一列用于类赋值。我可以使用一个适配器显示类名的所有值,但另一列保留默认提示值。我试图用每个类名及其各自的赋值相应地显示这两列 我知道我必须以某种方式将适配器合并到一个适配器中,但我不知道如何做(请参阅Java for Planner)。如果有人能帮我,我将不胜感激 这是用于屏幕的JavaAndroid 一个listview中有两个适配器,android,listview,adapter,Android,Listview,Adapter,我为提出一个已经存在的问题提前道歉,但我似乎无法让它发挥作用(我对安卓工作室也比较陌生) 我正在创建一个家庭作业应用程序,它通过弹出窗口接收用户输入,并将用户输入存储为字符串数组,然后转换为适配器。然后使用适配器通过列表视图将数组的值打印到用户屏幕上 我遇到的问题是我有两列:一列用于类名,另一列用于类赋值。我可以使用一个适配器显示类名的所有值,但另一列保留默认提示值。我试图用每个类名及其各自的赋值相应地显示这两列 我知道我必须以某种方式将适配器合并到一个适配器中,但我不知道如何做(请参阅Java
package com.example.johnta.homeworkappv2;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Arrays;
public class PlannerActivity extends ListActivity {
public static ArrayList<String> arrayList, arrayList_assignments;
private static ArrayAdapter<String> adapter, adapter_assignments;
private static EditText txtInput;
private static TextView tvMsg;
private static PopupWindow popUpWindow;
private static LinearLayout mainLayout;
private String [] items = {"Physics","Humanities","Math","STEM"};
private String [] list_of_assignments = {"Notes","Reading","Problems","Engineering"};
private String [][] twoDimensionalItems = {{"Physics","Notes"}};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
popUpWindow = new PopupWindow(this);
mainLayout = new LinearLayout(this);
setContentView(R.layout.activity_planner);
ListView listView = (ListView)findViewById(android.R.id.list);
arrayList = new ArrayList<>(Arrays.asList(items));
arrayList_assignments = new ArrayList<>(Arrays.asList(list_of_assignments));
adapter = new ArrayAdapter<String>(this,R.layout.editlistitems,R.id.name_of_class,arrayList);
adapter_assignments = new ArrayAdapter<String>(this,R.layout.editlistitems,R.id.homework_description,arrayList_assignments);
listView.setAdapter(adapter);
txtInput = (EditText)findViewById(R.id.name_of_class);
}
public void makeEditable(View v) {
}
public void onClickEdit (View v) {
startActivity(new Intent(PlannerActivity.this,popup.class));
}
public static void addItemToArray (String itemToAdd) {
arrayList.add(itemToAdd);
adapter.notifyDataSetChanged();
}
}
**这是活动计划器的XML**
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.johnta.homeworkappv2.settings"
android:background="#87ceeb">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintTop_creator="1">
<ImageView
android:id="@+id/planner_picture"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginTop="5dp"
android:background="@drawable/roundbutton"
app:srcCompat="@drawable/planner10pad"
android:layout_centerHorizontal="true"
android:layout_marginBottom="35dp" />
<Button
android:id="@+id/edit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Edit"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="130dp"
android:layout_marginTop="154dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="onClickEdit"/>
<Button
android:id="@+id/copy_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Copy"
android:layout_alignBaseline="@+id/edit_button"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignRight="@id/planner_picture"
/>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Planner"
android:textColor="#000000"
android:textSize="32sp"
android:layout_alignTop="@+id/edit_button"
android:gravity="center"
android:layout_alignBottom="@+id/edit_button"
android:layout_alignLeft="@+id/planner_picture"
android:layout_alignStart="@+id/planner_picture"
android:layout_alignRight="@+id/planner_picture"
android:layout_alignEnd="@+id/planner_picture" />
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:layout_below="@+id/edit_button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_weight="1"
android:divider="#000000">
</ListView>
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
**这是Planner中listview的XML**
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/name_of_class"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:paddingTop="3dp"
android:paddingBottom="3dp"
android:paddingLeft="5dp"
android:paddingRight="3dp"
android:hint="Class name"
android:textStyle="bold"
/>
<TextView
android:id="@+id/homework_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="3dp"
android:paddingBottom="3dp"
android:paddingLeft="3dp"
android:hint="Class Assignment"
android:textSize="20sp"
android:focusable="true"
android:focusableInTouchMode="true"
android:inputType="textMultiLine"
android:layout_alignBaseline="@+id/name_of_class"
android:layout_alignBottom="@+id/name_of_class"
android:layout_toRightOf="@+id/name_of_class"
android:layout_toEndOf="@+id/name_of_class" />
</RelativeLayout>
我遇到的问题是我有两列:一列用于类名,另一列用于类赋值。我可以使用一个适配器显示类名的所有值,但另一列保留默认提示值
不要为此存储两个ArrayList
您可以使用Hashmap和SimpleAdapter
或使用Java的OOP功能为某些包含两个字符串的Plan
类创建自定义ArrayAdapter(注意class
是Java中的保留字)
详情
可能也值得研究Android数据绑定只需使用一个自定义适配器,该适配器接受arraylist并更新onBindView中的值
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/name_of_class"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:paddingTop="3dp"
android:paddingBottom="3dp"
android:paddingLeft="5dp"
android:paddingRight="3dp"
android:hint="Class name"
android:textStyle="bold"
/>
<TextView
android:id="@+id/homework_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="3dp"
android:paddingBottom="3dp"
android:paddingLeft="3dp"
android:hint="Class Assignment"
android:textSize="20sp"
android:focusable="true"
android:focusableInTouchMode="true"
android:inputType="textMultiLine"
android:layout_alignBaseline="@+id/name_of_class"
android:layout_alignBottom="@+id/name_of_class"
android:layout_toRightOf="@+id/name_of_class"
android:layout_toEndOf="@+id/name_of_class" />
</RelativeLayout>