如何使用setOnClickListener将ListView数据从一个活动传递到另一个活动

时间:2013-12-02 15:11:19

标签: java android listview nullpointerexception onitemclick

在我的listview中有feilds,病房号码和date.i想要在点击listview时将这些数据发送到新活动。但是在这里有一个运行时错误,我不知道如何解决它。

这是我的dashboardActivity.java文件,

public class DashboardActivity extends ListActivity implements FetchDataListener{

    UserFunctions userFunctions;
    Button btnLogout;
    TextView resultView;
    private ProgressDialog dialog;
    //private String email;

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



    /**
     * Dashboard Screen for the application
     * */        
    // Check login status in database
    userFunctions = new UserFunctions();
    if(userFunctions.isUserLoggedIn(getApplicationContext())){
        setContentView(R.layout.dashboard);

        initView();   

         ListView lv = getListView();

          // listening to single list item on click
          lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

                // selected item
              TextView txtviewWard= (TextView) view.findViewById(R.id.ward_number);
              String ward = txtviewWard.getText().toString();

              TextView txtviewDate= (TextView) view.findViewById(R.id.date_time);
              String dateTime = txtviewDate.getText().toString(); 

                // Launching new Activity on selecting single List Item
                Intent i = new Intent(getApplicationContext(), SingleListItem.class);
                // sending data to new activity
                i.putExtra("ward", ward);
                i.putExtra("dateTime", dateTime);
                startActivity(i);

            }
          });

        btnLogout = (Button) findViewById(R.id.btnLogout);

        btnLogout.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                userFunctions.logoutUser(getApplicationContext());
                Intent login = new Intent(getApplicationContext(), LoginActivity.class);
                login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(login);
                // Closing dashboard screen
                finish();
            }
        });

    }else{
        // user is not logged in show login screen
        Intent login = new Intent(getApplicationContext(), LoginActivity.class);
        login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(login);
        // Closing dashboard screen
        finish();
        }
    }

private void initView() {
    // show progress dialog
    dialog = ProgressDialog.show(this, "", "Loading...");

    Intent intent = getIntent();
    String email = intent.getStringExtra(LoginActivity.EMAIL);


    String url = "http://pubbapp.comze.com/pubapp1.php?email=" + email;
    FetchDataTask task = new FetchDataTask(this);
    task.execute(url);


}


public void onFetchComplete(List<Application> data) {
    // dismiss the progress dialog
    if(dialog != null)  dialog.dismiss();
    // create new adapter
    ApplicationAdapter adapter = new ApplicationAdapter(this, data);
    // set the adapter to list
    setListAdapter(adapter);        
}


public void onFetchFailure(String msg) {
    // dismiss the progress dialog
    if(dialog != null)  dialog.dismiss();
    // show failure message
    Toast.makeText(this, msg, Toast.LENGTH_LONG).show();        
}

}

这是我的SingleListItem.java文件,

public class SingleListItem extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.single_list_item_view);

    TextView txtWard = (TextView) findViewById(R.id.ward_number);
    TextView txtDate = (TextView) findViewById(R.id.date_time);

    Intent i = getIntent();
    // getting attached intent data
    String wardNumber = i.getStringExtra("ward");
    String dateTime = i.getStringExtra("dateTime");
    // displaying selected product name
    txtWard.setText(wardNumber);
    txtDate.setText(dateTime);

}

这是我的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:id="@+id/ward_number"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="10dip"
    android:text="wardNumber"
    android:textColor="#ffffff"
    android:textSize="25dip"
    android:textStyle="bold" />

<TextView
    android:id="@+id/date_time"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Date"
    android:padding="10dip"
    android:textColor="#ffffff"
    android:textSize="20dip"
    android:textStyle="bold" />

</LinearLayout>

这是LogCat中的错误,

12-02 19:10:01.100: E/AndroidRuntime(22705): FATAL EXCEPTION: main
12-02 19:10:01.100: E/AndroidRuntime(22705): java.lang.NullPointerException
12-02 19:10:01.100: E/AndroidRuntime(22705):    at com.example.androidhive.DashboardActivity$1.onItemClick(DashboardActivity.java:59)
12-02 19:10:01.100: E/AndroidRuntime(22705):    at android.widget.AdapterView.performItemClick(AdapterView.java:284)
12-02 19:10:01.100: E/AndroidRuntime(22705):    at android.widget.ListView.performItemClick(ListView.java:3745)
12-02 19:10:01.100: E/AndroidRuntime(22705):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:1980)
12-02 19:10:01.100: E/AndroidRuntime(22705):    at android.os.Handler.handleCallback(Handler.java:587)
12-02 19:10:01.100: E/AndroidRuntime(22705):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-02 19:10:01.100: E/AndroidRuntime(22705):    at android.os.Looper.loop(Looper.java:130)
12-02 19:10:01.100: E/AndroidRuntime(22705):    at android.app.ActivityThread.main(ActivityThread.java:3691)
12-02 19:10:01.100: E/AndroidRuntime(22705):    at java.lang.reflect.Method.invokeNative(Native Method)
12-02 19:10:01.100: E/AndroidRuntime(22705):    at java.lang.reflect.Method.invoke(Method.java:507)
12-02 19:10:01.100: E/AndroidRuntime(22705):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
12-02 19:10:01.100: E/AndroidRuntime(22705):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
12-02 19:10:01.100: E/AndroidRuntime(22705):    at dalvik.system.NativeStart.main(Native Method)

请帮我修复此错误。 谢谢。

3 个答案:

答案 0 :(得分:0)

试试这个......

   ListView lv = getListView();

  // listening to single list item on click
  lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {

       //your code

    }
  });

答案 1 :(得分:0)

在第一项活动中,使用putExtras

将数据作为一个包传递
// sending data to new activity
Bundle b = new Bundle();
b.putString("ward", ward);
b.putString("dateTime", dateTime);
i.putExtras(b);   

在第二个活动中,检索包并按如下方式解析数据:

Bundle b = getIntent().getExtras();
if (b != null) 
{
    String wardNumber = b.getString("ward");
    String dateTime = b.getString("dateTime");
}

答案 2 :(得分:0)

在第二个活动中获取此类数据:

Bundle b = savedInstanceState.getExtras();
    if(b!=null)
    {
        String ward =b.getString("ward");
        String dateTime =b.getString("datetime");

    }

bundle负责getexras(),所以请使用this.it为我和我的其他朋友工作。