我的应用程序中有一个TableLayout。到目前为止,我有两条明确的行。 One Row分配了静态项目,另一行分配了3个微调器。
我想要做的是,当我点击按钮时添加一个新的TableRow。到目前为止,我有以下代码:
<TableRow
android:layout_width="fill_parent">
android:stretchColumns="0,1,2"
<Spinner
android:padding="3dip"
android:gravity="left"
android:id="@+id/Spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp" />
<Spinner
android:padding="3dip"
android:gravity="left"
android:id="@+id/Spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp" />
<Spinner
android:padding="3dip"
android:gravity="left"
android:id="@+id/spinner3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp" />
</TableRow>
这只是我的第二行,当我点击所述按钮时,我要添加的行。我的问题是,每次用户点击按钮时,如何将以下行添加到我的视图中?微调器的值会有所不同,但变量将保持不变。
编辑:
更多代码:
/* Find Tablelayout defined in teh XML file */
TableLayout tl = (TableLayout) findViewById(R.id.listTable);
/* Create a new row to be added. */
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
/* Create a Button to be the row-content. */
Button b = new Button(this);
b.setText("Add row");
b.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
/* Add Button to row. */
tr.addView(b);
/* Add row to TableLayout. */
tl.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
}
视图已更改为:
<TableRow
android:id="@+id/copyRow"
android:layout_width="fill_parent" >
android:stretchColumns="0,1,2"
<Spinner
android:padding="3dip"
android:gravity="left"
android:id="@+id/Spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp" />
<Spinner
android:padding="3dip"
android:gravity="left"
android:id="@+id/Spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp" />
<Spinner
android:padding="3dip"
android:gravity="left"
android:id="@+id/spinner3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp" />
</TableRow>
如果它有所作为,我使用JellyBean 4.3作为目标API
答案 0 :(得分:0)
这是ListView
的解决方案您可以查找所有源代码here
活动类
public class MainActivity extends Activity {
private List<RowItem> rows = new ArrayList<RowItem>();
private ListView list;
private Button addButton;
private SimpleListAdapter listAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get view elements
list = (ListView)findViewById(R.id.list);
addButton = (Button)findViewById(R.id.addButton);
//lets add few items to our list, note that selection item should not exceed length of spinners item
rows.add(new RowItem(0, 0, 0));
rows.add(new RowItem(0, 1, 2));
rows.add(new RowItem(2, 1, 1));
//create adapter and assign it to your list view
listAdapter = new SimpleListAdapter(this, R.layout.list_item, rows);
list.setAdapter(listAdapter);
addButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
rows.add(new RowItem(0, 0, 0));
listAdapter.notifyDataSetChanged();
}
});
}
适配器类
public class SimpleListAdapter extends ArrayAdapter<RowItem>{
private Context context;
private int layoutResourceId;
private List<RowItem> data = null;
private ArrayList<String> spinnerArray1 = new ArrayList<String>();
private ArrayList<String> spinnerArray2 = new ArrayList<String>();
private ArrayList<String> spinnerArray3 = new ArrayList<String>();
public SimpleListAdapter(Context context, int layoutResourceId, List<RowItem> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
spinnerArray1.add("S1 One");
spinnerArray1.add("S1 Two");
spinnerArray1.add("S1 Three");
spinnerArray2.add("S2 One");
spinnerArray2.add("S2 Two");
spinnerArray2.add("S2 Three");
spinnerArray3.add("S3 One");
spinnerArray3.add("S3 Two");
spinnerArray3.add("S3 Three");
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ItemHolder holder;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ItemHolder();
holder.Spinner1 = (Spinner)row.findViewById(R.id.Spinner1);
holder.Spinner2 = (Spinner)row.findViewById(R.id.Spinner2);
holder.Spinner3 = (Spinner)row.findViewById(R.id.Spinner3);
//add adapter to spinners so that there is something to select from
ArrayAdapter<String> spinnerArrayAdapter1 = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_dropdown_item, spinnerArray1);
holder.Spinner1.setAdapter(spinnerArrayAdapter1);
ArrayAdapter<String> spinnerArrayAdapter2 = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_dropdown_item, spinnerArray2);
holder.Spinner2.setAdapter(spinnerArrayAdapter2);
ArrayAdapter<String> spinnerArrayAdapter3 = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_dropdown_item, spinnerArray3);
holder.Spinner3.setAdapter(spinnerArrayAdapter3);
row.setTag(holder);
}
else
{
holder = (ItemHolder)row.getTag();
}
final RowItem rowItem = data.get(position);
holder.Spinner1.setSelection(rowItem.getSelectionIndex1());
holder.Spinner2.setSelection(rowItem.getSelectionIndex2());
holder.Spinner3.setSelection(rowItem.getSelectionIndex3());
holder.Spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Toast.makeText(context, "you selected an item from spinner 1", Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
return row;
}
class ItemHolder{
Spinner Spinner1;
Spinner Spinner2;
Spinner Spinner3;
}
简单模型
public class RowItem {
private int selectionIndex1;
private int selectionIndex2;
private int selectionIndex3;
public RowItem(int selectionIndex1, int selectionIndex2, int selectionIndex3) {
super();
this.selectionIndex1 = selectionIndex1;
this.selectionIndex2 = selectionIndex2;
this.selectionIndex3 = selectionIndex3;
}
public int getSelectionIndex1() {
return selectionIndex1;
}
public void setSelectionIndex1(int selectionIndex1) {
this.selectionIndex1 = selectionIndex1;
}
public int getSelectionIndex2() {
return selectionIndex2;
}
public void setSelectionIndex2(int selectionIndex2) {
this.selectionIndex2 = selectionIndex2;
}
public int getSelectionIndex3() {
return selectionIndex3;
}
public void setSelectionIndex3(int selectionIndex3) {
this.selectionIndex3 = selectionIndex3;
}
}
查看项目:主视图
<LinearLayout 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=".MainActivity"
android:orientation="vertical"
android:gravity="center" >
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:dividerHeight="5dp"/>
<Button
android:id="@+id/addButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Add Item"
/>
列表视图的项呈示器
<?xml version="1.0" encoding="utf-8"?>
<Spinner
android:id="@+id/Spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:gravity="left"
android:padding="3dip" />
<Spinner
android:id="@+id/Spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:gravity="left"
android:padding="3dip" />
<Spinner
android:id="@+id/Spinner3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:gravity="left"
android:padding="3dip" />