在按钮上单击自定义BaseAdapter的EditText点击更新数据库 [英] Custom BaseAdapter EditText click and update database in button click

查看:365
本文介绍了在按钮上单击自定义BaseAdapter的EditText点击更新数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要通过关系到自定义BaseAdapter一个问题。首先我作为定制对话框下方

I am going through a problem that is related to Custom BaseAdapter. First i have custom dialog as below

正如你可以看到为三TextView的,一个EditText上(),一键(红十字会)这里的另一个按钮,当前的能见度假的,那按钮保存按钮。现在,我想那是什么 - 当我的EditText单击保存,然后对应的列表可见的按钮(如果我对第三列表EDITTEXT那么只有第三个列表的保存应该可见单击手段)。而当我的EditText改变一些文字,然后点击保存按钮,然后它会更新数据库的该行。

As you can see there is three TextView , One EditText ( ) , One Button ( red Cross ) here another button which current visibility is false , and that button is Save button. Now what i want is that - When i click on EditText then Save Button of Corresponding List become visible ( means if i click on EditTExt of third List then only third list's Save should visible). And when i change some Text in EditText and click on SAVE button then it will update that row of database.

基本适配器类

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方法,

AS you can see that i created a clickHandler Method which will handle all click on Custom dialog. Now i ma going to post my clickHandler method.

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);

      }
}

不过,这不是为我工作。当这部分将超过然后,我会朝着这个移动

But this is not working for me . When this part will be over then i will move towards this

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

这是我的座级

    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;
}
    }

我希望我能来阐述我的问题给大家。虽然我知道,我是有点刺鼻问单线程三问,但实际上我是个从所有这些问题受够了。任何帮助真的是AP preciated。在此先感谢所有

I hope i am able to elaborate my problem to all of you. Although i know that i is little irritating to ask three question in single thread , but really i ma fed-up from all these issue. Any help is really appreciated . Thanks in advance to all

推荐答案

可能这不是一个完美的解决方案,因为你应该考虑使用时重新设计code 的ListView 的ListView 回收并重新使用它的查看 S当你滚动它向上和向下。这样一来,你可以观察一样,如果你启用的ImageView 在一行中,的ImageView 其他行会也是可见的。

May be this is not a perfect solution because you should consider redesigning your code when using ListView. ListView recycles and reuses it's Views when you scroll it up and down. As a result, you can observe like if you enable the ImageView in one row, the ImageView of other row will be also visible.

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);
    }
}

这篇关于在按钮上单击自定义BaseAdapter的EditText点击更新数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