Listview项目被覆盖?

时间:2016-01-19 09:24:17

标签: android listview android-fragments

我有一个主要活动,里面有两个名为taskcall的标签。在task中,有一个按钮,当按下时,转到新活动,然后数据进入主活动的viewpager中的任务片段,以填充任务片段中的listview。我已经传递了数据,但它没有动态添加,而且项目被覆盖。

Notifydata setchange也不起作用,我不知道我的错误是什么。你能帮我们帮忙吗?我发布了我的代码:

主要活动在viewpager中包含两个片段:

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

    /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide
     * fragments for each of the sections. We use a
     * {@link FragmentPagerAdapter} derivative, which will keep every
     * loaded fragment in memory. If this becomes too memory intensive, it
     * may be best to switch to a
     * {@link android.support.v4.app.FragmentStatePagerAdapter}.
     */
    private SectionsPagerAdapter mSectionsPagerAdapter;

    /**
     * The {@link ViewPager} that will host the section contents.
     */
    private ViewPager mViewPager;

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);
        //  Bundle bundle=getIntent().getExtras();
        // Intent intent=getIntent();
        //   ActivityView activityView=(ActivityView)intent.getSerializableExtra("yog");
        //     intent.putExtra("yogs",activityView);
        // Bundle bundle=new Bundle();
        // bundle.putSerializable("yogs",activityView);
        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);

    //    intent.getSerializableExtra("god");
       /* Bundle bundle=new Bundle();
        if(bundle!=null) {
            bundle.putSerializable("yog", activityView);
            Task task = new Task();
            task.setArguments(bundle);
        }*/}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */


    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the sections/tabs/pages.
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            // Return a PlaceholderFragment (defined as a static inner class below).
                switch (position){
                    case 0:
                            Task task=new Task();
                            return task;

                    case 1:
                            Calls calls=new Calls();
                            return calls;

                }
        return null;
        }

        @Override
        public int getCount() {
            // Show 3 total pages.
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {
           switch (position){
               case 0:
                   return "Task";
               case 1:
                   return "Call";

            }
        return null;
            }

        }
    }

这是视图寻呼机的任务片段,其中列出了listview 细节活动:

   import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;
public class Task extends Fragment
{
     List<ActivityView>activityViews;
    ActivityView activity;
    ActivityView activityView=new ActivityView();
    ActivityListAdapter activityListAdapter;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootview= inflater.inflate(R.layout.yog,container,false);
        ListView listview=(ListView)rootview.findViewById(R.id.listView);
        if (activityViews == null)
        {
            activityViews = new ArrayList<ActivityView>();

        }

          //  Intent intent=getActivity().getIntent();
           // ActivityView activityView=(ActivityView) intent.getSerializableExtra("yog");
            activityListAdapter = new ActivityListAdapter(getActivity(), R.id.listView, activityViews);
            listview.setAdapter(activityListAdapter);
        if(activityView!=null)
        {
            activityViews.add(activityView);
            activityListAdapter.notifyDataSetChanged();

        }
            Button btn=(Button)rootview.findViewById(R.id.button2);
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getActivity(), DetailActivity.class);
                    startActivity(intent);
                }
            });



        return rootview;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
       if(requestCode==1){
           Intent intent=getActivity().getIntent();
         activity  =(ActivityView) intent.getSerializableExtra("yog");

       }
    }
}

这是详细信息,其中数据填充任务片段中的listview:

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class DetailActivity extends AppCompatActivity {
    ActivityView activityView = new ActivityView();
    // public static String endpoint;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        final EditText editText = (EditText) findViewById(R.id.editText);
        final Spinner spinner = (Spinner) findViewById(R.id.spinner);
        final ArrayAdapter<CharSequence> arrayAdapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.yog, R.layout.support_simple_spinner_dropdown_item);
        spinner.setAdapter(arrayAdapter);
        final Button btn = (Button) findViewById(R.id.button);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                activityView.setDescription(editText.getText().toString());
                activityView.setStatus(spinner.getSelectedItem().toString());
                Intent intent=new Intent(DetailActivity.this,MainActivity.class);
                intent.putExtra("yog",activityView);
                intent.putExtra("god",activityView);
                startActivityForResult(intent,1);


            }
        });
    }
}

这是我的自定义列表适配器:

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.List;

/**
 * Created by 4264 on 08-01-2016.
 */
public class ActivityListAdapter  extends ArrayAdapter<ActivityView> {

    private List<ActivityView> listData;
    private LayoutInflater layoutInflater;

    public ActivityListAdapter(Context context, int resource, List<ActivityView> listdata) {
        super(context, resource, listdata);
        this.listData = listdata;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public void add(ActivityView object)
    {
        super.add(object);

    }


    @Override
    public int getCount()
    {

        return listData.size();
    }

    @Override
    public ActivityView getItem(int position)
    {

        return listData.get(position);
    }


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

    public View getView(int position, View convertView, ViewGroup parent)
    {
        ViewHolder comp_list_holder;

        View mview = convertView;
        if (mview == null) {
            convertView = layoutInflater.inflate(R.layout.list_activity, null);
            comp_list_holder  = new ViewHolder();
            comp_list_holder .competitors_name = (TextView) convertView.findViewById(R.id.companygrp);
            comp_list_holder .competitors_website = (TextView) convertView.findViewById(R.id.companyname);
            convertView.setTag(comp_list_holder );

        }
         else
         {
            comp_list_holder  = (ViewHolder) convertView.getTag();
         }

    comp_list_holder.competitors_name.setText("Description " + listData.get(position).getDescription());
    comp_list_holder.competitors_website.setText("Status "+ listData.get(position).getStatus());
    convertView.setBackgroundColor(Color.parseColor("#DCDCDCDC"));
    return convertView;



    }



    public static class ViewHolder

    {
        public TextView competitors_name;
        public TextView competitors_website;


    }


}

1 个答案:

答案 0 :(得分:0)

在任务片段中:

Intent intent = new Intent(getActivity(), DetailActivity.class);
                startActivityForResult(intent, 1);

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == 1) {
            // Make sure the request was successful
            if (resultCode == Activity.RESULT_OK){
                activityViews.add((ActivityView)data.getParcelableExtra("yog"));
                activityListAdapter.notifyDataSetChanged();

            }
        }

在btn.setOnclickListener中的DetailActivity中:

Intent intent=new Intent();
intent.putExtra("yog",activityView);
intent.putExtra("god",activityView);
setResult(RESULT_OK,intent);
finish();//finishing activity