2016-05-23 75 views
2

因此,我創建了一個小應用程序,並感謝這個網站,我已經在這方面取得了很多進展。它的想法是我只需要一個按鈕,當它被點擊時,它會添加一個帶有兩個「EditText」和一個「CheckBox」的新行。我已經通過動態添加這些自定義ListView和自定義適配器來實現這一點。我仍然在自定義ListView和適配器上。將視圖添加到另一行(另一個LinearLayout)?

無論如何,我已經得到它來添加兩個EditText和一個CheckBox,當點擊按鈕時,但它不會在此之後添加任何東西(儘管這是一個單獨的問題)。我遇到的問題是,它會將它們添加到與按鈕相同的行上。這是我迷路的地方。我不知道如何讓這個自定義ListView顯示在新行上,而不是像按鈕一樣顯示。

enter image description here

現在,這裏是我的MainActivity代碼:

import android.content.Context; 
import android.graphics.Color; 
import android.support.v7.app.ActionBar; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.text.InputType; 
import android.view.Gravity; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.CheckBox; 
import android.widget.EditText; 
import android.widget.LinearLayout; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
import android.widget.RelativeLayout; 
import android.view.ViewGroup.LayoutParams; 
import java.lang.reflect.Array; 
import java.util.ArrayList; 
import java.util.List; 

public class MainActivity extends AppCompatActivity { 
    ArrayList<CheckBox> checkBoxes = new ArrayList<>(); 
    ArrayList<EditText> courses = new ArrayList<>(); 
    ArrayList<EditText> credits = new ArrayList<>(); 

    int numOfCheckBoxes = 0; 

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

     final LinearLayout ll = (LinearLayout) findViewById(R.id.ll); 
     ll.setOrientation(LinearLayout.HORIZONTAL); 

     Button addCourse = new Button(this); 
     addCourse.setLayoutParams(new ActionBar.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); 
     addCourse.setText("Add Course"); 
     addCourse.setTextColor(Color.parseColor("#FFFFFF")); 
     addCourse.setBackgroundColor(Color.parseColor("#FF0000")); 
     addCourse.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       ListView myListView = new ListView(MainActivity.this); 
       List<CustomRow> rows = new ArrayList<CustomRow>(); 
       rows.add(new CustomRow(MainActivity.this)); 
       MyListArrayAdapter myAdapter = new MyListArrayAdapter(MainActivity.this, rows); 
       myListView.setAdapter(myAdapter); 
       ll.addView(myListView); 
      } 
     }); 
     ll.addView(addCourse); 
    } // End of onCreate 

    public void onCheckBoxChecked(View v) { 
     for (int i = 0; i < numOfCheckBoxes; i++) { 
      if (checkBoxes.get(i).isChecked()) { 
       courses.get(i).setEnabled(false); 
       courses.get(i).setFocusable(false); 
       courses.get(i).setInputType(InputType.TYPE_NULL); 
       credits.get(i).setEnabled(false); 
       credits.get(i).setFocusable(false); 
       credits.get(i).setInputType(InputType.TYPE_NULL); 
      } else { 
       courses.get(i).setEnabled(true); 
       courses.get(i).setFocusable(true); 
       courses.get(i).setFocusableInTouchMode(true); 
       courses.get(i).setInputType(InputType.TYPE_CLASS_TEXT); 
       credits.get(i).setEnabled(true); 
       credits.get(i).setFocusableInTouchMode(true); 
       credits.get(i).setFocusable(true); 
       credits.get(i).setInputType(InputType.TYPE_CLASS_NUMBER); 
      } // End of if else 
     } // End of for loop 
    } // End of onCheckBoxChecked 
} // End of MainActivity 

這裏是我的 「MyListArrayAdapter」 級(我的自定義適配器):

import android.app.Activity; 
import android.content.Context; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.view.LayoutInflater; 
import android.widget.CheckBox; 
import android.widget.EditText; 

import java.util.List; 

/** 
* Created by Thomas on 5/21/2016. 
*/ 
public class MyListArrayAdapter extends BaseAdapter { 

    LayoutInflater inflater; 
    List<CustomRow> rows; 

    public MyListArrayAdapter(Activity context, List rows){ 
     super(); 

     this.rows = rows; 
     this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 


    @Override 
    public int getCount() { 
     return rows.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return null; 
    } 

    @Override 
    public long getItemId(int position) { 
     return 0; 
    } 

    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 

     CustomRow row = rows.get(position); 

     View v = convertView; 

     if (convertView==null) { 
      v = inflater.inflate(R.layout.new_course_row, null); 
     } 

     EditText course = (EditText) v.findViewById(R.id.course); 
     EditText credits = (EditText) v.findViewById(R.id.credits); 
     CheckBox checkBox = (CheckBox) v.findViewById(R.id.checkBox); 

     course.setHint("Enter Course"); 
     credits.setHint("Enter Credits"); 
     checkBox.setText("Check if complete"); 
     course.setMaxHeight(150); 
     credits.setMaxHeight(150); 
     checkBox.setMaxHeight(150); 

     return v; 
    } 
} 

我 「CustomRow」 級(帶有兩個EditText和一個CheckBox的自定義類):

import android.content.Context; 
import android.widget.CheckBox; 
import android.widget.EditText; 
import android.widget.LinearLayout; 

/** 
* Created by Thomas on 5/21/2016. 
*/ 
public class CustomRow extends LinearLayout{ 


     public CustomRow(Context context) { 
     super(context); 

     setOrientation(LinearLayout.HORIZONTAL); 

     EditText course = new EditText(context); 
     EditText credits = new EditText(context); 
     CheckBox checkBox = new CheckBox(context); 

     addView(course); 
     addView(credits); 
     addView(checkBox); 
    } 
} 

我 「new_course_row.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"> 

    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/course" 
     android:layout_weight="1"/> 

    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/credits" 
     android:layout_weight="1"/> 

    <CheckBox 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/checkBox" 
     android:onClick="onCheckBoxChecked"/> 

</LinearLayout> 

最後,我的 「activity_main.xml中」:

<?xml version="1.0" encoding="utf-8"?> 

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

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     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="exporian.collegetracker.MainActivity" 
     android:id="@+id/ll"> 

    </LinearLayout> 
</ScrollView> 

預先感謝您大家!我在這裏呆了幾天。不尋找任何人爲我編寫應用程序,只是尋找一些方向。

+0

我也這樣做了我的應用程序,但我沒有動態添加視圖。我剛剛在我的XML佈局和OnCreate()中設置了它們,將View.GONE和Button onClick的可見性設置爲View.VISIBLE。 –

+0

您需要使用帶有自定義適配器的Listview或RecyclerView。這是最簡單的方法。 –

回答

0

確保添加一個按鈕後,你已經在你的適配器稱爲這兩種方法來更新列表

BuddyFeedsAdapter.this.notifyDataSetChanged(); 
      notifyDataSetInvalidated(); 
0

如果你的代碼是否正常工作,並在同一行中添加行,那麼請檢查下面的解決方案。

如您有以下行寫,

List<CustomRow> rows = new ArrayList<CustomRow>(); 
在OnButton Click事件

,所以每當按鈕被點擊,它會再次初始化行並拆除所有先前的記錄,所以只寫上面的頂線或在setContentView之後的onCreate()方法中,那麼您將獲得點擊的所有行。

希望這會幫助你。