Android: Customize list view, Table view in adapter

后端 未结 1 846
生来不讨喜
生来不讨喜 2020-12-10 23:38

I created a table view in list view adapter programmatically. For that first i created a Adapter layout as :

         


        
相关标签:
1条回答
  • 2020-12-11 00:02

    In your Custom Adapter.Used a holder.

    This is my Custom Adapter

    import java.util.ArrayList;
    
    import com.samplelogin.CustomerDetails;
    import com.samplelogin.R;
    
    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.TextView;
    
    public class UserCustomAdapter extends ArrayAdapter<Customers> {
        Context context;
        int layoutResourceId;
        ArrayList<Customers> data = new ArrayList<Customers>();
    
        public UserCustomAdapter(Context context, int layoutResourceId,
                ArrayList<Customers> data) {
            super(context, layoutResourceId, data);
            this.layoutResourceId = layoutResourceId;
            this.context = context;
            this.data = data;
        }
    
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View row = convertView;
            UserHolder holder = null;
    
            if (row == null) {
                LayoutInflater inflater = ((Activity) context).getLayoutInflater();
                row = inflater.inflate(layoutResourceId, parent, false);
                holder = new UserHolder();
                holder.textName = (TextView) row.findViewById(R.id.textView1);
                holder.textAddress = (TextView) row.findViewById(R.id.textView2);
                holder.textLocation = (TextView) row.findViewById(R.id.textView3);
                holder.btnEdit = (Button) row.findViewById(R.id.button1);
    
                row.setTag(holder);
            } else {
                holder = (UserHolder) row.getTag();
            }
            Customers user = data.get(position);        
            holder.textName.setText(user.getName());
            holder.textAddress.setText(user.getAddress());
            holder.textLocation.setText("");
    
            holder.btnEdit.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Customers user = data.get(position);
                    user.setTID(user.getID());
                    user.setTName(user.getName());
                    user.setTAddress(user.getAddress());
                    user.setTTelNo(user.getTelNo());
                    user.setTMobileNo(user.getMobileNo());
                    user.setTFaxNo(user.getFaxNo());
    
                    Intent intent = new Intent(context, CustomerDetails.class);
                    context.startActivity(intent);
                }
            });
            return row;
    
        }
    
        static class UserHolder {
            TextView textName;
            TextView textAddress;
            TextView textLocation;
            Button btnEdit;
            Button btnDelete;
        }
    
    }
    

    And in my activity. I put data on it by:

    declare you variable

     // Customer
       ListView CustomerList;
        UserCustomAdapter CustomerAdapter;
        ArrayList<Customers> CustomerArray = new ArrayList<Customers>();
        Button addCustomer;
    

    Put fill up your value with a data.

            // Customer Tab
    cdb = new CustomerDB(getApplicationContext());
    int custcount = cdb.getCustomerCount();
    if (custcount > 0) {
        List<Customers> cdbL = cdb.getAllCustomers();
        int listSize = cdbL.size();
    
        for (int i = 0; i < listSize; i++) {
    
            CustomerArray.add(new Customers(cdbL.get(i).getDBName()
                    .toString(), cdbL.get(i).getDBAddress().toString(),
                    cdbL.get(i).getDBTelNo().toString(), cdbL.get(i)
                            .getDBMobileNo().toString(), cdbL.get(i)
                            .getDBFaxNo().toString(), cdbL.get(i).getDBID()
                            .toString()));
        }
    }
    

    Now, from your list. transfer you data to your custom adapter

    // set item into adapter
    CustomerAdapter = new UserCustomAdapter(MainMaintenance.this,
            R.layout.maintenancetab, CustomerArray);
    CustomerList = (ListView) findViewById(R.id.lvCustomer);
    CustomerList.setItemsCanFocus(false);
    CustomerList.setAdapter(CustomerAdapter);
    

    Here, you can click the item and do what you want. in this sample, i pass my data in a list in a public class and then i go to the next activity.

        // get on item click listener
    CustomerList.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub
            Customers user = CustomerArray.get(arg2);
            user.setTID(user.getID());
            user.setTName(user.getName());
            user.setTAddress(user.getAddress());
            user.setTTelNo(user.getTelNo());
            user.setTMobileNo(user.getMobileNo());
            user.setTFaxNo(user.getFaxNo());
    
            Intent intent = new Intent(MainMaintenance.this,
                    CustomerDetails.class);
            startActivity(intent);
        }
    });
    

    The setOnClickListener in your custom adapter is if you will put a button in you custom adapter :) if youll be confuse just tell me :)

    I hope that this sample code will give you an idea and help you.

    0 讨论(0)
提交回复
热议问题