找到你要的答案

Q:Add an element from a listView to another listView

Q:另一个ListView添加ListView元素

I want to create an add to favorites action bar button on the news and if the button is clicked, add the news to a new list adapter in the favorites tab, can you help me please implementing the button and tell me how to create the new list?

here is my code: (displaying the clicked news)

    public class ListItemClicked extends ActionBarActivity {

    static Bundle extras;

    SectionsPagerAdapter mSectionsPagerAdapter;
    static ImageLoader imageLoader;
    static DisplayImageOptions options;




    ViewPager mViewPager;

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


        mSectionsPagerAdapter = new  SectionsPagerAdapter(getSupportFragmentManager());

        extras = getIntent().getExtras();

        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        //Setup the ImageLoader, we'll use this to display our images
        ImageLoaderConfiguration config = new  ImageLoaderConfiguration.Builder(this).build();
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(config);

        //Setup options for ImageLoader so it will handle caching for us.
        options = new DisplayImageOptions.Builder()
                .cacheInMemory()
                .cacheOnDisc()
                .build();

    }


    @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_activity2, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        return id == R.id.action_settings || super.onOptionsItemSelected(item);

    }



    public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

        @Override
        public Fragment getItem(int position) {
            return PlaceholderFragment.newInstance(position + 1);
        }

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            Locale l = Locale.getDefault();
            switch (position) {
                case 0:
                    return getString(R.string.title_section4).toUpperCase(l);
                case 1:
                    return getString(R.string.title_section5).toUpperCase(l);
            }
            return null;
        }
    }


    public static class PlaceholderFragment extends Fragment {


        private static final String ARG_SECTION_NUMBER = "section_number";


        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState)
        {

            View rootView = inflater.inflate(R.layout.fragment_list_item_clicked, container, false);


            TextView pDate = (TextView) rootView.findViewById(R.id.textView);
            pDate.setText( extras.getString("pdate") );


            TextView ptitle = (TextView) rootView.findViewById(R.id.section_label);
            ptitle.setText(extras.getString("pname"));


            TextView pnText = (TextView) rootView.findViewById(R.id.textView2);
            pnText.setText( extras.getString("pText"));




            //Setup a listener we can use to swtich from the loading indicator to the Image once it's ready
            ImageLoadingListener listener = new ImageLoadingListener(){



                @Override
                public void onLoadingStarted(String arg0, View arg1) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onLoadingCancelled(String arg0, View arg1) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onLoadingComplete(String arg0, View arg1, Bitmap arg2) {
                    // i/ndicator.setVisibility(View.INVISIBLE);
                    // iconImg.setVisibility(View.VISIBLE);
                }
                @Override
                public void onLoadingFailed(String arg0, View arg1, FailReason arg2) {
                    // TODO Auto-generated method stub

                }

            };

            //Load the image and use our options so caching is handled.
            final ImageView iconImg = (ImageView) rootView.findViewById(R.id.imageView);
            imageLoader.displayImage( extras.getString("pImage"), iconImg, options, listener);



            return rootView;
        }
    }

}

the first list adaptor with news news from list adaptor clicked the favorites tab here is the code for the listView:

