带有onItemClick的列表根本没有响应

时间:2015-08-11 06:55:30

标签: android listview background-process onitemclicklistener

使用带有onItemClick的listview的简单程序在按下列表中的项目时没有响应。

有人可以告诉我如何检查这些。我是android和编程的新手。 下面是我的程序类(ReadComments.java)的一部分,它有listview:

package com.lm.vciwhereabout;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.view.KeyEvent;
import android.content.DialogInterface;
import android.app.AlertDialog;
import android.widget.TextView;
import android.content.Context;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.view.LayoutInflater;
import android.widget.CursorAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Spinner;


public class ReadComments extends ListActivity {
    public final static String ID_EXTRA="com.lm.vciwhereabout._ID";
    // Progress Dialog
    private ProgressDialog pDialog;

    // php read comments script

    // localhost :
    // testing on your device
    // put your local ip instead, on windows, run CMD > ipconfig
    // or in mac's terminal type ifconfig and look for the ip under en0 or en1
    // private static final String READ_COMMENTS_URL =
    // "http://xxx.xxx.x.x:1234/webservice/comments.php";

    // testing on Emulator:
    private static final String READ_COMMENTS_URL = "http://192.168.0.245/vciwhereabout/wabout.php";

    // testing from a real server:
    // private static final String READ_COMMENTS_URL =
    // "http://www.mybringback.com/webservice/comments.php";

    // JSON IDS:
    private static final String TAG_SUCCESS = "success";
    // private static final String TAG_TITLE = "title";
    private static final String TAG_POSTS = "posts";
    private static final String TAG_POST_ID = "post_id";
    private static final String TAG_USERNAME = "username";
    private static final String TAG_FDATE = "fdate";
    private static final String TAG_TDATE = "tdate";
    private static final String TAG_FTIME = "ftime";
    private static final String TAG_TTIME = "ttime";
    private static final String TAG_CUSTBRANCH = "custbranch";
    private static final String TAG_CUSTNAME = "custname";
    private static final String TAG_CUSTADDR = "custaddr";
    private static final String TAG_CUSTCITY = "custcity";
    private static final String TAG_NOTE = "note";


    // it's important to note that the message is both in the parent branch of
    // our JSON tree that displays a "Post Available" or a "No Post Available"
    // message,
    // and there is also a message for each individual post, listed under the
    // "posts"
    // category, that displays what the user typed as their message.

