如何使用自定义列表适配器中的“更新图像”按钮更新数据库中的值?

鲁尼

这是我的自定义列表适配器。我想使用列表中的更新ImageButton更新表中的值。单击它时,旧值应显示在新活动中,然后必须将已编辑的值存储在数据库中。但是,我无法在onClick()方法内传递一个意图。

请给我建议一个解决方案

public class CustomListAdapter extends BaseAdapter implements ListAdapter 
     { 
      private ArrayList<String> list = new ArrayList<String>(); 
      private Context context; 
      OnItemSelectedListener onItemSelectedListener;
      public int pos;
      String pass,pass2,edit,epass;


public CustomListAdapter(List list, Context context) { 
    this.list = (ArrayList<String>) list; 
    this.context = context; 
} 


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

@Override
public Object getItem(int pos) {
    //pass2 = list.toString();
    return list.get(pos); 
} 

//@Override
//public Long getItemId(int pos) { 
//    
//    //just return 0 if your list items do not have an Id variable.
//} 

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View view = convertView;
    if (view == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        view = inflater.inflate(R.layout.layout_custom_list, null);
    } 

    //Handle TextView and display string from your list
    final TextView listItemText = (TextView)view.findViewById(R.id.list_item_string); 
    listItemText.setText(list.get(position)); 






    //Handle buttons and add onClickListeners
    ImageButton deleteBtn = (ImageButton)view.findViewById(R.id.delete_btn);
    ImageButton editBtn = (ImageButton)view.findViewById(R.id.edit_btn);
    //Button addBtn = (Button)view.findViewById(R.id.add_btn);

    deleteBtn.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) { 
            //do something
            list.remove(position);
            pass = listItemText.getText().toString();

            notifyDataSetChanged();
            pass2 = pass.substring(0,pass.indexOf(' '));
            System.out.println(pass2);
            Moneydb.delete(pass2);
        }
    }); 
    editBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v2) {
            // TODO Auto-generated method stub
            edit=listItemText.getText().toString();
            epass = listItemText.getText().toString();
            edit = epass.substring(0,epass.indexOf(' '));
            Moneydb.edit(edit);

        }
    });


    return view; 
}

protected Context getContext() {
    // TODO Auto-generated method stub
    return null;
}


@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    //return list.get(position).getId();
    return 0;
}


public void clear() {
    //CustomListAdapter collection = null;
    // TODO Auto-generated method stub
    list.clear();
    notifyDataSetChanged();
}
法罗克

我建议您ContextMenu使用两个分配和到您的列表视图MenuItemEditDelete在适配器之外编写关联的代码

或者您可以通过以下方式启动“活动”:

Intent new_intent = new Intent(v.getRootView().getContext(),edit_activity.class);
new_intent.putExtra("Key","Value");
v.getRootView().getContext().startActivity(new_intent);

我认为第一种方法是最好的;)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

自定义listview适配器中的clear()也会更新列表数据

无法更新自定义适配器中的项目

按下按钮后,更新自定义适配器中的视图

如何使用自定义基本适配器和sqlite数据库刷新Listview?

自定义适配器从数据库中检索广告但只有 7 行

如何获取JSON数据并存储在数据库中,然后显示到自定义适配器列表视图中?

如何向列表视图中的每个按钮添加一个 onclick,填充自定义数组适配器,从数组中删除该项目并更新它?

回到活动中时,使用自定义基本适配器更新Listview吗?

如何使用自定义数组适配器删除自定义列表中的特定列表项(列表行)?

如何在自定义列表视图的自定义适配器类中传递多个数组值?

使用自定义适配器时如何从Android中的ListView获取数据

如何从Firebase到ListView中检索数据(使用自定义数组适配器)

自定义GridView适配器中的TextView不会更新,但是ImageView会更新

防止将重复项添加到“自定义适配器”和数据库中

Fragment 中的自定义列表适配器

自定义列表适配器中的 getApplicationContext()

如何在自定义适配器中更改按钮单击时的按钮文本

如何更新适配器数据列表?

具有自定义适配器的listview中的Textview不会更新

自定义适配器中的复选框不会更新

在自定义适配器类中更新操作栏菜单

在自定义光标适配器中不断更新textview

如何在Android中自定义适配器?

在自定义列表数组适配器中追加数据

使用自定义适配器中的自定义ArrayList <MyOwnParseClass>在ListView中设置ParseFile图像

如何在自定义适配器中读取映射值

如何使用Hashmap为自定义列表视图创建自定义适配器

如何刷新/更新listView适配器中的数据

在内部按钮上单击以更新自定义适配器内容