2016-12-24 91 views
0

我在想什么,我想知道是否有任何解决方案。如何正确填充我的需求的列表项目

我想制作一个程序,当我要按“添加”button时,将创建一个新行,其左侧为Edittext,右侧为TextView。每次按“添加”button时,都会添加一个新的行,其中EdittextTextview将被添加。

我已经阅读了一些教程,但都只是说一个字段和使用listview

如何添加两个字段? 我无法弄清楚如何设置每一行和字段的位置?例如,我希望2个字段的距离为30dp,或者每行的距离为25dp。 此外,我将如何知道每个人的ID以便进行一些计算?

任何人都可以解释我的东西,给一些代码或建议我任何教程?

非常感谢!

类似下面(我做了油漆)

enter image description here

......更新中...

所以,我做出一些改变走得更远一点点..我为TextViews和EditTexts创建了两个列表,以便稍后检索它们。但是,当我第二次按下按钮时,它会卡住。在第一次按下时,它会创建第一行,但在第二次按下时,它会卡住,甚至没有割裂。我敢肯定,这是愚蠢的,我没有看到它..

 EditText ed; 
     List<EditText> allEds = new ArrayList<EditText>(); 
     TextView tv; 
     List<TextView> allTvs = new ArrayList<TextView>(); 

     for (i = 0; i < counter; i++) { 

      ed = new EditText(Main_5.this); 
      allEds.add(ed); 
      ed.setId(counter); 
      ed.setLayoutParams(new TableLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 0.5f)); 
      linearLayout.addView(ed); 
      ed.setInputType(InputType.TYPE_CLASS_NUMBER| InputType.TYPE_NUMBER_FLAG_DECIMAL); 


      tv = new TextView(Main_5.this); 
      allTvs.add(tv); 
      tv.setId(counter); 
      tv.setLayoutParams(new TableLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 0.5f)); 
      linearLayout.addView(tv); 

      mainLinearLayout.addView(linearLayout); 

     } 

另外,你可以给我一些帮助,以retreiving IDS?我想获得EditText ID,然后显示特定的TextView ID ..我认为这样的开始,但我无法测试它,因为错误..我想进一步采取它,以便把TextView的EditText ..任何想法?

Double[] doubles = new Double[allEds.size()]; 

     for(i=0;i<allEds.size();i++){ 

      doubles[i] = allEds.get(i).getText().toDouble(); 

     } 

谢谢!

+0

不多.. 我刚才提出的XML,然后我试着用一个列表视图,但我不能把两个字段,只TextView的... 正好看到和存储不管我写的.. –

+0

发布您尝试过的任何代码,我会帮助你。 –

+0

容易。你可以使用列表视图。 你有你的主视图和里面的列表视图。对于每个列表项目(每个原始项目),您需要有一个视图。所以你也需要创建它,这是你添加编辑文本和文本视图的地方。然后你需要将列表项视图充满到你的列表视图。然后你需要将数据传递给你的列表视图。例如,你想要多少物品......当用户点击该创建按钮时,你可以增加物品数量 –

回答

0

所以我写了一个快速程序来帮助你解决你的问题。这将需要调整以适应您的情况。

activity_main.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:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> 

    <Button 
     android:id="@+id/button" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Add Row"/> 

    <LinearLayout 
     android:id="@+id/linearLayout" 
     android:layout_below="@id/button" 
     android:layout_width="match_parent" 
     android:orientation="vertical" 
     android:layout_height="wrap_content"> 


    </LinearLayout> 

</RelativeLayout> 

所以在这里我们添加一个按钮来添加你想的东西,包含他们的LinearLayout。

MainActivity.java

public class MainActivity extends Activity { 

    private LinearLayout mainLinearLayout; 
    private int counter = 0; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Button button = (Button) findViewById(R.id.button); 
     mainLinearLayout = (LinearLayout) findViewById(R.id.linearLayout); 

     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       counter++; 
       LinearLayout linearLayout = new LinearLayout(MainActivity.this); 
       TableLayout.LayoutParams lp = new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1f); 
       linearLayout.setLayoutParams(lp); 
       linearLayout.setOrientation(LinearLayout.HORIZONTAL); 
       EditText editText = new EditText(MainActivity.this); 
       editText.setId(counter); 
       editText.setLayoutParams(new TableLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 0.5f)); 
       editText.setHint("Edit Text"); 
       TextView textView = new TextView(MainActivity.this); 
       textView.setId(counter); 
       textView.setLayoutParams(new TableLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 0.5f)); 
       textView.setText("TextView"); 
       linearLayout.addView(editText); 
       linearLayout.addView(textView); 
       mainLinearLayout.addView(linearLayout); 
      } 
     }); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
} 

那么这将现在要做的是在点击Add Row按钮时,它会创建一个EditText的新实例和TextView的新实例并将它们添加到横向LinearLayout,然后将该线性布局添加到在XML中创建的父线性布局。

+0

非常感谢!它的工作完美... 但是我怎么能知道每个领域的ID为了做一些计算? –

+0

你可以使用一个计数器,所以每次点击按钮时,它都会向计数器添加一个,并为EdiText和TextView分配一个id,例如“et”+ counter和“tv”+ counter,这样你就会知道根据你有什么id属于哪一行的行数。 –

+0

我编辑了代码,告诉你你需要做什么。 –

0

哦!
我添加了一个清除按钮,每个人都可以搜索它。

Button delete = (Button) findViewById(R.id.delete); 
    delete.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      mainLinearLayout.removeAllViews(); 
     } 
    });