    // An array of all of our comments
    private JSONArray mComments = null;
    // manages all of our comments in a list.
    private ArrayList<HashMap<String, String>> mCommentList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // note that use read_comments.xml instead of our single_post.xml
        setContentView(R.layout.read_comments);
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        // loading the comments via AsyncTask
        new LoadComments().execute();
    }

    public void addComment(View v) {
        Intent i = new Intent(ReadComments.this, AddComment.class);
        startActivity(i);
    }

    /**
     * Retrieves recent post data from the server.
     */
    public void updateJSONdata() {

        // Instantiate the arraylist to contain all the JSON data.
        // we are going to use a bunch of key-value pairs, referring
        // to the json element name, and the content, for example,
        // message it the tag, and "I'm awesome" as the content..

        mCommentList = new ArrayList<HashMap<String, String>>();

        // Bro, it's time to power up the J parser
        JSONParser jParser = new JSONParser();
        // Feed the beast our comments url, and it spits us
        // back a JSON object. Boo-yeah Jerome.
        JSONObject json = jParser.getJSONFromUrl(READ_COMMENTS_URL);

        // when parsing JSON stuff, we should probably
        // try to catch any exceptions:
        try {

            // I know I said we would check if "Posts were Avail." (success==1)
            // before we tried to read the individual posts, but I lied...
            // mComments will tell us how many "posts" or comments are
            // available
            mComments = json.getJSONArray(TAG_POSTS);

            // looping through all posts according to the json object returned
            for (int i = 0; i < mComments.length(); i++) {
                JSONObject c = mComments.getJSONObject(i);

                // gets the content of each tag

                String username = c.getString(TAG_USERNAME);

                String fdate = c.getString(TAG_FDATE);
                String tdate = c.getString(TAG_TDATE);
                String ftime = c.getString(TAG_FTIME);
                String ttime = c.getString(TAG_TTIME);
                String custbranch = c.getString(TAG_CUSTBRANCH);
                String custname = c.getString(TAG_CUSTNAME);
                String custaddr = c.getString(TAG_CUSTADDR);
                String custcity = c.getString(TAG_CUSTCITY);
                String note = c.getString(TAG_NOTE);


                // creating new HashMap
                HashMap<String, String> map = new HashMap<String, String>();


                map.put(TAG_USERNAME, username);
                map.put(TAG_FDATE, fdate);
                map.put(TAG_TDATE, tdate);
                map.put(TAG_FTIME, ftime);
                map.put(TAG_TTIME, ttime);
                map.put(TAG_CUSTBRANCH, custbranch);
                map.put(TAG_CUSTNAME, custname);
                map.put(TAG_CUSTADDR, custaddr);
                map.put(TAG_CUSTCITY, custcity);
                map.put(TAG_NOTE, note);



                // adding HashList to ArrayList
                mCommentList.add(map);

                // annndddd, our JSON data is up to date same with our array
                // list
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    /**
     * Inserts the parsed data into the listview.
     */


    private void updateList() {
        // For a ListActivity we need to set the List Adapter, and in order to do
        //that, we need to create a ListAdapter.  This SimpleAdapter,
        //will utilize our updated Hashmapped ArrayList, 
        //use our single_post xml template for each item in our list,
        //and place the appropriate info from the list to the
        //correct GUI id.  Order is important here.
        ListAdapter adapter = new SimpleAdapter

                (this, mCommentList,
                R.layout.single_post, new String[] { 
                        TAG_USERNAME,
                        TAG_FDATE,
                        TAG_TDATE,
                        TAG_FTIME,
                        TAG_TTIME,
                        TAG_CUSTBRANCH,
                        TAG_CUSTNAME, 
                        TAG_CUSTADDR,
                        TAG_CUSTCITY,
                        TAG_NOTE }, 

                        new int[] { R.id.username, 
                R.id.fdate, R.id.tdate, 
                R.id.ftime, R.id.ttime, R.id.custbranch, R.id.custname, 
                       R.id.custaddr, R.id.custcity, R.id.note });

        setListAdapter(adapter);

        ListView lv = getListView();    
        lv.setOnItemClickListener(new OnItemClickListener() {


            @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {

                    // getting values from selected ListItem
                    String username = ((TextView) view.findViewById(R.id.username))
                            .getText().toString();
                    String fdate = ((TextView) view.findViewById(R.id.fdate))
                            .getText().toString();
                    String tdate = ((TextView) view.findViewById(R.id.tdate))
                            .getText().toString();
                    String ftime = ((TextView) view.findViewById(R.id.ftime))
                            .getText().toString();
                    String ttime = ((TextView) view.findViewById(R.id.ttime))
                            .getText().toString();
                    String custbranch = ((TextView) view.findViewById(R.id.custbranch))
                            .getText().toString();
                    String custname = ((TextView) view.findViewById(R.id.custname))
                            .getText().toString();
                    String custaddr = ((TextView) view.findViewById(R.id.custaddr))
                            .getText().toString();
                    String custcity = ((TextView) view.findViewById(R.id.custcity))
                            .getText().toString();
                    String note = ((TextView) view.findViewById(R.id.note))
                            .getText().toString();

                    // Starting single contact activity

                    Intent in = new Intent(getApplicationContext(),
                            SingleContactActivity.class);

                    in.putExtra(TAG_USERNAME, username);
                    in.putExtra(TAG_FDATE, fdate);
                    in.putExtra(TAG_TDATE, tdate);
                    in.putExtra(TAG_FTIME, ftime);
                    in.putExtra(TAG_TTIME, ttime);
                    in.putExtra(TAG_CUSTBRANCH, custbranch);
                    in.putExtra(TAG_CUSTNAME, custname);
                    in.putExtra(TAG_CUSTADDR, custaddr);
                    in.putExtra(TAG_CUSTBRANCH, custbranch);
                    in.putExtra(TAG_CUSTCITY, custcity);
                    in.putExtra(TAG_NOTE, note);

                    startActivity(in);

                }     
            }); 
    }

它应该在下面启动SingleContactActivity类,但从未启动过:

package com.lm.vciwhereabout;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
// import info.androidhive.jsonparsing.R;

public class SingleContactActivity extends Activity {

    // JSON node keys
    private static final String TAG_USERNAME = "username";
    private static final String TAG_FDATE = "fdate";
    private static final String TAG_TDATE = "tdate";
    private static final String TAG_FTIME = "ftime";
    private static final String TAG_TTIME = "ttime";
    private static final String TAG_CUSTBRANCH = "custbranch";
    private static final String TAG_CUSTNAME = "custname";
    private static final String TAG_CUSTADDR = "custaddr";
    private static final String TAG_CUSTCITY = "custcity";
    private static final String TAG_NOTE = "note";


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

        // getting intent data
        Intent in = getIntent();

        // Get JSON values from previous intent
        String username = in.getStringExtra(TAG_USERNAME);
        String fdate = in.getStringExtra(TAG_FDATE);
        String tdate = in.getStringExtra(TAG_TDATE);
        String ftime = in.getStringExtra(TAG_FTIME);
        String ttime = in.getStringExtra(TAG_TTIME);
        String custbranch = in.getStringExtra(TAG_CUSTBRANCH);
        String custname = in.getStringExtra(TAG_CUSTNAME);
        String custaddr = in.getStringExtra(TAG_CUSTADDR);
        String custcity = in.getStringExtra(TAG_CUSTCITY);
        String note = in.getStringExtra(TAG_NOTE);

        // Displaying all values on the screen
        TextView lblUsername = (TextView) findViewById(R.id.username_label);
        TextView lblFdate = (TextView) findViewById(R.id.fdate_label);
        TextView lblTdate = (TextView) findViewById(R.id.tdate_label);
        TextView lblFtime = (TextView) findViewById(R.id.ftime_label);
        TextView lblTtime = (TextView) findViewById(R.id.ttime_label);
        TextView lblCustbranch = (TextView) findViewById(R.id.custbranch_label);
        TextView lblCustname = (TextView) findViewById(R.id.custname_label);
        TextView lblCustaddr = (TextView) findViewById(R.id.custaddr_label);
        TextView lblCustcity = (TextView) findViewById(R.id.custcity_label);
        TextView lblNote = (TextView) findViewById(R.id.note_label);

        lblUsername.setText(username);
        lblFdate.setText(fdate);
        lblTdate.setText(tdate);
        lblFtime.setText(ftime);
        lblTtime.setText(ttime);
        lblCustbranch.setText(custbranch);
        lblCustname.setText(custname);
        lblCustaddr.setText(custaddr);
        lblCustcity.setText(custcity);
        lblNote.setText(note);

    }
}

我已将这些类放在清单上,如下所示:

<activity
            android:name="com.lm.vciwhereabout.ReadComments"
            android:label="@string/app_name"  >
        </activity>
        <activity
            android:name="com.lm.vciwhereabout.QueriesMenu"
            android:label="@string/app_name"  >
        </activity>
        <activity
            android:name="com.lm.vciwhereabout.querywabyname"
            android:label="@string/app_name"  >
        </activity>

        <activity
            android:name="com.lm.vciwhereabout.querynameentry"
            android:label="@string/app_name"  >
        </activity>

        <activity
            android:name="com.lm.vciwhereabout.SingleContactActivity"
            android:label="@string/app_name"  >
        </activity>

这是主类(ReadComments.java)的布局:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    android:id="@+id/widget60"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="3dp"
        android:background="@drawable/post_border_style"
        android:orientation="vertical" >    


<LinearLayout
            android:id="@+id/box"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="2dp"
            android:background="@drawable/post_background_style"
            android:orientation="horizontal" >

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/logo3s" />   


<LinearLayout
                android:id="@+id/box"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="2dp"
                android:orientation="vertical"
                android:padding="5dp" >


<TableLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:stretchColumns="1"
    >

    <TableRow>
        <TextView android:text="Username  :" />
        <TextView android:id="@+id/username" 
    />
    </TableRow>

    <TableRow>
        <TextView android:text="Fromdate   :" />
        <TextView android:id="@+id/fdate" 
    />
    </TableRow>

    <TableRow>
        <TextView android:text="Fromtime :" />
        <TextView android:id="@+id/ftime" />
    </TableRow>

     <TableRow>
        <TextView android:text="Cust/Br.Name :" />
        <TextView android:id="@+id/custname" />
    </TableRow>


</TableLayout>  

</LinearLayout>
</LinearLayout>
</LinearLayout>

</ScrollView>

以下是应该启动的新意图的布局(singlecontactactivity.java)。名称为activity_single_contact.xml。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:padding="10dp">
  <!-- Name Label -->
  <TextView android:id="@+id/username_label"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="25dip"
            android:textStyle="bold"
            android:paddingTop="10dip"
            android:paddingBottom="10dip"
            android:textColor="#43bd00"/>
  <!-- From Date  Label -->
  <TextView android:id="@+id/fdate_label"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="#acacac"/>

  <!-- To Date Label -->

  <TextView android:id="@+id/tdate_label"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            />
  <TextView android:id="@+id/ftime_label"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            />          
  <TextView android:id="@+id/ttime_label"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            />          
  <TextView android:id="@+id/custbranch_label"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            />          
  <TextView android:id="@+id/custname_label"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            />          
  <TextView android:id="@+id/custaddr_label"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            />          
  <TextView android:id="@+id/custcity_label"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            />                      
  <TextView android:id="@+id/note_label"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            />              

</LinearLayout>      

非常感谢任何想法。非常感谢。

我已经更改了代码并在onCreate methot中放置了getListView()。setOnItemClickListener,但仍然获得了NPE。

新更新的ReadComments.java如下:

public class ReadComments<updateList> extends ListActivity {

    private ProgressDialog pDialog;
    private static final String READ_COMMENTS_URL = "http://192.168.0.245/vciwhereabout/wabout.php";
    private static final String TAG_MCOMMENTS = "mcomments";
    private static final String TAG_POST_ID = "post_id";
    private static final String TAG_POSTS = "posts";
    private static final String TAG_USERNAME = "username";
    private static final String TAG_FDATE = "fdate";
    private static final String TAG_TDATE = "tdate";
    private static final String TAG_FTIME = "ftime";
    private static final String TAG_TTIME = "ttime";
    private static final String TAG_CUSTBRANCH = "custbranch";
    private static final String TAG_CUSTNAME = "custname";
    private static final String TAG_CUSTADDR = "custaddr";
    private static final String TAG_CUSTCITY = "custcity";
    private static final String TAG_NOTE = "note";


    JSONArray mComments = null;

    ArrayList<HashMap<String, String>> mCommentList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.read_comments);

        mCommentList = new ArrayList<HashMap<String, String>>();

        ListView lv = getListView();    
        lv.setOnItemClickListener(new OnItemClickListener() {


                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                     String post_id = ((TextView) view.findViewById(R.id.post_id))
                                .getText().toString();

                    String username = ((TextView) view.findViewById(R.id.username))
                            .getText().toString();
                    String fdate = ((TextView) view.findViewById(R.id.fdate))
                            .getText().toString();
                    String tdate = ((TextView) view.findViewById(R.id.tdate))
                            .getText().toString();
                    String ftime = ((TextView) view.findViewById(R.id.ftime))
                            .getText().toString();
                    String ttime = ((TextView) view.findViewById(R.id.ttime))
                            .getText().toString();
                    String custbranch = ((TextView) view.findViewById(R.id.custbranch))
                            .getText().toString();
                    String custname = ((TextView) view.findViewById(R.id.custname))
                            .getText().toString();
                    String custaddr = ((TextView) view.findViewById(R.id.custaddr))
                            .getText().toString();
                    String custcity = ((TextView) view.findViewById(R.id.custcity))
                            .getText().toString();
                    String note = ((TextView) view.findViewById(R.id.note))
                            .getText().toString();

                    Intent in = new Intent(ReadComments.this, SingleContactActivity.class);
                    in.putExtra(TAG_POST_ID, post_id);
                    in.putExtra(TAG_USERNAME, username); 
                    in.putExtra(TAG_USERNAME, username);
                    in.putExtra(TAG_FDATE, fdate);
                    in.putExtra(TAG_TDATE, tdate);
                    in.putExtra(TAG_FTIME, ftime);
                    in.putExtra(TAG_TTIME, ttime);
                    in.putExtra(TAG_CUSTBRANCH, custbranch);
                    in.putExtra(TAG_CUSTNAME, custname);
                    in.putExtra(TAG_CUSTADDR, custaddr);
                    in.putExtra(TAG_CUSTBRANCH, custbranch);
                    in.putExtra(TAG_CUSTCITY, custcity);
                    in.putExtra(TAG_NOTE, note);

//                  in.putExtra(ID_EXTRA, String.valueOf(id));

                    startActivity(in);

                }       

          });
    }

    @Override
    protected void onResume() {
        super.onResume();
        // loading the comments via AsyncTask

        new LoadComments().execute();
    }



    public void addComment(View v) {
        Intent i = new Intent(ReadComments.this, AddComment.class);
        startActivity(i);
    }

    /**
     * Retrieves recent post data from the server.
     */
    public void updateJSONdata() {

        mCommentList = new ArrayList<HashMap<String, String>>();

        JSONParser jParser = new JSONParser();

        JSONObject json = jParser.getJSONFromUrl(READ_COMMENTS_URL);

        try {


            mComments = json.getJSONArray(TAG_POSTS);

            for (int i = 0; i < mComments.length(); i++) {
                JSONObject c = mComments.getJSONObject(i);

                String post_id = c.getString(TAG_POST_ID);

                String username = c.getString(TAG_USERNAME);

                String fdate = c.getString(TAG_FDATE);
                String tdate = c.getString(TAG_TDATE);
                String ftime = c.getString(TAG_FTIME);
                String ttime = c.getString(TAG_TTIME);
                String custbranch = c.getString(TAG_CUSTBRANCH);
                String custname = c.getString(TAG_CUSTNAME);
                String custaddr = c.getString(TAG_CUSTADDR);
                String custcity = c.getString(TAG_CUSTCITY);
                String note = c.getString(TAG_NOTE);
            HashMap<String, String> map = new HashMap<String, String>();

                map.put(TAG_POST_ID, post_id);
                map.put(TAG_USERNAME, username);
                map.put(TAG_FDATE, fdate);
                map.put(TAG_TDATE, tdate);
                map.put(TAG_FTIME, ftime);
                map.put(TAG_TTIME, ttime);
                map.put(TAG_CUSTBRANCH, custbranch);
                map.put(TAG_CUSTNAME, custname);
                map.put(TAG_CUSTADDR, custaddr);
                map.put(TAG_CUSTCITY, custcity);
                map.put(TAG_NOTE, note);

                mCommentList.add(map);

            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    /**
     * Inserts the parsed data into the listview.
     */
    private void updateList() {

        ListAdapter adapter = new SimpleAdapter

                (this, mCommentList,
                R.layout.single_post, new String[] {

                        TAG_POST_ID,
                        TAG_USERNAME,
                        TAG_FDATE,
                        TAG_TDATE,
                        TAG_FTIME,
                        TAG_TTIME,
                        TAG_CUSTBRANCH,
                        TAG_CUSTNAME, 
                        TAG_CUSTADDR,
                        TAG_CUSTCITY,
                        TAG_NOTE }, 

                        new int[] { R.id.post_id, R.id.username,
                R.id.fdate, R.id.tdate, 
                R.id.ftime, R.id.ttime, R.id.custbranch, R.id.custname, 
                       R.id.custaddr, R.id.custcity, R.id.note });

        setListAdapter(adapter);

      }  



    public class LoadComments extends AsyncTask<Void, Void, Boolean> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(ReadComments.this);
            pDialog.setMessage("Loading Where About...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        @Override
        protected Boolean doInBackground(Void... arg0) {

            updateJSONdata();
            return null;

        }

        @Override
        protected void onPostExecute(Boolean result) {

            super.onPostExecute(result);

            pDialog.dismiss();
            updateList();
        }
    }

    public void queriesMenu(View v) {
        Intent i = new Intent(ReadComments.this, QueriesMenu.class);
        startActivity(i);
    }


    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            dialogOnBackPress();

            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    protected void dialogOnBackPress() {

          AlertDialog.Builder builder = new AlertDialog.Builder(ReadComments.this);
          builder.setMessage("Are you sure you want to exit?")
               .setCancelable(false)
               .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                   ReadComments.this.finish();
                   }
               })
               .setNegativeButton("No", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                   });
            AlertDialog alert = builder.create();               
               alert.show();

    }

}

