Show next row in listview when previous row's checkbox is checked

北城余情 提交于 2019-12-31 02:20:08

问题



I have Question regarding to Listview and CheckBox
I m creating a custom ListView using following JSON data

{
            "title": "Amount Field",
            "settingItems": [
              {
                "title": "Manual Entry",
                "type": "boolean",
                "value": true
              },
              {
                "title": "Predefined Amount",
                "type": "Amount",
                "value": 0.00
              },
              {
                "title": "Drop Down Amount Menu",
                "type": "boolean",
                "value": false
              },
              {
                "title": "",
                "type": "Amount",
                "value": 0.00
              }
            ]
          }

Using above data I have managed my Listview also At the end of list i.e. at last item of listview i m showing dropDown Button to add Amount. Now What should I want
when I check Drop Down Amount Menu listItem of ListView then the last row must be visible also Vise-Versa
what should the code have to write into getView method


回答1:


you can set Layout Visible and Invisibile Using view.setVisibility(View.VISIBLE) or view.setVisibility(View.INVISIBLE)




回答2:


In this example have use the string variable that store id of selected row

MainActivity

public class MainActivity extends Activity {

 static final String TAG_NAME = "name";
 static final String TAG_ID = "id";
 static String selected = "";

ListView listView;


HashMap<String, String> map;
ArrayList<HashMap<String,String>> myList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    myList = new ArrayList<HashMap<String,String>>();       
    listView = (ListView) findViewById(R.id.listView1);
    fillListView();
}

private void fillListView() {

    myList.clear();

    map = new HashMap<String, String>();
    map.put(TAG_ID,"1");
    map.put(TAG_NAME, "Name1");
    myList.add(map);

    map = new HashMap<String, String>();
    map.put(TAG_ID,"2");
    map.put(TAG_NAME, "Name2");
    myList.add(map);

    map = new HashMap<String, String>();
    map.put(TAG_ID,"3");
    map.put(TAG_NAME, "Name3");
    myList.add(map);

    map = new HashMap<String, String>();
    map.put(TAG_ID,"4");
    map.put(TAG_NAME, "Name4");
    myList.add(map);

    listView.setAdapter(new MyListAdapter(this, R.layout.row, myList));

}
private class MyListAdapter extends BaseAdapter 
{

    private List<HashMap<String,String>> List;
    private LayoutInflater mInflater;
    private int layoutresource;

    public MyListAdapter(Context context,int resource,List<HashMap<String, String>> myList) {
        List = myList;
        mInflater = LayoutInflater.from(context);
        layoutresource = resource;
    }

    @Override
    public int getCount() {
        return List.size();
    }

    @Override
    public Object getItem(int position) {
        return List.get(position);
    }

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = mInflater.inflate(layoutresource,null);
        }    

         final View rootView = convertView.findViewById(R.id.root);

        ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView1);
        final TextView name = (TextView) convertView.findViewById(R.id.textView1);

        imageView.setImageDrawable(getResources().getDrawable(R.drawable.profile));
        name.setTextColor(Color.parseColor("#ffffff"));
        name.setText(List.get(position).get(TAG_NAME));

        if ( List.get(position).get(TAG_ID).equals(selected))
            name.setTextColor(Color.parseColor("#ff0000"));
        else
            name.setTextColor(Color.parseColor("#ffffff"));


        rootView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                selected = List.get(position).get(TAG_ID);
                fillListView();
            }
        });

        name.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                rootView.performClick();
            }
        });

        imageView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                rootView.performClick();
            }
        });

        return convertView;
    }
}

}



来源:https://stackoverflow.com/questions/15065549/show-next-row-in-listview-when-previous-rows-checkbox-is-checked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!