使用SharedPreference更新ListView

时间:2015-09-02 21:17:24

标签: android listview sharedpreferences

我想在按下星形按钮到第二个活动列表

时从第一个列表中发送多行

我的代码仅适用于一行。例如,如果我按下第一个星,在我的SecondActivity上它将只是第一行,但如果我按下第一个和最后一个星,则只有最后一行它将显示在SecondActivity上

第一个清单:http://i.stack.imgur.com/6lD8j.png

第一个适配器:

public class AdapterExploreListView extends BaseAdapter implements Filterable {
    ArrayList<Track> tracks;
    Context context;
    private ArrayList<Track> items;
    private ArrayList<Track> orig;
    public static final String Title = "titleKey";
    public static final String Username = "usernameKey";

    public AdapterExploreListView(Context context, ArrayList<Track> tracks) {
        this.tracks = new ArrayList<>();
        this.tracks = tracks;
        this.context = context;
        getFilter();
    }

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

    @Override
    public Object getItem(int i) {
        return null;
    }

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        View rowView = view;
        // reuse views
        if (rowView == null) {
            LayoutInflater inflater = LayoutInflater.from(context);
            rowView = inflater.inflate(R.layout.row_list_explore, null);
            // configure view holder
            ViewHolder viewHolder = new ViewHolder();
            viewHolder.title = (TextView) rowView.findViewById(R.id.titleTextView);
            viewHolder.userName = (TextView) rowView.findViewById(R.id.userNameTextView);
            viewHolder.listImageView = (ImageView) rowView.findViewById(R.id.favbutton);
            rowView.setTag(viewHolder);
        }
        // fill data
        final ViewHolder holder = (ViewHolder) rowView.getTag();
        Track track = tracks.get(i);
        holder.title.setText(track.getTitle());
        holder.userName.setText(track.getUsername());
        holder.listImageView.setImageResource(R.drawable.favoritespic);
        holder.listImageView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context,ForthActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
                SharedPreferences sp = context.getSharedPreferences("Save",
                    Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sp.edit();

                String n = holder.title.getText().toString();
                String e = holder.userName.getText().toString();
                editor.putString(Title, n);
                editor.putString(Username, e);
                editor.commit();
            }
        });
        //  if(!track.getArtworkUrl().equalsIgnoreCase("null"))
        //  {
        //      ImageLoader.getInstance().displayImage(track.getArtworkUrl(), holder.listImageView);
        //  } else {
        //      ImageLoader.getInstance().displayImage(track.getAvatarUrl(), holder.listImageView);
        //  }
        return rowView;
    }

    static class ViewHolder {
        TextView title;
        TextView userName;
        ImageView listImageView;
    }

    public Filter getFilter() {
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                final FilterResults oReturn = new FilterResults();
                final ArrayList<Track> results = new ArrayList<Track>();
                if (orig == null) orig = items;
                if (constraint != null) {
                    if (orig != null && orig.size() > 0) {
                        for (final Track g : orig) {
                            if (g.getTitle().toLowerCase().contains(constraint.toString()))
                                results.add(g);
                        }
                    }
                    oReturn.values = results;
                }
                return oReturn;
            }

            @SuppressWarnings("unchecked")
            @Override
            protected void publishResults(CharSequence constraint,
                                          FilterResults results) {
                items = (ArrayList<Track>) results.values;
                notifyDataSetChanged();
            }
        };
    }

    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
        boolean notifyDataSetChanged = true;
    }
}

第二个适配器:

public class AdapterExploreListView2 extends BaseAdapter {
    private ArrayList<CustomObject> objects;
    Context context;

    public static final String Title = "titleKey";
    public static final String Username = "usernameKey";

    public AdapterExploreListView2(Context context, ArrayList<CustomObject> objects) {
        this.objects = new ArrayList<>();
        this.objects = objects;
        this.context = context;
    }

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

    public CustomObject getItem(int position) {
        return objects.get(position);
    }

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

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        View rowView = view;
        // reuse views
        if (rowView == null) {
            LayoutInflater inflater = LayoutInflater.from(context);
            rowView = inflater.inflate(R.layout.row_list_forthact, null);
            // configure view holder
            ViewHolder viewHolder = new ViewHolder();
            viewHolder.title = (TextView) rowView.findViewById(R.id.titleTV);
            viewHolder.userName = (TextView) rowView.findViewById(R.id.userNameTV);
            // viewHolder.listImageView = (ImageView) rowView.findViewById(R.id.listImageView);
            rowView.setTag(viewHolder);
        }
        // fill data
        ViewHolder holder = (ViewHolder) rowView.getTag();
        holder.title.setText(objects.get(position).getProp1());
        holder.userName.setText(objects.get(position).getProp2());
        return rowView;
    }

    static class ViewHolder {
        TextView title;
        TextView userName;
        // ImageView listImageView;
    }

    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
        boolean notifyDataSetChanged = true;
    }
}

第二项活动:

public class ForthActivity extends Activity {
    SharedPreferences sharedpreferences;
    public static final String Title = "titleKey";
    public static final String Username = "usernameKey";
    CustomObject obj;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.forthact);
        // title = (TextView) findViewById(R.id.etitle);
        // username = (TextView) findViewById(R.id.eemail);
        sharedpreferences = getSharedPreferences("Save",
           Context.MODE_PRIVATE);

        if (sharedpreferences.contains(Title)) {
            obj.setProp1(sharedpreferences.getString(Title, ""));
        }
        if (sharedpreferences.contains(Username)) {
            obj.setProp2(sharedpreferences.getString(Username, ""));
        }

        ArrayList<CustomObject> object = new ArrayList<CustomObject>();
        AdapterExploreListView2 customAdapter = new AdapterExploreListView2(ForthActivity.this, object);
        ListView lv = (ListView) findViewById(R.id.forthlistview);
        lv.setAdapter(customAdapter);
    }
}

CustomObject类:

public class CustomObject {
    private String prop1;
    private String prop2;

    public CustomObject(String prop1, String prop2) {
        this.prop1 = prop1;
        this.prop2 = prop2;
    }

    public void setProp1(String prop1) {
        this.prop1 = prop1;
    }

    public void setProp2(String prop2) {
        this.prop2 = prop2;
    }

    public String getProp1() {
        return prop1;
    }

    public String getProp2() {
        return prop2;
    }
}

0 个答案:

没有答案