2016-03-30 21 views
0
public class ListViewAdapter extends BaseAdapter { 

private Context mContext; 
private LayoutInflater mLayoutInflater; 
DatabaseHelper mydb; 
int pos = 0; 
enter code here 
public ArrayList<Employee_info> mArrayList = new ArrayList<Employee_info>(); 


public ListViewAdapter(Context context,ArrayList<Employee_info> arrayList){ 
    mContext=context; 
    mArrayList=arrayList; 
    mLayoutInflater=LayoutInflater.from(mContext); 

    mydb = new DatabaseHelper(mContext); 
} 
enter code here 
@Override 
public int getCount() { 
    return mArrayList.size(); 
} 
enter code here 
@Override 
public Object getItem(int position) { 
    return mArrayList.get(position); 
} 

enter code here 
@Override 
public long getItemId(int position) { 
    return position; 
} 
enter code here 

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    ViewHolder holder=new ViewHolder(); 
    View view=convertView; 

    if(view == null){ 
     //view = mLayoutInflater.inflate(R.layout.list_view,parent,false); 
     view = mLayoutInflater.inflate(R.layout.list_view,null); 
     holder.txtName = (TextView)view.findViewById(R.id.txtName); 
     holder.txtCode = (TextView)view.findViewById(R.id.txtCode); 
     holder.txtStatus = (TextView)view.findViewById(R.id.txtStatus); 
     //// Start ////////////// 

     Button b1 = (Button) view.findViewById(R.id.button1); 
     Button b2 = (Button) view.findViewById(R.id.button2); 
     Button b3 = (Button) view.findViewById(R.id.button3); 

     b1.setTag(position); 
     final Employee_info emp_info = mArrayList.get(position); 
     b2.setTag(position); 
     b3.setTag(position); 


     final ViewHolder finalHolder = holder; 
     b3.setOnClickListener(
       new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         int x = (int) v.getTag(); 
         /* if (itemPos.contains(v)) { 

          finalHolder.txtCode.setTextColor(Color.RED); 
          finalHolder.txtName.setTextColor(Color.RED); 
          finalHolder.txtStatus.setTextColor(Color.RED); 
         } 
         else {*/ 
         if (mArrayList.get(x).getGet_status() != null) { 
          if (mArrayList.get(x).getGet_status().equals("Unpaid")) { 
           String upd1 = mArrayList.get(x).getGet_code(); 
           String upd2 = mArrayList.get(x).getGet_name(); 
           String upd3 = "Paid"; 
           mydb.UpdateData(upd1, upd2, upd3); 

           // THis code for only catch Month, year, Employee ID, Status 
           Calendar cal=Calendar.getInstance(); 
           SimpleDateFormat month_date = new SimpleDateFormat("MMM"); 
           String month_name = month_date.format(cal.getTime()); 
           int thisYear = Calendar.getInstance().get(Calendar.YEAR); 
           mydb.insertEmpSalary(upd1, month_name, String.valueOf(thisYear), upd3); 

           finalHolder.txtStatus.setTextColor(Color.GREEN); 
           ListViewAdapter.this.notifyDataSetChanged(); 

          } else { 
           String upd1 = mArrayList.get(x).getGet_code(); 
           String upd2 = mArrayList.get(x).getGet_name(); 
           String upd3 = "Unpaid"; 
           mydb.UpdateData(upd1, upd2, upd3); 
           finalHolder.txtStatus.setTextColor(Color.RED); 
          } 
         } 
        } 
        //} 
       }); 

     b1.setOnClickListener(
       new View.OnClickListener() { 
        @Override 
        public void onClick(final View position) { 
         AlertDialog.Builder builder = new AlertDialog.Builder(mContext); 
         builder.setTitle("Confirm"); 
         builder.setMessage("Are you sure?"); 
         builder.setPositiveButton("YES", new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int which) { 
           try 
           { 
            pos = (int) position.getTag(); 
            mydb.deleteData(mArrayList.get(pos).getGet_code()); 
            mArrayList.remove(pos); 
            ListViewAdapter.this.notifyDataSetChanged(); 
            Toast.makeText(mContext, mArrayList.get(pos).getGet_code() + " Employee is Delete", Toast.LENGTH_LONG).show(); 
           } 
           catch (Exception e) 
           { 
            e.printStackTrace(); 
           } 
           dialog.dismiss(); 
          } 
         }); 
         builder.setNegativeButton("NO", new DialogInterface.OnClickListener() { 

          @Override 
          public void onClick(DialogInterface dialog, int which) { 
           dialog.dismiss(); 
          } 
         }); 
         AlertDialog alert = builder.create(); 
         alert.show(); 
        } 
       }); 

     b2.setOnClickListener(
       new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         AlertDialog.Builder alert = new AlertDialog.Builder(mContext); 
         alert.setTitle("Alert Dialog With EditText"); //Set Alert dialog title here 
         alert.setMessage("Enter Your Code"); 

         pos = (int) v.getTag(); 
         LinearLayout layout = new LinearLayout(mContext); 
         layout.setOrientation(LinearLayout.VERTICAL); 

         final EditText input = new EditText(mContext); 
         input.setText(mArrayList.get(pos).getGet_code()); 
         layout.addView(input); 

         final EditText input1 = new EditText(mContext); 
         input1.setText(mArrayList.get(pos).getGet_name()); 
         layout.addView(input1); 

         final EditText input2 = new EditText(mContext); 
         input2.setText(mArrayList.get(pos).getGet_status()); 
         layout.addView(input2); 

         alert.setView(layout); 

         alert.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int whichButton) { 
           try 
           { 

            String srt = input.getEditableText().toString(); 
            Log.d("ccccccc", srt); 
            String upd1 = input.getText().toString(); 
            String upd2 = input1.getText().toString(); 
            String upd3 = input2.getText().toString(); 

            /*String upd1 = mArrayList.get(pos).getGet_code(); 
            String upd2 = mArrayList.get(pos).getGet_name(); 
            String upd3 = mArrayList.get(pos).getGet_status(); 
            Log.d("Code1",mArrayList.get(pos).getGet_code()); 
            Log.d("Name1",mArrayList.get(pos).getGet_name()); 
            Log.d("Status1",mArrayList.get(pos).getGet_status());*/ 
            Log.d("Code",upd1); 
            Log.d("Name", upd2); 
            Log.d("Status", upd3); 


            boolean isUpdate = mydb.UpdateData(upd1, upd2, upd3); 
            ListViewAdapter.this.notifyDataSetChanged(); 
            notifyDataSetChanged(); 


            if (isUpdate == true) 
            { 
             Log.e("Update Complete", String.valueOf(isUpdate)); 
             ListViewAdapter.this.notifyDataSetChanged(); 
             Toast.makeText(mContext, srt + " Employee is Updated", Toast.LENGTH_LONG).show(); 
            } 
            else 
            { 
             Toast.makeText(mContext, srt + " Employee is not Updated", Toast.LENGTH_LONG).show(); 
            } 
           } 
           catch(Exception e) 
           { 
            e.printStackTrace(); 
           } 
           dialog.dismiss(); 
          } 
         }); 
         alert.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int whichButton) { 
           dialog.dismiss(); 
          } 
         }); 
         AlertDialog alertDialog = alert.create(); 
         alertDialog.show(); 
        } 
       }); 


     view.setTag(holder); 

    } else { 
     holder=(ViewHolder)view.getTag(); 
    } 

    Employee_info employee_info = mArrayList.get(position); 
    holder.txtName.setText(employee_info.getGet_name()); 
    holder.txtCode.setText(employee_info.getGet_code()); 
    holder.txtStatus.setText(employee_info.getGet_status()); 
    return view; 
} 

private class ViewHolder{ 
    private TextView txtName,txtCode,txtStatus; 
    public Button b1; 
} 
} 
+2

yapalım Sorununuzun bazı açıklamalarını ekleyin. –

+0

Kayıt güncellendiğinde, liste görünümünü yenilemediğimde ...... Kayıt Güncelleştirdikten sonra ana sayfaya git ve tekrar liste görünümü sayfasında da git – user1785128

cevap

0

Değerleri getView içine ekleyemezsiniz. Olayı

+0

Güncelleniyor ..... 1.CustomAdapter sayfası yaa 2.MainActiity. kod gösterimde – user1785128

+0

plz yardım et bana lütfen yardım et .... henüz denemedim ama .. – user1785128