List view isn't updating in a fragment


Last Updated:

  1. rasen58

    rasen58 New Member

    When the user clicks a button, the listview is supposed to add another value to the list and show it. When I press the button nothing happens.


    MatchesList.java
    [HIGH]

    import java.util.ArrayList;

    import com.actionbarsherlock.app.SherlockListActivity;

    import android.app.ListActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.ArrayAdapter;
    import android.widget.Button;

    public class MatchesList extends SherlockListActivity{
    ArrayList<String> listItems = new ArrayList<String>();
    ArrayAdapter<String> adapter;

    int matchNum=0;
    Button addM;

    @Override
    public void onCreate(Bundle icicle){
    super.onCreate(icicle);
    setContentView(R.layout.fragment_matches);
    adapter= new ArrayAdapter<String>(this,
    android.R.layout.simple_list_item_1,
    listItems);

    setListAdapter(adapter);
    Log.v("blah", "blah adsjfkd ");
    addM = (Button) findViewById(R.id.addMatchBtn);
    addM.setOnClickListener(new OnClickListener(){
    public void onClick(View v){
    addMatch(v);
    }
    });


    }

    public void addMatch(View view){
    matchNum += 1;
    listItems.add("Match " + matchNum);
    adapter.add("Match " + matchNum);
    adapter.notifyDataSetChanged();
    System.out.println(adapter);
    Log.d(UI_MODE_SERVICE, adapter.toString());
    Log.v("blah", "blah adsjfkd ");
    }

    }

    [/HIGH]

    MatchesFragment.java

    [HIGH]

    import com.actionbarsherlock.app.SherlockFragment;

    import android.os.Bundle;
    import android.util.Log;
    import android.view.*;

    public class MatchesFragment extends SherlockFragment{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    Log.v("test", "test message");

    return inflater.inflate(R.layout.fragment_matches, container, false);
    }
    }
    [/HIGH]

    fragment_matches.xml

    HTML:
    1.  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3.    xmlns:tools="http://schemas.android.com/tools"
    4.    android:layout_width="match_parent"
    5.    android:layout_height="match_parent"
    6.    tools:context=".MatchesList" >
    7.    
    8.     <Button
    9.        android:id="@+id/addMatchBtn"
    10.        android:layout_width="50dp"
    11.        android:layout_height="40dp"
    12.        android:text="Add a Match"
    13.      
    14.    
    15.    />
    16.    
    17.    
    18.     <ListView
    19.        android:id="@android:id/list"
    20.        android:layout_width="match_parent"
    21.        android:layout_height="fill_parent"></ListView>
    22.    
    23. </LinearLayout>
    24.  
    MainActivity.java

    [HIGH]


    import com.actionbarsherlock.app.ActionBar.Tab;
    import com.actionbarsherlock.app.ActionBar.TabListener;
    import com.actionbarsherlock.app.*;
    import com.actionbarsherlock.view.Menu;
    import com.actionbarsherlock.view.MenuInflater;

    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.app.FragmentTransaction;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;


    public class MainActivity extends SherlockFragmentActivity{

    ActionBar actionBar;
    Tab tabData;
    Tab tabComp;

    Tab tabMatches;
    Tab tabRed;
    Tab tabBlue;


    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // setContentView(R.layout.activity_main);

    actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    tabData = actionBar.newTab();
    tabData.setText("Match Data");
    tabData.setTabListener(new MyTabListener());
    actionBar.addTab(tabData);

    // tabRed.setTabListener(new TabListener<TabFragment>(this, "tab1", TabFragment.class));
    tabComp = actionBar.newTab();
    tabComp.setText("New Competition");
    tabComp.setTabListener(new MyTabListener());
    actionBar.addTab(tabComp);
    // tabBlue.setTabListener(new TabListener<TabFragment>(this, tabBlue, TabFragment.class));

    tabMatches = actionBar.newTab();
    tabMatches.setText("Matches");
    tabMatches.setTabListener(new MyTabListener());

    tabRed = actionBar.newTab();
    tabRed.setText("Red Side");
    tabRed.setTabListener(new MyTabListener());

    tabBlue = actionBar.newTab();
    tabBlue.setText("Blue Side");
    tabBlue.setTabListener(new MyTabListener());





    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu){
    MenuInflater inflater = getSupportMenuInflater();
    inflater.inflate(R.menu.activity_main, menu);
    return true;
    }



    private class MyTabListener implements ActionBar.TabListener{
    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft){
    if(tab.equals(tabData))
    {
    ScoreRedActivity frag = new ScoreRedActivity();
    ft.replace(android.R.id.content, frag);

    }
    else if(tab.equals(tabComp))//new competition
    {
    FragmentBlueScorer frag = new FragmentBlueScorer();
    ft.replace(android.R.id.content, frag);

    actionBar.removeTab(tabData);
    actionBar.removeTab(tabComp);
    actionBar.addTab(tabMatches);
    actionBar.addTab(tabRed);
    actionBar.addTab(tabBlue);

    }
    else if(tab.equals(tabMatches))
    {
    MatchesFragment frag = new MatchesFragment();
    ft.replace(android.R.id.content, frag);
    }
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft){

    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft){
    }

    }





    }
    [/HIGH]

    Advertisement
  2. jonbonazza

    jonbonazza Well-Known Member

    Moved to App Dev forums. =)

Share This Page