如何从ParseQuery适配器设置文本字段?

时间:2014-08-29 19:34:19

标签: android-fragments parse-platform

我有一个ParseQueryAdapter,它填充片段内的listview:

QueryAdapter:

public class CustomAdapter extends ParseQueryAdapter implements AdapterView.OnItemClickListener {

public CustomAdapter(Context context) {

    super(context, new ParseQueryAdapter.QueryFactory<ParseObject>() {
        public ParseQuery create() {
            ParseQuery query = new ParseQuery("Invoice");
            query.whereEqualTo("CustomerID", ParseUser.getCurrentUser());
            return query;
        }
    });
}

@Override
public View getItemView(ParseObject object, View cellview, ViewGroup parent) {
    if (cellview == null) {

        cellview = View.inflate(getContext(), R.layout.cell_layout, null);

    }

    TextView cellDate = (TextView) cellview.findViewById(R.id.dateTextView);
    TextView invoice = (TextView) cellview.findViewById(R.id.invoiceNoTextView);

    cellDate.setText(object.getString("Date"));
    invoice.setText(object.getString("InvoiceNumber"));

    return cellview;
}

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

}
}

包含Listview的片段:

public class InvoiceHistory extends Fragment {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

final   FrameLayout layout = (FrameLayout) inflater.inflate(R.layout.fragment_invoice_history, container, false);

    ListView invoicesListView = (ListView) layout.findViewById(R.id.invoiceList);

    ParseQueryAdapter adapter = new CustomAdapter(getActivity());
    invoicesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {


        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            Fragment detailView = new InvoicesFragment();
            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.placeholder, detailView);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }
    });

    invoicesListView.setAdapter(adapter);
    adapter.loadObjects();

    return layout;
}

}

这会使用查询中的相应字段填充ListView,但是当我单击其中一个单元格时,会发生片段事务,但我无法弄清楚如何使用ParseObject Keys在该新片段上设置文本字段来自查询。

这是我想要设置Textfields的片段:

public class InvoicesFragment extends Fragment {


public InvoicesFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    RelativeLayout layout = (RelativeLayout) inflater.inflate(R.layout.fragment_invoices, container, false);


    return layout;
}

Android对我来说是一个新手,所以也许这是一种错误的方式来接近它。但我想在这里做的就是让详细片段显示填充的TextViews。我不确定我在做什么。

1 个答案:

答案 0 :(得分:1)

您可以通过在DetailsFragment中设置一个构造函数来实现此目的,您可以在要显示的textview信息中传递该构造函数。在下面的示例中,数据和发票号码将传递到构造函数中。设置片段后,使用Parse对象的数据设置此片段:

invoicesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  @Override
  public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
    ParseObject object = adapter.getItem(i);
    Fragment detailView = DetailsFragment.newInstance(object.getString("Date"),
                                                    object.getString("InvoiceNumber"));
    // ...
  }
});

这里是DetailsFragment的一部分,展示了新构造函数以及如何在设置视图时使用它们:

// InvoicesFragment.java
public static class InvoicesFragment extends Fragment {

  // ...

  public static InvoicesFragment newInstance(String invoiceDate, String invoiceNumber) {
    InvoicesFragment f = new InvoicesFragment();

    Bundle args = new Bundle();
    args.putString("invoiceDate", InvoiceDate);
    args.putString("invoiceNumber", InvoiceNumber);
    f.setArguments(args);

    return f;
  }

  public String getInvoiceDate() {
    return getArguments().getString("invoiceDate", "");
  }

  public String getInvoiceNumber() {
    return getArguments().getString("invoiceNumber", "");
  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    RelativeLayout layout = (RelativeLayout) inflater.inflate(R.layout.fragment_invoices, container, false);

    // Assign some TextView, ex: dateTextView
    dateTextView.setText(getInvoiceDate());
    return layout;
  }

}