现在,当点击其中一个项目时,会有一个“响应”,但在此之后就会获得NPE。

3 个答案:

答案 0 :(得分:0)

您在updateList()中编写了onItemClick侦听器。你需要在方法之外编写它然后才能完美地工作。

lv.setOnItemClickListener

答案 1 :(得分:0)

您可以尝试使用以下代码获取所选列表项的值,如下所示:

String userName = parent.getSelectedItemAtPosition(position).toString;

希望这有帮助

答案 2 :(得分:0)

尝试下面的任何一项都可行 1.在onCreate()方法中调用updateList()。 打电话   getListView()。setOnItemClickListener(new OnItemClickListener(){

        @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                // getting values from selected ListItem
                String username = ((TextView) view.findViewById(R.id.username))
                        .getText().toString();
                String fdate = ((TextView) view.findViewById(R.id.fdate))
                        .getText().toString();
                String tdate = ((TextView) view.findViewById(R.id.tdate))
                        .getText().toString();
                String ftime = ((TextView) view.findViewById(R.id.ftime))
                        .getText().toString();
                String ttime = ((TextView) view.findViewById(R.id.ttime))
                        .getText().toString();
                String custbranch = ((TextView) view.findViewById(R.id.custbranch))
                        .getText().toString();
                String custname = ((TextView) view.findViewById(R.id.custname))
                        .getText().toString();
                String custaddr = ((TextView) view.findViewById(R.id.custaddr))
                        .getText().toString();
                String custcity = ((TextView) view.findViewById(R.id.custcity))
                        .getText().toString();
                String note = ((TextView) view.findViewById(R.id.note))
                        .getText().toString();

                // Starting single contact activity

                Intent in = new Intent(getApplicationContext(),
                        SingleContactActivity.class);

                in.putExtra(TAG_USERNAME, username);
                in.putExtra(TAG_FDATE, fdate);
                in.putExtra(TAG_TDATE, tdate);
                in.putExtra(TAG_FTIME, ftime);
                in.putExtra(TAG_TTIME, ttime);
                in.putExtra(TAG_CUSTBRANCH, custbranch);
                in.putExtra(TAG_CUSTNAME, custname);
                in.putExtra(TAG_CUSTADDR, custaddr);
                in.putExtra(TAG_CUSTBRANCH, custbranch);
                in.putExtra(TAG_CUSTCITY, custcity);
                in.putExtra(TAG_NOTE, note);

                startActivity(in);

            }     
        }); 
onCreate()方法中的