【问题标题】:Android: Listview custom cell - know which itemAndroid:Listview 自定义单元格 - 知道哪个项目
【发布时间】:2012-10-25 10:12:05
【问题描述】:

我有一个自定义表格单元格,它基本上有 2 个按钮、一个文本视图和一个编辑文本。每个编辑文本旁边都有一个加号和减号按钮。有效地增加/减少文本框中的数字。

我不知道如何将按钮链接到正确的文本框。 IE。当我按加号时,有一个仅适用于其行中的编辑文本框的例程。是否必须为每个文本框设置自定义 ID?

我的问题的所有文本都正确显示。

我的自定义单元格是:

<TextView
    android:id="@+id/txtOption"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_weight="0.00"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<Button
    android:id="@+id/btnPlus"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="49dp"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/txtOption"
    android:layout_weight="0.00"
    android:text="+" />

<Button
    android:id="@+id/btnMinus"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="49dp"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/btnPlus"
    android:layout_alignBottom="@+id/btnPlus"
    android:layout_toRightOf="@+id/btnPlus"
    android:layout_weight="0.00"
    android:text="-" />

<EditText
    android:id="@+id/tbAnswer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/btnMinus"
    android:layout_toRightOf="@+id/btnMinus"
    android:ems="10"
    android:inputType="number" >

    <requestFocus />
</EditText>

我的自定义单元格被调用:

    class CustomAdapter extends BaseAdapter
    {

    @Override
    public int getCount() {

        return mDescription.size();
    }

    @Override
    public Object getItem(int arg0) {

        return null;
    }

    @Override
    public long getItemId(int arg0) {

        return 0;
    }

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {

        LayoutInflater inf=getLayoutInflater();
        View v=inf.inflate(R.layout.noncriticalasset, arg2,false);

        Button btPlus=(Button)v.findViewById(R.id.btnPlus);
        Button btMinus=(Button)v.findViewById(R.id.btnMinus);

        TextView tv=(TextView)v.findViewById(R.id.txtOption);

        EditText et=(EditText)v.findViewById(R.id.tbAnswer);

        tv.setText(mDescription.get(arg0).toString()); 


        return v;
    }


}

之后我还必须整理所有文本框中的所有信息,并为每个文本框编写一个查询。我有一个提交按钮吗? (是否有创建或类似的自动数组?)

汤姆

【问题讨论】:

  • 提供整个 xml 文件,因为您已将 android:layout_weight 分配给前两个组件而不是最后一个组件。
  • 您是否尝试过为这两个按钮实现监听器?
  • 如果您为不同的行元素赋予相同的 id,而不是更改它并使其唯一

标签: java android custom-cell


【解决方案1】:

您只需要添加监听器,这里是如何为每个项目中的按钮分配监听器的示例:

@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {

  btPlus.setOnClickListener(new OnClickListener() {

     public void onClick(View v) {


     }
  });

  return v;
}

【讨论】:

    【解决方案2】:

    这是你需要的吗?

    在按钮的onclick()中使用

    @Override
        public void onClick(View v) {
    
                try {
    
                    position = listview
                            .getPositionForView((View) v.getParent()); // position is an integer variable which helps you identify which item's plus button  was clicked
                    Log.v("Position id", "" + position);
    
                    // do some manipulations 
                    notifyDataSetChanged();
    
                } catch (Exception e) {
    
                    Log.e("error", "error" + e.getMessage());
    
                }
    
                break;
    

    【讨论】:

    • 非常感谢,这很有帮助。我现在无法解决的问题是如何将位置 3 的按钮按下与位置 3 的文本框等联系起来
    • 现在更改 mDescription 的列表项在特定位置的值。 (我的意思是删除该位置的项目并在同一位置插入新值并刷新适配器)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多