public class Noutati1 extends Activity {

private SitesAdapter mAdapter;
private ListView sitesList;
private Context context;



@Override
protected void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);
    Log.i("StackSites", "OnCreate()");
    setContentView(R.layout.noutati);

    context = this;

    //Get reference to our ListView
    sitesList = (ListView)findViewById(R.id.sitesList);




  sitesList.setOnItemClickListener(new     AdapterView.OnItemClickListener() {

      @Override
      public void onItemClick(AdapterView<?> parent, View v, int pos,long id) {
      String Title = mAdapter.getItem(pos).getName();
      String nDate = mAdapter.getItem(pos).getnDate();
      String ImageUrl = mAdapter.getItem(pos).getImgUrl();
      String Text = mAdapter.getItem(pos).getnText();
      String VideoUrl = mAdapter.getItem(pos).getVideoUrl();
      Intent i = new Intent(context, ListItemClicked.class);
      //i.setData(Uri.parse(VideoUrl));

      i.putExtra("pname", Title);
      i.putExtra("pdate", nDate);
      i.putExtra("pText", Text);
      i.putExtra("pImage", ImageUrl);


      startActivity(i);

     }

  });




  /*
   * If network is available download the xml from the Internet.
   * If not then try to use the local file from last time.
   */

    if(isNetworkAvailable()){
        Log.i("StackSites", "starting download Task");
        SitesDownloadTask download = new SitesDownloadTask();
        download.execute();
    }else{
        mAdapter = new SitesAdapter(getApplicationContext(), -1,  SitesXmlPullParser.getStackSitesFromFile(Noutati1.this));
        sitesList.setAdapter(mAdapter);
    }

}

//Helper method to determine if Internet connection is available.
private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager
            = (ConnectivityManager)  getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo =  connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null &&  activeNetworkInfo.isConnected();
}

/*
 * AsyncTask that will download the xml file for us and store it locally.
 * After the download is done we'll parse the local file.
 */
private class SitesDownloadTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... arg0) {
        //Download the file
        try {
              Downloader.DownloadFromUrl("http://www.link..../news.xml",  openFileOutput("news.xml", Context.MODE_PRIVATE));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result){
        //setup our Adapter and set it to the ListView.
        mAdapter = new SitesAdapter(context, -1,  SitesXmlPullParser.getStackSitesFromFile(Noutati1.this));
        sitesList.setAdapter(mAdapter);
        Log.i("StackSites", "adapter size = " + mAdapter.getCount());
    }
}

}

我想创建一个添加到收藏夹的动作栏按钮的新闻,如果按钮被点击,添加新闻到一个新的列表适配器的收藏夹选项卡,你能帮助我请执行按钮,并告诉我如何创建新的名单?

这里是我的代码:(显示点击的新闻)

    public class ListItemClicked extends ActionBarActivity {

    static Bundle extras;

    SectionsPagerAdapter mSectionsPagerAdapter;
    static ImageLoader imageLoader;
    static DisplayImageOptions options;




    ViewPager mViewPager;

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


        mSectionsPagerAdapter = new  SectionsPagerAdapter(getSupportFragmentManager());

        extras = getIntent().getExtras();

        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        //Setup the ImageLoader, we'll use this to display our images
        ImageLoaderConfiguration config = new  ImageLoaderConfiguration.Builder(this).build();
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(config);

        //Setup options for ImageLoader so it will handle caching for us.
        options = new DisplayImageOptions.Builder()
                .cacheInMemory()
                .cacheOnDisc()
                .build();

    }


    @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_activity2, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        return id == R.id.action_settings || super.onOptionsItemSelected(item);

    }



    public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

        @Override
        public Fragment getItem(int position) {
            return PlaceholderFragment.newInstance(position + 1);
        }

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            Locale l = Locale.getDefault();
            switch (position) {
                case 0:
                    return getString(R.string.title_section4).toUpperCase(l);
                case 1:
                    return getString(R.string.title_section5).toUpperCase(l);
            }
            return null;
        }
    }


    public static class PlaceholderFragment extends Fragment {


        private static final String ARG_SECTION_NUMBER = "section_number";


        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState)
        {

            View rootView = inflater.inflate(R.layout.fragment_list_item_clicked, container, false);


            TextView pDate = (TextView) rootView.findViewById(R.id.textView);
            pDate.setText( extras.getString("pdate") );


            TextView ptitle = (TextView) rootView.findViewById(R.id.section_label);
            ptitle.setText(extras.getString("pname"));


            TextView pnText = (TextView) rootView.findViewById(R.id.textView2);
            pnText.setText( extras.getString("pText"));




            //Setup a listener we can use to swtich from the loading indicator to the Image once it's ready
            ImageLoadingListener listener = new ImageLoadingListener(){



                @Override
                public void onLoadingStarted(String arg0, View arg1) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onLoadingCancelled(String arg0, View arg1) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onLoadingComplete(String arg0, View arg1, Bitmap arg2) {
                    // i/ndicator.setVisibility(View.INVISIBLE);
                    // iconImg.setVisibility(View.VISIBLE);
                }
                @Override
                public void onLoadingFailed(String arg0, View arg1, FailReason arg2) {
                    // TODO Auto-generated method stub

                }

            };

            //Load the image and use our options so caching is handled.
            final ImageView iconImg = (ImageView) rootView.findViewById(R.id.imageView);
            imageLoader.displayImage( extras.getString("pImage"), iconImg, options, listener);



            return rootView;
        }
    }

}

