在HeaderListView android

时间:2016-08-22 10:49:51

标签: android

我有一个带有editTextView和TextView的haderListView: 当我点击添加图标时,我添加一个List,我可以填充空的editTextView, 当我添加2个项目列表时出现问题:当我开始完成第一个列表时,我发现第二个列表采用相同的数据, 我如何能够分离每个清单中的项目。

这是第一个List,我把测试放在了fonction中 enter image description here

在附件列表中我发现,它从第一个中获取了数据

enter image description here

我的问题怎么做分离形式?

这是我的适配器:

public class SectionAuthorAdapter extends com.applidium.headerlistview.SectionAdapter {

    private MainActivity mainActivity;
    private Realm realm;
    private Author author = null;
    private Authors_groups authors_groups = null;
    private RealmList<Author> authors = new RealmList<>();
    int num= 0;
    private List<Authors_groups> authors_groupses;

    public SectionAuthorAdapter(MainActivity mainActivity, List<Authors_groups> authors_groupses) {

        this.mainActivity = mainActivity;
        realm = Realm.getInstance(mainActivity);


        this.authors_groupses = authors_groupses;
    }

    @Override
    public int numberOfSections() {
        return authors_groupses.size();
    }

    @Override
    public int numberOfRows(int section)
    {
        if(section == -1)
            section = 0;
        return authors_groupses.get(section).getAuthors().size();
    }

    @Override
    public View getRowView(int section, int row, View convertView, ViewGroup parent) {
        final int id ;
        //ViewHolder holder = null;
        final ViewHolder holder;

        if (convertView == null) {

            holder = new ViewHolder();
            LayoutInflater inflater = mainActivity.getLayoutInflater();
            convertView = inflater.inflate(R.layout.listview_item_agent, null);
            holder.textView1 = (TextView) convertView.findViewById(R.id.textView1);
            holder.editText1 = (EditText) convertView.findViewById(R.id.editText1);


            convertView.setTag(holder);

        } else {

            holder = (ViewHolder) convertView.getTag();
        }
        realm.beginTransaction();

        id = authors_groupses.get(section).getAuthors().get(row).getId();
        authors_groups = new Authors_groups();

         author = authors_groupses.get(section).getAuthors().get(row); 
        if (author != null) {
            holder.textView1.setText(author.getField().getLabel() + " : ");
            if (author.getData() != null && !author.getData().isEmpty())
            {
                holder.editText1.setText(author.getData());
            }
            holder.editText1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    holder.editText1.getId();
                }
            });


        authors.add(author);
        authors_groups.setAuthors(authors);
        }
        realm.commitTransaction();
        return convertView;
    }

    @Override
    public Object getRowItem(int section, int row) {
        return null;
    }

    @Override

    public View getSectionHeaderView(int section, View convertView, ViewGroup parent) {

        if (convertView == null) {

                convertView = (TextView) mainActivity.getLayoutInflater().inflate(mainActivity.getResources().getLayout(android.R.layout.simple_list_item_1), null);

        }
                section = section+1;
            ((TextView) convertView).setText("Témoin : " +section );

convertView.setBackgroundColor(mainActivity.getResources().getColor(R.color.android_green));


        return convertView;
    }
    @Override
    public boolean hasSectionHeaderView(int section) {
        return true;
    }

    @Override
    public void onRowItemClick(AdapterView<?> parent, View view, int section, int row, long id) {
        super.onRowItemClick(parent, view, section, row, id);

    }

    class ViewHolder {
        TextView textView1;
        EditText editText1;

    }
}

1 个答案:

答案 0 :(得分:0)

我认为你应该在每次设置适配器之前启动新的适配器。

