android - 自定义 BaseAdapter EditText 单击并在按钮单击中更新数据库

标签 android baseadapter customdialog

我遇到了一个与 Custom BaseAdapter 相关的问题。首先我有自定义对话框如下 enter image description here

如您所见,这里有三个 TextView 、一个 EditText ( ) 、一个 Button(红色十字),另一个按钮当前可见性为 false ,该按钮是 Save 按钮。现在我想要的是 - 当我点击 EditText 时,相应列表的保存按钮变得可见(意味着如果我点击第三个列表的 EditTExt,那么只有第三个列表的保存应该可见)。当我更改 EditText 中的一些文本并单击“保存”按钮时,它将更新该行数据库。

基础适配器类

public class OrderListAdapter extends BaseAdapter{

EditText edt_txt_qty_name;
private static final String TAG = "OrderListAdapter";

private TreeSet mCategoryItem = new TreeSet();
String title;
String price;
private Cursor oStarterCursor;
private SQLiteDatabase dh;
private Context mContext;
private static LayoutInflater inflater = null;
private ImageView imagv;

private int starterCount;

public static int totalCount;

Button btnCTime;
EditText txtCTime;
TextView txtv;
EditText edt_orderQty_var ;
//orderQty
private final int TYPE_STATUS = 0;
private final int TYPE_ITEM = 1;
private final int TYPE_MAX_COUNT = TYPE_STATUS + 4;

Resources res;
public Handler addMenuItemHandler = new Handler() {

    @Override
    public void handleMessage(android.os.Message msg) {
        if (msg.what == 1) {
            System.out.println("inside handler");
            Toast.makeText(mContext, (String) msg.obj, Toast.LENGTH_SHORT)
                    .show();
        }
    };

};
public OrderListAdapter(Activity activity) {



    dh = DatabaseHelpereKOT.getInstance().getDb();
    inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    res = activity.getResources();
    Log.d(TAG, "cursor counts " + starterCount);
    totalCount = starterCount;
    if (totalCount <= 0) {
        System.out.println("totalCount at OrderListAdapter  if ="+ totalCount);
    } else {
        System.out.println("totalCount at OrderListAdapter  else ="+ totalCount);
    }
}

public OrderListAdapter(Activity activity, Cursor oStarterCursor) {

    this.oStarterCursor = oStarterCursor;

    starterCount = oStarterCursor.getCount();
    System.out.println("SartCount :"+starterCount);


    dh = DatabaseHelpereKOT.getInstance().getDb();
    inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    res = activity.getResources();
    Log.d(TAG, "cursor counts " + starterCount);
    totalCount = starterCount;
    if (totalCount <= 0) {
        System.out.println("totalCount at OrderListAdapter  if ="+ totalCount);
    } else {
        System.out.println("totalCount at OrderListAdapter  else ="+ totalCount);
    }
}

public int getCount() {
    // TODO Auto-generated method stub
    Log.d(TAG, "GetCount "+starterCount);
    return starterCount +1 ;
}

public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}       

public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position ;
}

@Override            
public int getItemViewType(int position) {
    return mCategoryItem.contains(position) ? TYPE_STATUS : TYPE_ITEM;

}

@Override
public int getViewTypeCount() {
    return TYPE_MAX_COUNT;
}

public void addCategoryItem(int index) {
    mCategoryItem.add(index);
    notifyDataSetChanged();
}

