0

I am using following method to populate a listview inside a fragment class.

private void showEmployee(){
        JSONObject jsonObject = null;
        ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String, String>>();
        try {
            jsonObject = new JSONObject(JSON_STRING);
            JSONArray result = jsonObject.getJSONArray(Config.TAG_JSON_ARRAY);

            for(int i = 0; i<result.length(); i++){
                JSONObject jo = result.getJSONObject(i);
                String cia = jo.getString(Config.TAG_CIA);
                String fn = jo.getString(Config.TAG_FN);
                String ln = jo.getString(Config.TAG_LN);
                String ad1 = jo.getString(Config.TAG_AD1);
                String ad2 = jo.getString(Config.TAG_AD2);
                String type = jo.getString(Config.TAG_TYPE);
                String city = jo.getString(Config.TAG_CITY);
                String state = jo.getString(Config.TAG_STATE);
                String zip = jo.getString(Config.TAG_ZIP);

                String phone = jo.getString(Config.TAG_PHONE);

                String ext = jo.getString(Config.TAG_EXT);

                String fromto = jo.getString(Config.TAG_FROMTO);

                Log.d("HOLA ADDRESSES", "FROM O TO: " + fromto);


                String user = jo.getString(Config.TAG_USER);


                String id_address = jo.getString(Config.TAG_ID_ADDRESS);


                HashMap<String,String> employees = new HashMap<>();
                employees.put(Config.TAG_CIA,cia);
                employees.put(Config.TAG_LN,fn);
                employees.put(Config.TAG_FN,ln);
                employees.put(Config.TAG_AD1,ad1);
                employees.put(Config.TAG_AD2,ad2);
                employees.put(Config.TAG_TYPE,type);
                employees.put(Config.TAG_CITY,city);
                employees.put(Config.TAG_STATE,state);
                employees.put(Config.TAG_ZIP,zip);
                employees.put(Config.TAG_PHONE,phone);
                employees.put(Config.TAG_EXT,ext);
                employees.put(Config.TAG_FROMTO,fromto);
                employees.put(Config.TAG_USER,user);
                employees.put(Config.TAG_ID_ADDRESS,id_address);



                list.add(employees);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        ListAdapter adapter = new SimpleAdapter(
                getActivity(), list, R.layout.addresses_list_item,
                new String[]{Config.TAG_CIA,
                        Config.TAG_FN,
                        Config.TAG_LN,
                        Config.TAG_AD1,
                        Config.TAG_AD2,
                        Config.TAG_TYPE,
                        Config.TAG_CITY,
                        Config.TAG_STATE,
                        Config.TAG_ZIP,
                        Config.TAG_PHONE,
                        Config.TAG_EXT,
                        Config.TAG_FROMTO,
                        Config.TAG_USER,
                        Config.TAG_ID_ADDRESS},
                new int[]{R.id.cia,
                        R.id.fn,
                        R.id.ln,
                        R.id.ad1,
                        R.id.ad2,
                        R.id.type,
                        R.id.city,
                        R.id.state,
                        R.id.zip,
                        R.id.phone,
                        R.id.ext,
                        R.id.fromto,
                        R.id.user,
                        R.id.id_address});

        listView.setAdapter(adapter);
    }

Now I want to include a button on every row from the list and be able to change the button text on every row depending on the value from one of the items.

I have included the button inside the listview item layout file.

I don´t know where to put the button reference on this method.

Thank you.

3
  • 1
    You have to use custom adapters for this, make custom adapter, have the reference for the button there, and have onClickListener on button Commented May 16, 2017 at 5:24
  • @AbdulKawee, would you help me creating the custom adapter based on my currend code? Commented May 16, 2017 at 6:05
  • Yes sure, i can share the code with you and you will understand it easily Commented May 16, 2017 at 6:09

5 Answers 5

1

First make a model for your data

public class DataModel {

    String title;
    String description;
    String addedby;

    public DataModel(String title, String description, String addedby) {
        this.title=title;
        this.description=description;
        this.addedby=addedby;

    }

    public String getTitle() {
        return title;
    }

    public String getDescription() {
        return description;
    }

    public String getAddedby() {
        return addedby;
    }


}

Then define custom adapter for your list

public class CustomAdapter extends ArrayAdapter implements View.OnClickListener{

private ArrayList<DataModel> dataSet;
Context mContext;

// View lookup cache
private static class ViewHolder {
    TextView title;
    TextView description;
    TextView addedBy;
Button yourButton;

}

    public CustomAdapter(ArrayList<DataModel> data, Context context) {
        super(context, R.layout.list_view_items, data);
        this.dataSet = data;
        this.mContext=context;

    }
    @Override
    public void onClick(View v) {

        int position=(Integer) v.getTag();
        Object object= getItem(position);
        DataModel dataModel=(DataModel)object;

    }

    private int lastPosition = -1;

    @Override
    public DataModel getItem(int position) {
        return super.getItem(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Get the data item for this position
        final DataModel dataModel = getItem(position);
        // Check if an existing view is being reused, otherwise inflate the view
        ViewHolder viewHolder; // view lookup cache stored in tag

        final View result;

        if (convertView == null) {

            viewHolder = new ViewHolder();
            LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = inflater.inflate(R.layout.list_view_items, parent, false);
            viewHolder.title = (TextView) convertView.findViewById(R.id.title);
            viewHolder.description = (TextView) convertView.findViewById(R.id.description);
            viewHolder.addedBy = (TextView) convertView.findViewById(R.id.addedBy);
        viewholder.yourButton = (Button) convertView.findViewById(R.id.btn);
            result=convertView;

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
            result=convertView;
        }


        lastPosition = position;

        viewHolder.title.setText(dataModel.getTitle());
        viewHolder.description.setText(dataModel.getDescription());
        viewHolder.addedBy.setText(dataModel.getAddedby());
    viewholder.yourButton.setOnClickListener(this);
        // Return the completed view to render on screen

        return convertView;
    }
}

Now the layout for your list items

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:descendantFocusability="blocksDescendants">

        <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textSize="18sp"

            android:textColor="#000000"
        android:id="@+id/title"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:textColor="#212121"
            android:fadingEdge="horizontal"
            android:maxLines="3"
            android:ellipsize="end"
            android:id="@+id/description"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/addedBy"
            android:textSize="13sp"
            android:layout_gravity="end"
            android:textColor="#212121"/>


</LinearLayout>

Now simply use these classes

  ArrayList<DataModel> dataModels;
        private static CustomAdapter adapter;

JSONObject jo = result.getJSONObject(i);
            String cia = jo.getString(Config.TAG_CIA);
            String fn = jo.getString(Config.TAG_FN);
            String ln = jo.getString(Config.TAG_LN);
 dataModels.add(new DataModel(cia , fn , ln ));
 adapter= new CustomAdapter(dataModels,getApplicationContext());

            list.setAdapter(adapter);
Sign up to request clarification or add additional context in comments.

4 Comments

Glad it helped you :)
I am getting error at method in CustomAdapter; @Override public DataModel getItem(int position) { return super.getItem(position); } Incompatible types
Abdul, I am having problems implementing your code from the last group into my code.
It might be because you are entering data from you already made hashmaps use arraylist of data type data model
0

You should create custom adapter. Try below link :-

http://android.amberfog.com/?p=296

Comments

0

The only solution for this is to have to use custom adpter. Like this

public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.RecyclerViewViewHolder>  {

    private ContactAdapter.RecyclerViewViewHolder viewHolder;
    private View view;
    Context ctx;
    List<Contact> usersList;

    public ContactAdapter(Context ctx, List<Contact> usersList) {
        this.ctx = ctx;
        this.usersList = usersList;
    }

    @Override
    public ContactAdapter.RecyclerViewViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_contact_list, parent, false);
        viewHolder = new RecyclerViewViewHolder(view);
        viewHolder.setIsRecyclable(false);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerViewViewHolder holder, int position) {
        viewHolder.name.setText(usersList.get(position).getName());
        viewHolder.avatar.setImageBitmap(AppUtil.getCircleBitmap(usersList.get(position).getAvatar()));
    }

    @Override
    public int getItemCount() {
        return usersList.size();
    }

    public class RecyclerViewViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        TextView name;
        Button avatar;

        public RecyclerViewViewHolder(View itemView) {
            super(itemView);

            name = (TextView) itemView.findViewById(R.id.name);
            avatar = (Button) itemView.findViewById(R.id.avatar);
            avatar.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            int position = getLayoutPosition();
            Intent intent = new Intent(ctx,DetailScreen.class);
            AppUtil.hideKeyBoard(ctx);
            intent.putExtra("id",usersList.get(position).getId());
            ctx.startActivity(intent);
        }
    }
}

Comments

0

you should create a custom adapter class and in getView method you should reference the button and set on click listener on that button.

btn.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v) { 
        //do something
    }
}); 

refer to this link for more details ListView with Add and Delete Buttons in each Row in android

Comments

0

Use Base Adapter for it and in getView code like this:

  @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        //use convertView recycle
        if(convertView==null){
            holder=new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.content_orders, parent, false);
            holder.textView= (TextView) convertView.findViewById(R.id.textView2);
            holder.imageView= (ImageView) convertView.findViewById(R.id.imageView2);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
    holder.clickableButton.setOnClickListener(new OnClickListener() {  

                @Override  
                public void onClick(View v) {  
                   //Code goes here

                }  
            });  

        //set text and url
        holder.textView.setText(mList.get(position).getText());
        Picasso.with(mContext).load(mList.get(position).getUrl()).resize(500,500).into(holder.imageView);

        return convertView;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.