我在一个片段上显示了一些数据,每次点击一个按钮,它都应该添加到第二个片段中的listView。 出于某种原因,当我点击按钮时,listView不会改变。
fragment1:
nextButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
HistoryFragment.addToHistory( myObject );
}});
HistoryFragment(listView):
public class HistoryFragment extends ListFragment
{
private static List<Password> historyList = new ArrayList<MyObject>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
historyList = myManager.getMyObjects ();
}
public static void addToHistory( MyObject myObject )
{
historyList.add( myObject );
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_history, container, false);
MyObjectAdapter adapter = new MyObjectAdapter( historyList );
setListAdapter(adapter);
return view;
}
....
....
....
private class MyObjectAdapter extends BaseAdapter
{
private final ArrayList<MyObject> data;
public MyObjectAdapter( List<MyObject> historyList )
{
data = new ArrayList<MyObject>();
data.addAll( historyList );
}
@Override
public int getCount()
{
return data.size();
}
@Override
public MyObject getItem( int position )
{
return data.get( position );
}
@Override
public long getItemId(int position) {
// TODO implement you own logic with ID
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final View result;
notifyDataSetChanged();
if (convertView == null)
{
result = LayoutInflater.from(parent.getContext()).inflate(R.layout.history_list_item, parent, false);
}
else
{
result = convertView;
}
MyObject item = getItem( position );
( (TextView) result.findViewById(R.id.historyObjectTextView) ).setText(item.toString());
return result;
}
}
片段main(1):
public class MainFragment extends Fragment
{
Map<String,MyObject> objects = new HashMap<String,MyObject>();
List< MyObject > objectsList = new ArrayList< MyObject >();
TextView nextDisplay;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
objects = myManager.getInstance().getObjects();
objectsList = MyObject.getObjectsList();
}
@Override
public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState )
{
View view = inflater.inflate(R.layout.fragment_main, container, false);
nextDisplay = ( TextView ) view.findViewById( R.id.displayTextView );
nextDisplay.setText( objectsList.get( 0 ).getObjectString() );
Button nextButton = ( Button ) view.findViewById( R.id.nextObjectButton );
nextButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
if( objectsList.size() > 0 )
{
objects.get(objectsList.get( 0 ).getObjectString()).setTried( true );
long currentTime = System.currentTimeMillis();
objects.get(objectsList.get( 0 ).getObjectString()).setTime( currentTime );
HistoryFragment.addToHistory( objectsList.get( 0 ) );
objectsList.remove( 0 );
if( objectsList.size() > 0 && !objectsList.get( 0 ).getTried() )
{
nextCodeDisplay.setText( objectsList.get( 0 ).getObjectString() );
}
}
}});
return view;
}
答案 0 :(得分:1)
喜欢这个。
private List<Password> historyList = new ArrayList<MyObject>();
private MyObjectAdapter adapter
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_history, container, false);
adapter = new MyObjectAdapter( historyList );
setListAdapter(adapter);
return view;
}
private static List<Password> historyList = new ArrayList<MyObject>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
historyList = myManager.getMyObjects ();
}
public void addToHistory( MyObject myObject )
{
historyList.add( myObject );
adapter.notifyDataSetChanged();
}
答案 1 :(得分:0)
尝试在OnClickListener上调用notifyDataSetChanged()
:
nextButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
HistoryFragment.addToHistory( myObject );
notifyDataSetChanged();
}});