JERRY
JERRY

Reputation: 9

How to make custom ListView?

I want to make a re-usable Listview control from listView in which the columns can be controlled , Say I want to load 3 column list view and sometimes 2 and sometimes 4 . How can I Control the columns and rows pro-grammatically for a list view.Depending upon my json values I will display the list. Also I want to make some column editable also .This also needs to be controlled by code level

This is my code which I started :

import android.content.Context;
import android.graphics.Color;
import android.graphics.PorterDuff;

import org.json.JSONArray;
import org.json.JSONObject;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class LayoutAdvancedList extends ListView {
    private String m_name;
    private int m_editMask = 0;
    private int m_EditedRowIndex = 0;
    private int m_EditedFieldIndex = 0;
    public String getName() {
        return m_name;
    }
    public void setName(String name) {
        this.m_name = name;
    }
    public void setMaxLength(final int maxLength) {
        if (maxLength > 0) {
           // super.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxLength)});
        } else {
         //   super.setFilters(new InputFilter[]{});
        }
    }
    public void setReadOnly(final boolean readOnly) {
        super.setFocusable(!readOnly);
        super.setFocusableInTouchMode(!readOnly);
        super.setClickable(!readOnly);
        super.setLongClickable(!readOnly);
       // super.setCursorVisible(!readOnly);
    }
    public LayoutAdvancedList(Context context) {
        super(context);
        LayoutInitialize(context);
    }
    public LayoutAdvancedList(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInitialize(context);
    }
    public LayoutAdvancedList(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        LayoutInitialize(context);
    }
    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        if (enabled) {
            this.getBackground().setColorFilter(null);
        } else {
            this.getBackground().setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);
        }
    }

Upvotes: 0

Views: 58

Answers (2)

MSpeed
MSpeed

Reputation: 8280

If you have the choice, you should use a RecyclerView for this with a GridLayoutManager so you can choose the number of columns on the fly:

recyclerView.setLayoutManager(new GridLayoutManager(this, numberOfColumns));

Here's an example of how to make a RecyclerView

Upvotes: 1

iaindownie
iaindownie

Reputation: 1056

Presumably each item in your ListView is defined by child views arranged to make up 2-4 columns in each row, depending on your requirement. Simply control the presence/absence of each column programmatically using view.setVisibility(View.GONE), view.setVisibility(View.VISIBLE) or view.setVisibility(View.INVISIBLE)

Upvotes: 0

Related Questions