列表视图项的{em}分享选项

时间:2017-10-11 12:16:33

标签: android

我想分享listview items.Listview包含很多项目。但是当我选择第三项或除第一项以外的任何项目进行共享时,它只占用第一项共享。我已附加我的代码。请帮助我来解决这个问题。

public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
   switch (item.getItemId()) {
     case R.id.share:
       SparseBooleanArray selectedshare = adapter.getSelectedIds();
       for (int i = (selectedshare.size() - 1); i >= 0; i--) {
           if (selectedshare.valueAt(i)) {

               val = list.get(i).getId();
               System.out.println("idddddddddd"+val);
               date = list.get(i).getDate();
               System.out.println("dateeee"+date);
               title = list.get(i).getTitle();
               System.out.println("titleeee"+title);
               content = list.get(i).getContent();
               System.out.println("contenttttt"+content);

               shareIt();

            }
        }
        return true;
        ///////for sharing///////
      default:
        return false;
    }
}





////here is the function//////

private void shareIt() {
//sharing implementation here
    Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
    sharingIntent.setType("text/plain");
    sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "All memories");
    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Date :"+date +"\n"+"Title :"+title+"\n"+"Content :"+ content);
    startActivity(Intent.createChooser(sharingIntent, "Share via"));
}

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

这是我的适配器代码: 我在适配器类中没有做任何分享。

public class ListViewAdapter extends BaseAdapter {

public ArrayList<all_memories_getter_setter>list;
Activity activity;

/////**///// multiple delete
private SparseBooleanArray mSelectedItemsIds;

public  ListViewAdapter(Activity activity, 
ArrayList<all_memories_getter_setter>list)
{
    super();
    mSelectedItemsIds= new SparseBooleanArray();
    this.activity=activity;
    this.list=list;
}

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

@Override
public Object getItem(int position) {
    return list.get(position);
}

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

/////**/////// for multiple delete
public void remove(all_memories_getter_setter object){
    list.remove(object);
    notifyDataSetChanged();
}

public List<all_memories_getter_setter> getall_memories_getter_setter(){
    return list;
}

public void toggleSelection(int position){
    selectView(position, !mSelectedItemsIds.get(position));

}
public void removeSelection(){
    mSelectedItemsIds = new SparseBooleanArray();
    notifyDataSetChanged();
}

public void selectView(int position , boolean value){
    if(value){
        mSelectedItemsIds.put(position, value);
    }else{
        mSelectedItemsIds.delete(position);
        notifyDataSetChanged();
    }

}
public int getSelectedCount(){
    return mSelectedItemsIds.size();
}
public SparseBooleanArray getSelectedIds(){
    return mSelectedItemsIds;
}

public  class Viewholder
{
    TextView txtFirst;
    TextView txtSecond;
    TextView txtThird;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Viewholder holder;
    LayoutInflater inflater = activity.getLayoutInflater();
    if (convertView == null) {
        convertView = 
inflater.inflate(R.layout.activity_all_memories_listview, null);
        holder = new Viewholder();
        holder.txtFirst = (TextView) 
convertView.findViewById(R.id.list_date);
        holder.txtSecond = (TextView) 
convertView.findViewById(R.id.list_title);
        holder.txtThird = (TextView) 
convertView.findViewById(R.id.list_content);
        convertView.setTag(holder);

    } else {
        holder = (Viewholder) convertView.getTag();
    }

    holder.txtFirst.setText("" + list.get(position).getDate());
    holder.txtSecond.setText("" + list.get(position).getTitle());
    holder.txtThird.setText("" + list.get(position).getContent());
    return  convertView;
 }

}

1 个答案:

答案 0 :(得分:0)

请按照以下步骤将listview项目内容分享到其他应用或应用内 你可以在ListviewAdapter类的getview()方法中编写自己的方法

前:

public class ListViewTestFiveActivity extends Activity{

private ListView listView;

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


    init();
}

private void init() {
    listView = (ListView)findViewById(R.id.ListViewTestFiveActivity_listView);
    ArrayList<Item> items = new ArrayList<>();

    for (int i = 0; i < 15; i++) {
        Item item = new Item();
        item.setCount(i);
        items.add(item);
    }

    MyAdapter adapter = new MyAdapter(getApplicationContext(), R.layout.single_item_listview_five, items);
    listView.setAdapter(adapter);
}

private class MyAdapter extends ArrayAdapter{

    private ArrayList<Item> items;
    private Context a_context;
    private LayoutInflater a_layoutInflater;

    public MyAdapter(Context context, int resource, ArrayList<Item> items) {
        super(context, resource, items);
        this.a_context = context;
        this.items = items;
        a_layoutInflater = LayoutInflater.from(this.a_context);
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        ViewHolder holder = null;
        if (row == null) {
            row = a_layoutInflater.inflate(R.layout.single_item_listview_five, parent, false);
            holder = new ViewHolder();
            holder.share = (Button) row.findViewById(R.id.ListViewTestFiveActivity_share_button);

            row.setTag(holder);
        } else {
            holder = (ViewHolder) row.getTag();
        }

        final Item item = items.get(position);
         holder.share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               //Here you can write your own code to share the data, item has the data which you want to share
            }
        });

        return row;
    }

    private class ViewHolder{
        Button share;

    }
}

private class Item {
    int count;

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }


   }
}

让我知道它是否适用