2016-08-11 13 views
0

我試圖將項目添加到數組列表上點擊一個按鈕/適配器在我的課的ArrayList在單獨的類移植ListView不工作

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); 

中的記錄有下劃線並說:「表達的預期」。
我覺得我需要以某種方式在適配器中的自定義佈局文件中設置TextViews,以便知道將哪個項目放入哪個TextView中。
我應該有一個適配器類?

這裏是我的custom_record:

<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 fr om以上。
奇怪的是,這個給了我一個不同的錯誤,它不能解決設置適配器時的「記錄」,在我的應用程序中它說「記錄預期的表達」。

+0

你是什麼意思「你的其他班級」?就像那樣,你不能在類之間引用變量。 –

+0

您是否正在[活動間傳遞數據](http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-on-android)? –

+0

我希望能夠將項目添加到一個類中的數組列表,並使用另一個類中的數組列表填充列表視圖。我在前面的問題中使用了這個答案:http://stackoverflow.com/questions/ 38904588 /添加自定義對象到列表列表按鈕單擊/ 38905151?noredirect = 1#comment65169369_38905151 – LBJ33

回答

1

爲什麼你不能有一個視圖與你添加到一個按鈕和一個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; 
} 
+0

感謝這個答案,很好的例子。有沒有辦法從單獨的佈局文件做到這一點?我不反對這樣做,它會工作,我只是寧願自動添加其他佈局類中的值,因爲它對用戶而言只有一個步驟 – LBJ33

+0

您可以拆分並重新排列布局,你想要的 - 這是變量傳遞,這是問題。是的,可以用'startActivityForResult'和'onActivityResult'從一些「CreateRecordActivity」中「返回記錄」... [這裏是示例](http://stackoverflow.com/questions/10407159/how-to-manage -startactivityforresult-ON-機器人)。然而,正如我所說的,一個簡單的彈出式輸入窗口的AlertDialog似乎更簡單。 –