public class HomeActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {
    HorizontalGridView newHorizontalGridView,popularHorizontalGridView,unreleasedHorizontalGridView;
    PopularGridElementAdapter popularAdapter;
    PopularGridElementAdapter unreleasedGridElementAdapter;
    PopularGridElementAdapter newReleasedGridElementAdapter;
    List<PopularImageModel> popularimageModelList = new ArrayList<PopularImageModel>();
    List<UnreleasedImageModel> unreleasedimageModelList = new ArrayList<UnreleasedImageModel>();
    List<NewReleasedImageModel> newreleasedimageModelList = new ArrayList<NewReleasedImageModel>();

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        newHorizontalGridView = (HorizontalGridView) findViewById(R.id.gridViewNewReleased);
        popularHorizontalGridView = (HorizontalGridView) findViewById(R.id.gridViewPopular);
        unreleasedHorizontalGridView = (HorizontalGridView) findViewById(R.id.gridViewUnreleased);
        popularAdapter = new PopularGridElementAdapter(this, popularimageModelList);
        unreleasedGridElementAdapter = new PopularGridElementAdapter(this, unreleasedimageModelList);
        newReleasedGridElementAdapter = new PopularGridElementAdapter(this, newreleasedimageModelList);

        newHorizontalGridView.setAdapter(newReleasedGridElementAdapter);
        popularHorizontalGridView.setAdapter(popularAdapter);
        unreleasedHorizontalGridView.setAdapter(unreleasedGridElementAdapter);

        parsePopularImages();
        parseUnreleasedImages();
        parseNewReleasedImages();

//        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
//        fab.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
//                        .setAction("Action", null).show();
//            }
//        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);



    }

    @Override
    public void onBackPressed () {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }



    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected (MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }


    public void parsePopularImages () {


        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(AppController.TAG_POPULAR_IMAGES, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse (JSONArray response) {

                for (int i = 0; i < response.length(); i++) {
                    try {
                        JSONObject obj = response.getJSONObject(i);

                        PopularImageModel model = new PopularImageModel();

                        model.setSinger_id(obj.getString("singer_id"));
                        model.setImage(obj.getString("image"));
                        model.setName(obj.getString("name"));

                        popularimageModelList.add(model);


                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                popularAdapter.notifyDataSetChanged();
            }
        },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse (VolleyError error) {

                    }
                });
        jsonArrayRequest.setRetryPolicy(new DefaultRetryPolicy(
                (int) TimeUnit.SECONDS.toMillis(20),
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        AppController.getInstance().addToRequestQueue(jsonArrayRequest);
    }

    public void parseUnreleasedImages () {


        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(AppController.TAG_UNRELEASED_IMAGES, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse (JSONArray response) {

                for (int i = 0; i < response.length(); i++) {
                    try {
                        JSONObject obj = response.getJSONObject(i);

                        UnreleasedImageModel model = new UnreleasedImageModel();

                        model.setSinger_id(obj.getString("singer_id"));
                        model.setImage(obj.getString("image"));
                        model.setName(obj.getString("name"));

                        unreleasedimageModelList.add(model);


                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                unreleasedGridElementAdapter.notifyDataSetChanged();
            }
        },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse (VolleyError error) {

                    }
                });
        jsonArrayRequest.setRetryPolicy(new DefaultRetryPolicy(
                (int) TimeUnit.SECONDS.toMillis(20),
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

        AppController.getInstance().addToRequestQueue(jsonArrayRequest);
    }

    public void parseNewReleasedImages () {


        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(AppController.TAG_NEW_RELEASED_IMAGES, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse (JSONArray response) {

                for (int i = 0; i < response.length(); i++) {
                    try {
                        JSONObject obj = response.getJSONObject(i);

                        NewReleasedImageModel model = new NewReleasedImageModel();

                        model.setSinger_id(obj.getString("singer_id"));
                        model.setImage(obj.getString("image"));
                        model.setName(obj.getString("name"));

                        newreleasedimageModelList.add(model);


                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                newReleasedGridElementAdapter.notifyDataSetChanged();
            }
        },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse (VolleyError error) {

                    }
                });

        jsonArrayRequest.setRetryPolicy(new DefaultRetryPolicy(
                (int) TimeUnit.SECONDS.toMillis(20),
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

        AppController.getInstance().addToRequestQueue(jsonArrayRequest);
    }
}

。:: UPDATE ::。

#itemList {
    width: calc(100% - 310px);
}
#sidePanel {
    background-color: #ff0000;
    float: right;
    padding-left: 10px;
    padding-right: 10px;
    position: fixed;
    right: 0;
    width: 300px;
}