使用SharedPreference使用内容填充ListView

时间:2015-09-02 16:36:30

标签: android listview sharedpreferences adapter

基本上我在一个Activity中有ListView,我想在按ImageButton(星号)时发送行上下文。

在第一个列表的适配器类(AdapterExploreListView)中,我覆盖OnClicklistener的{​​{1}},并使用ImageButton将内容转移到第二个第二项活动清单。

在我的第二个活动中,我尝试使用第一个活动的数据生成一个新的SharedPreference。这是我的问题:

第一个清单:

第一个适配器:

ListView

第二个适配器:

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;
    }
}

CustomObject类:

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);
    }
}

1 个答案:

答案 0 :(得分:1)

它仅适用于一行:

               sharedpreferences = getSharedPreferences("Save",
           Context.MODE_PRIVATE);
    CustomObject obj=new CustomObject(sharedpreferences.getString(Title, ""),sharedpreferences.getString(Username, ""));

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

}