【问题标题】:Arraylist populating Listview in separate class not workingArraylist 在单独的类中填充 Listview 不起作用
【发布时间】:2016-08-11 22:40:52
【问题描述】:

我正在尝试在我的班级中单击按钮时将项目添加到 arraylist/adapter

MyMaxes.java:

Date currentDate = new Date();
ArrayAdapter<Record> records = new ArrayAdapter<Record>(getApplicationContext(), R.layout.custom_record);
records.add(new Record(currentDate ,maxSquat, maxBench, maxDead, maxTotal));
records.notifyDataSetChanged();

在我的另一个类 MyProgress.java 中,我正在尝试将该适配器分配给 ListView:

ListView listViewRec = (ListView) findViewById(R.id.listViewRecord);
listViewRec.setAdapter(records);

记录带有下划线并显示“预期表达”。
我觉得我需要以某种方式在适配器的自定义布局文件中设置 TextView,以便它知道将哪个项目放入哪个 TextView。
我应该有一个适配器类吗?

这是我的自定义记录:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Date"
        android:textSize="18sp"
        android:id="@+id/txtDate"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Bench"
        android:id="@+id/txtBench"
        android:paddingRight="5dp"
        android:textSize="18sp"
        android:layout_gravity="center_horizontal"
        android:layout_alignParentTop="true"
        android:paddingLeft="5dp"
        android:layout_toLeftOf="@+id/txtSquat"
        android:layout_toStartOf="@+id/txtSquat"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Squat"
        android:textSize="18sp"
        android:paddingRight="5dp"
        android:id="@+id/txtSquat"
        android:layout_alignParentTop="true"
        android:paddingLeft="5dp"
        android:layout_toLeftOf="@+id/txtDead"
        android:layout_toStartOf="@+id/txtDead"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Dead"
        android:paddingRight="5dp"
        android:textSize="18sp"
        android:id="@+id/txtDead"
        android:layout_alignParentTop="true"
        android:paddingLeft="5dp"
        android:layout_toLeftOf="@+id/txtTotal"
        android:layout_toStartOf="@+id/txtTotal"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Total"
        android:textSize="18sp"
        android:paddingRight="5dp"
        android:paddingLeft="5dp"
        android:id="@+id/txtTotal"
        android:layout_marginRight="34dp"
        android:layout_marginEnd="34dp"

        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"/>
</RelativeLayout>

还有我的自定义对象:

public class Record {

    private Integer squat, bench, dead;
    private double total;
    private Date dateTime;

    public Record(Date date,int squat, int bench, int dead, double total) {
        this.bench = bench;
        this.dateTime = date;
        this.dead = dead;
        this.squat = squat;
        this.total = total;
    }

    public Integer getSquat() {
        return squat;
    }

    public void setSquat(Integer squat) {
        this.squat = squat;
    }

    public Integer getBench() {
        return bench;
    }

    public void setBench(Integer bench) {
        this.bench = bench;
    }

    public Integer getDead() {
        return dead;
    }

    public void setDead(Integer dead) {
        this.dead = dead;
    }

    public Double getTotal() {
        return total;
    }

    public void setTotal(Integer total) {
        this.total = total;
    }

    public Date getDateTime() {
        return dateTime;
    }

    public void setDateTime(Date dateTime) {
        this.dateTime = dateTime;
    }
}

编辑::验证示例::

主要活动代码:

public class MainActivity extends AppCompatActivity {

    Button mButton;
    Integer maxSquat = 1;
    Integer maxBench = 1;
    Integer maxDead = 1;
    Double maxTotal = 3.0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mButton= (Button) findViewById(R.id.btnTest);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Date currentDate = new Date();
                ArrayAdapter<Record> records = new ArrayAdapter<Record>(getApplicationContext(), R.layout.custom_record);
                records.add(new Record(currentDate ,maxSquat, maxBench, maxDead, maxTotal));
                records.notifyDataSetChanged();
            }
        });
    }
}

listview.java:

public class listview extends MainActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview);

        ListView listViewRec = (ListView) findViewById(R.id.listViewRecord);
        listViewRec.setAdapter(records);
    }
}

listview.xml:

<LinearLayout 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="wrap_content"
    android:id="@+id/listViewRecord"/>

activitymain.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="com.bestworkouts.myapplication.MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/btnTest"/>

然后我有上面的 record.java 类,以及上面的 custom_record.xml。
奇怪的是,这给了我一个不同的错误,它在设置适配器时无法解析“记录”,在我的应用程序中,它显示记录的“预期表达”。

【问题讨论】:

标签: android listview arraylist android-arrayadapter


【解决方案1】:

为什么不能有一个视图,其中包含一个 Button 和一个添加到的 ListView?

您的第一个问题 - 您无法从点击侦听器之外的任何地方引用局部变量 records。这是一个编译错误。

第二个问题 - 每次单击按钮时,您都会创建一个全新的空适配器,然后只添加一个记录。


activitymain.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listViewRecord"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btnTest"/>

</LinearLayout>

我们称它为 RecordActivity,因为它显示记录并将记录添加到 ListView。

我建议使用AlertDialog 在您单击按钮以获取Record 对象时显示弹出视图。

public class RecordActivity extends AppCompatActivity {

    Button mButton;
    ArrayAdapter<Record> mAdapter;
    ListView listViewRec;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mButton= (Button) findViewById(R.id.btnTest);
        mAdapter = new ArrayAdapter<Record>(getApplicationContext(), R.layout.custom_record);
        listViewRec = (ListView) findViewById(R.id.listViewRecord);
        listViewRec.setAdapter(mAdapter);

        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mAdapter.add(getRecord());
            }
        });
    }

    private Record getRecord() {
        Date currentDate = new Date();
        // TODO: Get your max values from somewhere
        int maxSquat = 1;  // TODO: From input field
        int maxBench = 1; // TODO: From input field
        int maxDead = 1; // TODO: From input field

        return new Record(currentDate ,maxSquat, maxBench, maxDead);
    }
}

从参数中删除maxTotal。你可以计算里面一个Record的构造函数。

public Record(Date date,int squat, int bench, int dead) {
    this.bench = bench;
    this.dateTime = date;
    this.dead = dead;
    this.squat = squat;
    this.total = bench + dead + squat;
}

【讨论】:

  • 感谢您的回答,非常好的例子。有没有办法从单独的布局文件中做到这一点?我不反对这样做,它会起作用,我宁愿它自动添加来自另一个布局类的值,因为它对用户来说是少一步
  • 您可以根据需要拆分和重新排列布局 - 问题在于变量传递。是的,startActivityForResultonActivityResult 可以从某些“CreateRecordActivity”中“返回记录”...Example here。但是,正如我所说,用于简单弹出输入窗口的AlertDialog 似乎更简单。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多