当按下另一个片段中的按钮时,我想在一个片段中向listView添加元素。我试图自己弄清楚,但我找不到任何解决方案。
这些是我的试验:
代码:
import java.util.ArrayList;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
public class HistoryFragment extends Fragment {
ListView listView;
HistoryAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View history = inflater.inflate(R.layout.fragment_history, container, false);
ArrayList<ToSave> arrayOfData = new ArrayList<ToSave>();
adapter = new HistoryAdapter(getActivity().getBaseContext(), arrayOfData);
listView = (ListView) history.findViewById (R.id.listView1);
listView.setAdapter(adapter);
//ToSave toSave = new ToSave();
//adapter.add(toSave);
return history;
}
}
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class HistoryAdapter extends ArrayAdapter<ToSave>{
public HistoryAdapter(Context context, ArrayList<ToSave> records) {
super(context, R.layout.history_item, records);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ToSave record = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.history_item, parent, false);
}
TextView tvDate = (TextView) convertView.findViewById(R.id.textViewDate);
TextView tvStats = (TextView) convertView.findViewById(R.id.textViewStats);
// Populate the data into the template view using the data object
tvDate.setText(Integer.valueOf(record.numberDrinks).toString());
tvStats.setText(Integer.valueOf(record.numberDrinks).toString());
// Return the completed view to render on screen
return convertView;
}
}
OnFinishedParty mCallback;
public interface OnFinishedParty {
public void onFinishedParty();
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mCallback = (OnFinishedParty) activity;
}
catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement OnFinishedParty");
}
}
Button button5 = (Button) started.findViewById(R.id.button5);
button5.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mCallback.onFinishedParty();
}
});
MainActivity中的public void onFinishedParty() {
///What to put here??????
///Tried this but did not work:
ToSave toSave = new ToSave();
HistoryFragment.adapter.add(toSave);
HistoryFragment.adapter.notifyDataSetChanged();
}
答案 0 :(得分:0)
使用listView
一些公共方法制作片段,例如名称为callBack()
。
然后在带有按钮的片段中从活动中获取FragmentManager
,并使用方法findFragmentById
。然后你可以调用你的公共方法。
我的代码示例:
((MainMenuFragment)getActivity().getFragmentManager().findFragmentById(R.id.main_menu_fragment)).notificationsReaded();
我希望它足够清楚:)
答案 1 :(得分:0)
如果您知道,有多少片段,并且您在代码中手动添加它们,则应在res / values中使用文件ids.xml,示例:
<resources>
<item type="id" name="ambaja_view_reply_button" />
</resources>
另一种选择是设置标签,并按标签搜索。
如果没有,也许更好的解决方案是使用一些视图而不是标签(如果您不需要在另一个尺寸布局中重复使用它们,例如平板电脑,则不需要使用片段......)并尝试添加标签或在父布局的子项中搜索..