the first list adaptor with news news from list adaptor clicked the favorites tab here is the code for the listView:

public class Noutati1 extends Activity {

private SitesAdapter mAdapter;
private ListView sitesList;
private Context context;



@Override
protected void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);
    Log.i("StackSites", "OnCreate()");
    setContentView(R.layout.noutati);

    context = this;

    //Get reference to our ListView
    sitesList = (ListView)findViewById(R.id.sitesList);




  sitesList.setOnItemClickListener(new     AdapterView.OnItemClickListener() {

      @Override
      public void onItemClick(AdapterView<?> parent, View v, int pos,long id) {
      String Title = mAdapter.getItem(pos).getName();
      String nDate = mAdapter.getItem(pos).getnDate();
      String ImageUrl = mAdapter.getItem(pos).getImgUrl();
      String Text = mAdapter.getItem(pos).getnText();
      String VideoUrl = mAdapter.getItem(pos).getVideoUrl();
      Intent i = new Intent(context, ListItemClicked.class);
      //i.setData(Uri.parse(VideoUrl));

      i.putExtra("pname", Title);
      i.putExtra("pdate", nDate);
      i.putExtra("pText", Text);
      i.putExtra("pImage", ImageUrl);


      startActivity(i);

     }

  });




  /*
   * If network is available download the xml from the Internet.
   * If not then try to use the local file from last time.
   */

    if(isNetworkAvailable()){
        Log.i("StackSites", "starting download Task");
        SitesDownloadTask download = new SitesDownloadTask();
        download.execute();
    }else{
        mAdapter = new SitesAdapter(getApplicationContext(), -1,  SitesXmlPullParser.getStackSitesFromFile(Noutati1.this));
        sitesList.setAdapter(mAdapter);
    }

}

//Helper method to determine if Internet connection is available.
private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager
            = (ConnectivityManager)  getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo =  connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null &&  activeNetworkInfo.isConnected();
}

/*
 * AsyncTask that will download the xml file for us and store it locally.
 * After the download is done we'll parse the local file.
 */
private class SitesDownloadTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... arg0) {
        //Download the file
        try {
              Downloader.DownloadFromUrl("http://www.link..../news.xml",  openFileOutput("news.xml", Context.MODE_PRIVATE));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result){
        //setup our Adapter and set it to the ListView.
        mAdapter = new SitesAdapter(context, -1,  SitesXmlPullParser.getStackSitesFromFile(Noutati1.this));
        sitesList.setAdapter(mAdapter);
        Log.i("StackSites", "adapter size = " + mAdapter.getCount());
    }
}

}
answer1: 回答1:

You can add button to layout of item in Recyclerview or ListView. Then set action ClickItemListener. When click add button you can store this data of row item you clicked to Shared Preferences or Sqlite. When you open tab favorite reload view and get data from data you stored.

You can add button to layout of item in Recyclerview or ListView. Then set action ClickItemListener. When click add button you can store this data of row item you clicked to Shared Preferences or Sqlite. When you open tab favorite reload view and get data from data you stored.

java  android  android-listview