我的ListView没有显示我输入的项目列表?

时间:2014-04-09 11:58:23

标签: android listview

我的android项目中有两个单独的Tabs。 1)用户输入数据 2)在具有ListView

的另一个选项卡中将数据显示为列表

数据输入运行良好.. 但当我更改选项卡以查看列表时不显示列表。运行时没有错误或异常。我提供的代码请帮帮我。

  1st Fragment


 import android.app.Activity;
 import android.app.Fragment;
 import android.content.Intent;
 import android.os.Bundle;
 import android.util.Log;

 import android.view.LayoutInflater;
 import android.view.Menu;
 import android.view.MenuItem;
 import android.view.View;
 import android.view.ViewGroup;

 import android.widget.ArrayAdapter;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.ListView;


 public class MainActivity extends Fragment {
AdapterData ad;
Button send;
EditText text;
ListView names;
ArrayAdapter<String> aa;
ListsActivity la;
int COUNT;

public MainActivity() {
    // TODO Auto-generated constructor stub
}


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



}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
View rootView=inflater.inflate(R.layout.fragment1, container,false);




return rootView;
}


@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);


    Activity temp=getActivity();

    send=(Button)temp.findViewById(R.id.button);
    text=(EditText)temp.findViewById(R.id.editText);
    names=(ListView)temp.findViewById(R.id.list);
    ad=new AdapterData();
    aa=new ArrayAdapter<String>(getActivity(), R.layout.how_to_display,ad.list);
    names.setAdapter(aa);


    send.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            if(text.getText()!=null)
            {

            ad.list.add(text.getText().toString());
            Log.i("myTag", ""+ad.list);


            }

        }
    });
}


public void onClick(View view)
{   Log.i("myTag", "See List Button Clicked");

    //Intent ii=new Intent(this,ListsActivity.class);
    //startActivity(ii);
}

  }


 2nd Fragment

 import android.R.anim;
 import android.app.Fragment;
 import android.app.ListActivity;
 import android.os.Bundle;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.view.ViewGroup.LayoutParams;
 import android.widget.ArrayAdapter;
 import android.widget.Button;
 import android.widget.ListView;
 import android.widget.TextView;

    public class ListsActivity extends Fragment{
AdapterData ad;
ListView names;

TextView view;
ArrayAdapter<String> aa;

public ListsActivity() {
    // TODO Auto-generated constructor stub
}




@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View rootView=inflater.inflate(R.layout.fragment_main, container,false);
    return rootView;
}


@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);

     ad=new AdapterData();



        names=(ListView)getActivity().findViewById(R.id.list);



aa=new ArrayAdapter<String>(getActivity(),R.layout.how_to_display,R.id.empty, ad.list);
                                aa.notifyDataSetChanged();

    names.setAdapter(aa);

}


}


Main Activity

    import android.app.ActionBar;
    import android.app.ActionBar.Tab;
    import android.app.Activity;
    import android.app.ActionBar.TabListener;
    import android.app.Fragment;
    import android.app.FragmentTransaction;
    import android.os.Bundle;
    import android.util.Log;

     public class LaunchActivity extends Activity implements TabListener {


Fragment tab1,tab2;
private String TAG="myTag";



@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    tab1=new MainActivity();
    tab2=new ListsActivity();

    ActionBar ab=getActionBar();
    ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    ab.addTab(ab.newTab().setText("Enter Items").setTabListener(this));
    ab.addTab(ab.newTab().setText("See List").setTabListener(this));
}

@Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
    // TODO Auto-generated method stub

}

@Override
public void onTabSelected(Tab arg0, FragmentTransaction arg1) {
    // TODO Auto-generated method stub

    Log.i(TAG, "onTabSelected");
    if(arg0.getText().equals("Enter Items"))
    {   Log.i(TAG, "onTabSelected");
        getFragmentManager().beginTransaction().replace(R.id.linearLayout, tab1).commit();
        Log.i(TAG, "onTabSelected");

    }

    else
    {Log.i(TAG, "onTabSelected2");
    //getFragmentManager().beginTransaction().remove(tab1);
    getFragmentManager().beginTransaction().replace(R.id.linearLayout, tab2).commit();
    Log.i(TAG, "onTabSelected2");
    }


}

@Override
public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
    // TODO Auto-generated method stub

}





   }

0 个答案:

没有答案