Custom BaseAdapter EditText click and update datab

2019-08-03 09:07发布

问题:

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

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.

Base adapter Class

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

}

Below is my 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>

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
}

This is my holder class

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

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

回答1:

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


回答2:

Try the base adapter like this way.

public class tes_adp extends BaseAdapter{

    ArrayList<HashMap<String, String>> arry;

    private LayoutInflater layoutInflater;
    Context context;
    HashMap<String, String> map;

    public tes_adp(Context context,  ArrayList<HashMap<String, String>> arry) {
        this.context = context;
        this.arry = arry;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return arry.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return arry.get(position);
    }

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        final ViewHolder holder;

        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.items, null);
            holder = new ViewHolder();

            holder.price = (TextView)convertView.findViewById(R.id.price);
            holder.name = (TextView)convertView.findViewById(R.id.name);
            holder.qty = (EditText)convertView.findViewById(R.id.qty);
            holder.delete = (Button)convertView.findViewById(R.id.delete);
            holder.save = (Button)convertView.findViewById(R.id.save);

            convertView.setTag(holder);

        } else {

            holder = (ViewHolder) convertView.getTag();

        }

        map = arry.get(position);
        holder.price.setText(map.get("price"));
        holder.name.setText(map.get("name"));
        holder.qty.setText(map.get("qty"));
        holder.save.setVisibility(View.GONE);
       holder.save.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                //Put your DB Code Here
                holder.save.setVisibility(View.GONE);
            }
        });

        holder.qty.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {
                // TODO Auto-generated method stub

                holder.save.setVisibility(View.VISIBLE);
                return false;
            }
        });


    return convertView;
}

    class ViewHolder {

        Button delete,save;
        TextView name,price,qty;

    }
}