从数据库获取数据并共享内容

时间:2016-07-28 10:19:10

标签: android

我有 ListActivity mySql 获取一些数据并将其插入 listView ...现在我有一个 shareMethod 我需要从 listView 接收这些数据进行分享..我很抱歉,但我想我必须将整个活动放在这里..

“提前谢谢”

  

ListActivity.java

public class ListActivity extends AppCompatActivity implements
AsyncResponse, AdapterView.OnItemClickListener {


public static final int NOTIFICATION_ID = 1;

final String LOG = "ListActivity";

private ArrayList<Product> productList;
private ListView lvProduct;
private FunDapter<Product> adapter;

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

    ImageLoader.getInstance().init(UILConfig.config(ListActivity.this));

    PostResponseAsyncTask taskRead = new PostResponseAsyncTask(ListActivity.this, this);

     //taskRead.execute("http://10.0.3.2/customer/product.php");
     taskRead.execute("http://symphonyrecords.6te.net/product.php");

    lvProduct = (ListView)findViewById(R.id.lvProduct);

    registerForContextMenu(lvProduct);


}//onCreate


@Override
public void processFinish(String s) {
    productList = new JsonConverter<Product>().toArrayList(s, Product.class);

    BindDictionary<Product> dict = new BindDictionary<Product>();
    dict.addStringField(R.id.tvName, new StringExtractor<Product>() {
        @Override
        public String getStringValue(Product product, int position) {
            return product.name;
        }
    });

    dict.addStringField(R.id.tvDescription, new StringExtractor<Product>() {
        @Override
        public String getStringValue(Product product, int position) {
            return product.description;
        }
    });


    dict.addStringField(R.id.tvDate, new StringExtractor<Product>() {
        @Override
        public String getStringValue(Product product, int position) {
            return "" + product.date;
        }
    });


    dict.addStringField(R.id.tvQty, new StringExtractor<Product>() {
        @Override
        public String getStringValue(Product product, int position) {
            return "" + product.qty;
        }
    });


    dict.addDynamicImageField(R.id.ivImage,
            new StringExtractor<Product>() {
                @Override
                public String getStringValue(Product product, int position) {
                    return product.image_url;
                }
            }, new DynamicImageLoader() {
                @Override
                public void loadImage(String url, ImageView imageView) {            

                    ImageLoader.getInstance().displayImage(url, imageView);
                    imageView.setPadding(0, 0, 0, 0);
                    imageView.setAdjustViewBounds(true);
                }
            });

    adapter = new FunDapter<>(
            ListActivity.this, productList, R.layout.layout_list, dict);


    lvProduct.setAdapter(adapter);
    lvProduct.setOnItemClickListener(this);
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Product selectedProduct = productList.get(position);
    Intent in = new Intent(ListActivity.this, DetailActivity.class);
    in.putExtra("product", (Serializable) selectedProduct);
    startActivity(in);


}




//This is my share method
public void shareMethod(View view1)
{

    Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND); 
    sharingIntent.setType("text/plain");
    String shareBody = "Share content body";
    sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT,shareBody);
    startActivity(Intent.createChooser(sharingIntent, "share via"));

}



}

1 个答案:

答案 0 :(得分:0)

单击列表项时,您可以形成实例String变量,并填写Product中的数据。在shareMethod中,您可以添加此字符串shareStr作为额外和共享。例如,

public class ListActivity..{
       private String mShareStr;
           ...
      <  *When the list item is clicked fill in*
     mShareStr = String.format("%s - %s for %s : %s", name, description, date, qty);  > 

      public void shareMethod(View view1)
      {
          ...
          sharingIntent.putExtra(Intent.EXTRA_TEXT, mShareStr);
      }
  }