@SuppressLint("ResourceAsColor")
public View getView(int position, View convertView, ViewGroup parent) {
    //imagv = (ImageView)convertView.findViewById(R.id.edt_order);
    Log.d(TAG, "Position " + position);
    int _id = 0;
    int type = getItemViewType(position);
    OrderViewHolder orderViewHolder = null;
    if (convertView == null) {
        orderViewHolder = new OrderViewHolder();

        switch (type) {
        case TYPE_STATUS:

            convertView = inflater.inflate(R.layout.category_header, null);
            orderViewHolder.setTvTitle((TextView) convertView.findViewById(R.id.category));

            break;

        case TYPE_ITEM:
            convertView = inflater.inflate(R.layout.order_list_row_test, null);
            orderViewHolder.setTvTitle((TextView) convertView
                    .findViewById(R.id.orderTitle));
            orderViewHolder.setTvItemCost((TextView) convertView
                    .findViewById(R.id.orderCost));
            orderViewHolder.setEdtTxtQty((EditText) convertView
                    .findViewById(R.id.orderQty));
            orderViewHolder.setTvAmount((TextView) convertView
                    .findViewById(R.id.orderTotAmount));
            orderViewHolder.setIvDelete((ImageButton) convertView
                    .findViewById(R.id.deleteOrder));
            orderViewHolder.setIvImageView((ImageView) convertView
                    .findViewById(R.id.edt_order));

            break;
        }

        convertView.setTag(orderViewHolder);

    } else {
        orderViewHolder = (OrderViewHolder) convertView.getTag();       
    }

    if (position == 0) {          
        if (starterCount != 0) {     
            orderViewHolder.getTvTitle().setText("");
            // orderViewHolder.getTvTitle().setBackgroundDrawable(R.drawable.tab_starters_menu_on);
            orderViewHolder.getTvTitle().setTextColor(R.color.Black);
            orderViewHolder.getTvTitle().setTextSize(12);
            orderViewHolder.getTvTitle().setTypeface(Typeface.DEFAULT_BOLD);
          orderViewHolder.getTvTitle().setBackgroundResource(R.drawable.tt111);
            orderViewHolder.getTvTitle().setHeight(20); 
            orderViewHolder.getTvTitle().setWidth(100);



        }         
        else {    
            orderViewHolder.getTvTitle().setText(" ");
            orderViewHolder.getTvTitle().setBackgroundColor(Color.WHITE);   
        }

    }           

    if ((position !=0)&& (position != starterCount + 1))    

    {                       
        System.out.println(" xposition value   : "+position);         
        if (oStarterCursor.moveToPosition(position -1)) {
            if(position == starterCount+1){
                System.out.println(" xPosition is at : "+position);
            }
            String title = oStarterCursor.getString(oStarterCursor.getColumnIndex("item_name"));
            System.out.println( " value of title "+title);
            String price = oStarterCursor.getString(oStarterCursor.getColumnIndex("Item_cost"));  
            String Qty = oStarterCursor.getString(oStarterCursor.getColumnIndex("qty")); 
            String amount = oStarterCursor.getString(oStarterCursor.getColumnIndex("amount"));  

             String cost_qty_amount = price+"     "+Qty+"     "+amount+"";

            System.out.println( " amount value   "+amount);

            _id = oStarterCursor.getInt(oStarterCursor.getColumnIndex("_id"));     
            if (title != null) {
                title = title.trim();          
                orderViewHolder.getTvTitle().setText(title);   
                orderViewHolder.getTvTitle().setTextColor(R.color.black);
                orderViewHolder.getTvTitle().setTextSize(12);
                orderViewHolder.getTvTitle().setTypeface(Typeface.DEFAULT);
                orderViewHolder.getTvTitle().setGravity(Gravity.CENTER_VERTICAL);
            }

            if (price != null) {
                price = price.trim();
                orderViewHolder.getTvItemCost().setText(price + ".00    ");
                orderViewHolder.getTvItemCost().setTextColor(R.color.black);
                orderViewHolder.getTvItemCost().setTextSize(12);
                orderViewHolder.getTvItemCost().setTypeface(Typeface.DEFAULT);
                orderViewHolder.getTvItemCost().setGravity(Gravity.CENTER_VERTICAL);    

            }
            if (Qty != null) {
                Qty = Qty.trim();
                orderViewHolder.getEdtTxtQty().setText(Qty + ".00    ");
                orderViewHolder.getEdtTxtQty().setTextColor(R.color.black);
                orderViewHolder.getEdtTxtQty().setTextSize(12);
                orderViewHolder.getEdtTxtQty().setTypeface(Typeface.DEFAULT);
                orderViewHolder.getEdtTxtQty().setGravity(Gravity.CENTER_VERTICAL);    

            }
            if (amount != null) {
                amount = amount.trim();
                orderViewHolder.getTvAmount().setText(amount+ ".00    ");
                orderViewHolder.getTvAmount().setTextColor(R.color.black);
                orderViewHolder.getTvAmount().setTextSize(12);
                orderViewHolder.getTvAmount().setTypeface(Typeface.DEFAULT);
                orderViewHolder.getTvAmount().setGravity(Gravity.CENTER_VERTICAL);    

            }
            _id = oStarterCursor.getInt(oStarterCursor.getColumnIndex("_id"));
            }

           convertView.setTag(R.id.orderTitle, _id);
        if (orderViewHolder.getIvDelete() != null) {
            orderViewHolder.getIvDelete().setTag(R.id.orderTitle, _id);
        }}


    return convertView;
     }

private EditText findViewById(int edt) {
    // TODO Auto-generated method stub
    return null;
}

private void setContentView(int activityMain) {
    // TODO Auto-generated method stub

}

private Resources getResources() {
    // TODO Auto-generated method stub
    return null;
}

下面是我的 XML order_list_row_test

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<!-- icon_close -->

