Android Studio从活动中更改TextView片段

时间:2016-06-22 20:14:36

标签: android android-activity fragment

我在activities文件夹中创建了一个活动,在fragment文件夹中创建了一个片段。看看我的片段是如何构建的:http://i.stack.imgur.com/jIt9W.png

有些人像我一样尝试Android : Update Tab Layout(fragments) textviews from an activity,但我不明白解决方案是什么。

如何将活动中的字符串数据发送到此片段,我想在tab2中(如图片所示)?

我尝试了很多方法而且找不到解决方案

来自我的活动

Bundle bundle = new Bundle();
bundle.putString("edttext", "From Activity");
// set Fragmentclass Arguments
Fragmentclass fragobj = new Fragmentclass();
fragobj.setArguments(bundle);

来自Fragment

String strtext = getArguments().getString("edttext");    

不起作用:(

2 个答案:

答案 0 :(得分:0)

我可以推荐另一种解决方案 - 使用名为EventBus的第三方库。它非常易于使用,对于不同的场景非常有用。 创建一个自定义事件,如下所示:

$url = 'https://xxxxxxxxxxx:xxxxxxxxxxxx@mystore.myshopify.com/admin/orders.json';
  $rows = explode(PHP_EOL, $_POST['CSV']);
  $customer = $_POST['customer'];
  $thisID = explode(',', $_POST['IDs']);
  $lines = count($rows);
  $IDs = count($thisID);
  for ($x = 0; $x < $lines; $x++) {
    $ordtot = 0;
    $cells = str_getcsv($rows[$x]);
    $line_items = array();
    $POs = array();
if ($x > 0){
    $csmpo = $cells[0];
    $orddte = $cells[1];
    $shipvia = $cells[2];
    $shipdte = $cells[3];
    $cncldte = $cells[4];
    $ohsname = $cells[5];
    $ohsaddr1 = $cells[6];
    $ohsaddr2 = $cells[7];
    $ohscity = $cells[8];
    $ohsstate = $cells[9];
    $ohszip = $cells[10];
    $ohsphone = $cells[11];
    $oddpart = $cells[12];
    $oddupc = $cells[13];
    $oddprice = $cells[14];
    $oddordqty = $cells[15];
    $ohdnote = $cells[16];
    if ($ohsphone == ""){
        $ohsphone = "1111111111";
    }
    for($a = 0; $a <= $IDs; $a++){
        if ($a == $IDs){
            $result .= "Order " . $csmpo . " NOT processed, item " . $oddpart . " not found. Please contact the webmaster.<br>";
            $error = true;
            break 1;
        }
        $myID = explode('/', $thisID[$a]);
        $mySKU = explode('_', $thisID[$a]);
        if ($mySKU[0] == $oddpart){
            $line_items[] = array('variant_id' => $myID[1], 'quantity' => $oddordqty);
            $ordtot = $ordtot + $oddordqty;
            break 1;
        }
    }
    $lines2 = $lines - $x;
    $w = $x + 1;
    for ($y = $w; $y < $lines2; $y++){
        $cells2 = str_getcsv($rows[$y]);
        if ($cells2[0] == $csmpo){
            for($b = 0; $b <= $IDs; $b++){
                if ($b == $IDs){
                    $result .= "Order " . $csmpo . " NOT processed, item " . $oddpart . " not found. Please contact the webmaster.<br>";
                    $error = true;
                    break 1;
                }
                $myID = explode('/', $thisID);
                $mySKU = explode('_', $thisID);
                if ($mySKU[0] == $cells2[12]){
                    $line_items[] = array('variant_id' => $myID[1], 'quantity' => $cells2[15]);
                    $ordtot = $ordtot + $cells2[15];
                    break 1;
                }
            }
            $x = $x + 1;
        } else {
            break 1;
        }
    }
    if ($error){continue;}
$order = array(
  "order" => array(
    "customer" => array(
      "id" => $customer
    ),
    "financial_status" => "pending",
    "shipping_address" => array(
                        "first_name" => '',
                        "address1" => $ohsaddr1,
                        "addres2" => $ohsaddr2,
                        "phone" => $ohsphone,
                        "city" => $ohscity,
                        "zip" => $ohszip,
                        "province" => $ohsstate,
                        "country" => "United States",
                        "last_name" => $ohsname,
                        "name" => $ohsname,
                        "country_code" => "US",
                        "province_code" => $ohsstate
    ),
    /*"billing_address" => array(
                        "first_name" => '',
                        "address1" => $_POST["bill_address1"],
                        "phone" => $_POST["bill_phone"],
                        "city" => $_POST["bill_city"],
                        "zip" => $_POST["bill_zip"],
                        "province" => $_POST["bill_state"],
                        "country" => "United States",
                        "last_name" => $_POST["bill_name"],
                        "name" => $_POST["bill_name"],
                        "country_code" => "US",
                        "province_code" => $_POST["bill_state"]
    ),*/
    "line_items" => $line_items,
    "note_attributes" => array(
                    array("name" => "POnumber",
                          "value" => $csmpo),
                    array("name" => "shipdate", 
                          "value" => $shipdte),
                    array("name" => "canceldate", 
                          "value" => $cncldte)
    ),
    "gateway" => "Achim Credit Terms"
)); 
$session = curl_init(); 
curl_setopt($session, CURLOPT_URL, $url);
curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8'));
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_VERBOSE, 0);
curl_setopt($session, CURLOPT_HEADER, 1);
curl_setopt($session, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($session, CURLOPT_POST, 1); 
curl_setopt($session, CURLOPT_POSTFIELDS, json_encode($order)); 
curl_setopt($session, CURLOPT_SSL_VERIFYPEER,false);
$APIresult = curl_exec($session);
curl_close($session);
usleep(500000);
$result .= "Processed Order " . $csmpo . " for a total of " . $ordtot . " items, shipping to " . $ohdcity . ", " . $ohdstate . "<br>";
}
  }
  print_r($result);

只需在您的活动中,使用您要发送到片段的文本发布此事件:

public class MyEvent {
private String text;

public MyEvent(String text) {
    this.text = text;
}

public String getText() {
    return text;
}

public void setText(String text) {
    this.text= text;
}

现在只需在片段的EventBus.getDefault().post(new MyEvent(yourTextString); 中注册eventBus:

onCreate

创建一个侦听事件的方法:

EventBus.getDefault().register(this);

答案 1 :(得分:-1)

public class ListAdapter extends BaseAdapter
{
Context context;
List<cources> valueList;

public ListAdapter(List<cources> listValue, Context context)
{
    this.context = context;
    this.valueList = listValue;
}

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

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) 
{
    ViewItem viewItem = null;
    if(convertView == null)
    {
        viewItem = new ViewItem();
        LayoutInflater layoutInfiater = (LayoutInflater)this.context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        //LayoutInflater layoutInfiater = LayoutInflater.from(context);
        convertView = layoutInfiater.inflate(R.layout.list_adapter_view, null);

        viewItem.txtNamePlanche = (TextView)convertView.findViewById(R.id.Tx_name_planche);
        viewItem.txtPriceBaget = (TextView)convertView.findViewById(R.id.Tx_price_baget);
        viewItem.txtPricePlate = (TextView)convertView.findViewById(R.id.Tx_price_plate);
        convertView.setTag(viewItem);
    }
    else
    {
        viewItem = (ViewItem) convertView.getTag();
    }

    viewItem.txtNamePlanche.setText(valueList.get(position).name);
    viewItem.txtPriceBaget.setText(valueList.get(position).price_baget);
    viewItem.txtPricePlate.setText(valueList.get(position).price_plate);
    return convertView;
}

}

和viewitem

class ViewItem
{
TextView txtNamePlanche;
TextView txtPricePlate;
TextView txtPriceBaget;
}