我正在尝试设置5个字符串,airport_finish,date_finish,time_finish,persons_finish,address_finish& flightnr_finish,在ListView的一个Item中,它与扩展Fragment的Class DemandFragment相关联。
有可能吗?我该怎么做?
现在所有字符串都在不同的项目中。
FinishPostFragment:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.add_post_finish:
// User chose the "Favorite" action, mark the current item
// as a favorite...
final FragmentManager fm = getActivity().getFragmentManager();
final Fragment fragment = new DemandFragment();
// put strings to listview in DemandFragment
ParseObject post = new ParseObject("Posts");
post.put("airport", airport_finish.getText().toString());
post.put("date", date_finish.getText().toString());
post.put("time", time_finish.getText().toString());
post.put("persons", persons_finish.getText().toString());
post.put("address", address_finish.getText().toString());
post.put("flightnr", flightnr_finish.getText().toString());
post.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
airport_finish.setText("");
date_finish.setText("");
time_finish.setText("");
persons_finish.setText("");
address_finish.setText("");
flightnr_finish.setText("");
ParseQuery<ParseObject> query = ParseQuery.getQuery("Posts");
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> scoreList, ParseException e) {
if (e == null) {
ArrayList arrayPost = new ArrayList<String>();
for (ParseObject j : scoreList) {
arrayPost.add(j.getString("airport"));
arrayPost.add(j.getString("date"));
arrayPost.add(j.getString("time"));
arrayPost.add(j.getString("persons"));
arrayPost.add(j.getString("address"));
arrayPost.add(j.getString("flightnr"));
}
Bundle bundle = new Bundle();
bundle.putStringArrayList("listPost", arrayPost);
fragment.setArguments(bundle);
fm.beginTransaction().replace(R.id.content_main, fragment).commit();
} else {
// error
}
}
});
}
});
return true;
DemandFragment:
public class DemandFragment extends Fragment {
ListView lv;
ArrayList yourList;
ArrayAdapter adapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_demand, container, false);
if(rootView != null){
lv = (ListView) rootView.findViewById(R.id.listDemand);
ArrayList<String> arraypost = getArguments().getStringArrayList("listPost");
Log.d("arraylist", "arraypost");
ArrayAdapter adapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, arraypost);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// clicked on item show post
}
});
}
return rootView;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
}
答案 0 :(得分:1)
您要做的是为您ListView
创建自定义项目。
然后你可以用xml:
制作自定义行<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Header"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text"/>
</LinearLayout>
看起来像这样:
在您的情况下,您将只有5个textviews ...
这就是你做的事情: https://stackoverflow.com/a/15832564/3180983
您需要扩展BaseAdapter并覆盖某些方法才能使其正常工作。