<LinearLayout
    android:id="@+id/relativeLayoutMainBody"
    android:layout_width="wrap_content"
    android:layout_height="38dp"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/orderTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dip"
        android:layout_toLeftOf="@id/item_cost"
        android:layout_toRightOf="@id/main_body_item_pics2"
        android:textColor="#000"
        android:textSize="20dip" />

    <TextView
        android:id="@+id/orderCost"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="4sp"
        android:background="#fff"
        android:textColor="#000"
        android:textSize="16dp" />

    <EditText
        android:id="@+id/orderQty"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:focusable="true"
        android:focusableInTouchMode="false"
        android:onClick="clickHandler" >
    </EditText>

    <TextView
        android:id="@+id/orderTotAmount"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="4sp"
        android:background="#fff"
        android:textColor="#000"
        android:textSize="16dp" />

    <ImageView
        android:id="@+id/veg_nv_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="10dp"
        android:visibility="gone" />

    <ImageView
        android:id="@+id/main_body_item_pics2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@id/veg_nv_image"
        android:src="@drawable/add_item_order"
        android:textSize="20dip"
        android:visibility="gone" />

    <TextView
        android:id="@+id/main_body_item_title_description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/item_name"
        android:layout_marginLeft="15dip"
        android:layout_toLeftOf="@id/item_cost"
        android:layout_toRightOf="@id/main_body_item_pics2"
        android:text="title description"
        android:textColor="#000"
        android:textSize="16dip"
        android:visibility="gone" />
</LinearLayout>

<ImageButton
    android:id="@+id/deleteOrder"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#fff"
    android:clickable="true"
    android:onClick="clickHandler"
    android:src="@drawable/icon_close" />



<ImageView
    android:id="@+id/save_on_id"
    android:layout_width="50dp"
    android:layout_height="match_parent"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/separator_bottom"
    android:background="#fff"
    android:clickable="true"
    android:visibility="gone"
    android:onClick="clickHandler"
    android:src="@drawable/save_on" />

  </LinearLayout>

如您所见,我创建了一个 clickHandler 方法,它将处理自定义对话框上的所有点击。现在我要发布我的 clickHandler 方法。

public void clickHandler(View v) 
{

    if (v.getId() == R.id.orderQty)
      {

    System.out.println(" edit text click");
    ImageView imageView = (ImageView)v; 
    imageView.setId(R.id.save_on_id);

    imageView.setVisibility(ImageView.VISIBLE);

      }
}

但这对我不起作用。当这部分结束时,我将朝着这个方向前进

if (v.getId() == R.id.save_on_id){
// update  database row
}

这是我的 holder 类

    public class OrderViewHolder {

TextView tvTitle;
TextView tvPrice;
ImageButton ivDelete;
TextView tvMultiplication;
ImageView imgv_edt_order;
TextView item_name;
TextView item_cost;
EditText edtxt_qty;
TextView item_total_amount;


public TextView getTvTitle() {

    return tvTitle;
}
public void setTvTitle(TextView tvTitle) {
    this.tvTitle = tvTitle;
}

public TextView getTvMultiplication() {

        return tvMultiplication;
    }
    public void setTvMultip(TextView tvMultiplication) {
        this.tvMultiplication = tvMultiplication;
    }


public ImageButton getIvDelete() {
    return ivDelete;
}
public void setIvDelete(ImageButton ivDelete) {
    this.ivDelete = ivDelete;
}

public ImageView getIvImageView() {
    return imgv_edt_order;
}
public void setIvImageView(ImageView imgv_edt_order) {
    this.imgv_edt_order = imgv_edt_order;
}


public TextView getTvItemCost() {
    return item_cost;
}
public void setTvItemCost(TextView item_cost) {
    this.item_cost = item_cost;
}

public EditText getEdtTxtQty() {
    return edtxt_qty;
}
public void setEdtTxtQty(EditText edtxt_qty) {
    this.edtxt_qty = edtxt_qty;

}

public TextView getTvAmount() {
    return item_total_amount;
}
public void setTvAmount(TextView item_total_amount) {
    this.item_total_amount = item_total_amount;
}
    }

我希望我能够向大家详细说明我的问题。虽然我知道在单线程中问三个问题有点烦人,但我真的受够了所有这些问题。非常感谢任何帮助。在此先感谢大家

最佳答案

这可能不是一个完美的解决方案,因为您在使用 ListView 时应该考虑重新设计您的代码。当您上下滚动时,ListView 会回收并重用它的 View。因此,您可以观察到,如果在一行中启用 ImageView,则另一行的 ImageView 也将可见。

public void clickHandler(View view) {
    if (view.getId() == R.id.orderQty) {
        LinearLayout layout = (LinearLayout) view.getParent().getParent();
        ImageView imageView = (ImageView) layout.findViewById(R.id.save_on_id);
        imageView.setImageResource(R.drawable.save_on);
        imageView.setVisibility(ImageView.VISIBLE);
    }
}

关于android - 自定义 BaseAdapter EditText 单击并在按钮单击中更新数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21570176/

相关文章:

android - 如何将边距设置为自动完成 TextView 适配器

java - 显示从服务器检索到的图像

android - 使用 notifyDataSetChanged 时出现 IndexOutOfBoundException

java - 如何创建一个自定义alertDialog,其中的列表绑定(bind)到onClick事件android?

当自定义对话框miss()之后调用finish()时,Android Activity 崩溃

具有透明窗口背景的Android自定义对话框破坏了我的按钮选择器

android - 如何制作循环 ScrollView ?

android - 为什么 Android 需要上下文来访问资源?

Android Mail 应用不断调整内容大小

java - 我想根据应用程序中的用户选择禁用/隐藏移动设备上的应用程序