将Android listview字符串值传递给另一个活动

将Android listview字符串值传递给另一个活动,android,listview,select,android-listview,Android,Listview,Select,Android Listview,我的android应用程序有问题,我使用JSON解析从mysql获取数据 当我选择一个listview项目并将其传递给另一个活动时,它会采用一个随机值,而不是我选择的值 这是我在列表视图活动中的代码 public class Outlets extends ListActivity{ // Progress Dialog private ProgressDialog pDialog; // testing on Emulator: private static fi

我的android应用程序有问题,我使用JSON解析从mysql获取数据

当我选择一个
listview
项目并将其传递给另一个活动时,它会采用一个随机值,而不是我选择的值

这是我在
列表视图
活动中的代码

public class Outlets extends ListActivity{

// Progress Dialog
    private ProgressDialog pDialog;


    // testing on Emulator:
    private static final String READ_OUTLETS_URL = "http://10.0.2.2:8081/bfc_webservice/outlet_list.php";



    // JSON IDS:
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_OUTLET_NAME = "outlet_name";
    private static final String TAG_SPARKLING_CLASSIFICATION = "sparkling_classification";
    private static final String TAG_ID = "id";
    private static final String TAG_POSTS= "posts";
    private static final String TAG_SPARKLING_CHANNEL = "sparkling_channel";



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


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



}

@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(ShowComments.this, PostComment.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.

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

    // Instantiating the json parser 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_OUTLETS_URL);

    //Catcing Exceptions
    try {
        //Checking the amount of data rows.
        mOutlets = json.getJSONArray(TAG_POSTS);

        // looping through the database
        for (int i = 0; i < mOutlets.length(); i++) {
            JSONObject c = mOutlets.getJSONObject(i);

            // gets the content of each tag
            String outletname = c.getString(TAG_OUTLET_NAME);
            String spark_channel = c.getString(TAG_SPARKLING_CHANNEL);
            String spark_class = c.getString(TAG_SPARKLING_CLASSIFICATION);

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

            map.put(TAG_OUTLET_NAME, outletname);
            map.put(TAG_SPARKLING_CHANNEL, spark_channel);
            map.put(TAG_SPARKLING_CLASSIFICATION, spark_class);

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

            // JSON data parsing completed by hash mappings
            // 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, mOutletsList,
            R.layout.single_outlet, new String[] { TAG_OUTLET_NAME, TAG_SPARKLING_CHANNEL,
            TAG_SPARKLING_CLASSIFICATION }, new int[] { R.id.outlet_name, R.id.sparkling_channel,
                    R.id.sparkling_classification });

    // I shouldn't have to comment on this one:
    setListAdapter(adapter);

    // Optional: when the user clicks a list item we 
    //could do something.  However, we will choose
    //to do nothing...
    ListView lv = getListView();    
    lv.setOnItemClickListener(new OnItemClickListener() {

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

            int itemPosition = position;


            TextView outname = (TextView)findViewById(R.id.outlet_name);
            TextView channel = (TextView)findViewById(R.id.sparkling_channel);
            TextView clas = (TextView)findViewById(R.id.sparkling_classification);

            String foutname = outname.getText().toString();
            String fchannel = channel.getText().toString();
            String fclass = clas.getText().toString();


             Intent i = new Intent(Outlets.this, ScoreSheet.class);
             i.putExtra("outlt", foutname);
             i.putExtra("chnl", fchannel);
             i.putExtra("cls", fclass);
             startActivity(i);

        }
    });
}

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

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(Outlets.this);
        pDialog.setMessage("Loading Outlets...");
        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();
    }
}
你们的帮助在我心中永远是如此的感激,伙计们。即使是做同样任务的其他方法,我也欢迎他们。提前非常感谢你们。

改变这一点

 TextView outname = (TextView)findViewById(R.id.outlet_name);
 TextView channel = (TextView)findViewById(R.id.sparkling_channel);
 TextView clas = (TextView)findViewById(R.id.sparkling_classification);

您需要使用
视图
查找视图。您可以按如下操作,而不是初始化视图

相反

  lv.setOnItemClickListener(new OnItemClickListener() {

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

          HashMap<String,String> map=  (HashMap<String, String>) parent.getItemAtPosition(position); // use postion. get the map
          String foutname =map.get(TAG_OUTLET_NAME); // get the value using key
          String fchannel = map.get(TAG_SPARKLING_CHANNEL);
          String fclass = map.get(TAG_SPARKLING_CLASSIFICATION);
          ...// rest of the code

       }
   });
lv.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父级、视图、,
内部位置,长id){
HashMap=(HashMap)parent.getItemAtPosition(position);//使用position。获取映射
String foutname=map.get(TAG_OUTLET_NAME);//使用键获取值
字符串fcchannel=map.get(标记闪烁通道);
字符串fclass=map.get(标记\闪烁\分类);
…//代码的其余部分
}
});
使用以下内容:

lv.setOnItemClickListener(new OnItemClickListener() {

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

        int itemPosition = position;

           View view1=(View) lv.getItemAtPosition(itemPosition );
        TextView outname = (TextView)view1.findViewById(R.id.outlet_name);
        TextView channel = (TextView)view1.findViewById(R.id.sparkling_channel);
        TextView clas = (TextView)view1.findViewById(R.id.sparkling_classification);

        String foutname = outname.getText().toString();
        String fchannel = channel.getText().toString();
        String fclass = clas.getText().toString();


         Intent i = new Intent(Outlets.this, ScoreSheet.class);
         i.putExtra("outlt", foutname);
         i.putExtra("chnl", fchannel);
         i.putExtra("cls", fclass);
         startActivity(i);

    }
});
lv.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父级、视图、,
内部位置,长id){
int itemPosition=位置;
视图view1=(视图)lv.getItemAtPosition(itemPosition);
TextView outname=(TextView)view1.findViewById(R.id.outlet\u name);
TextView频道=(TextView)view1.findViewById(R.id.sparkling_频道);
TextView clas=(TextView)view1.findViewById(R.id.sparkling_分类);
String foutname=outname.getText().toString();
字符串fcchannel=channel.getText().toString();
字符串fclass=clas.getText().toString();
意向i=新意向(Outlets.this、ScoreSheet.class);
i、 putExtra(“outlt”,foutname);
i、 putExtra(“chnl”,fchannel);
i、 putExtra(“cls”,fclass);
星触觉(i);
}
});

谢谢@Shashank Srivastava,它确实运行得很顺利,非常感谢。这是我的荣幸。如果您对它感到满意,请接受答案@Shashank Srivastava,我很想这样做,我如何接受答案?我是新来的兄弟。我只能发表评论。@user3652315请检查我答案左边的箭头。好的兄弟,我需要一分。我到15岁时一定会这么做。谢谢@Raghunandan,代码运行得很好,我很高兴。
  lv.setOnItemClickListener(new OnItemClickListener() {

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

          HashMap<String,String> map=  (HashMap<String, String>) parent.getItemAtPosition(position); // use postion. get the map
          String foutname =map.get(TAG_OUTLET_NAME); // get the value using key
          String fchannel = map.get(TAG_SPARKLING_CHANNEL);
          String fclass = map.get(TAG_SPARKLING_CLASSIFICATION);
          ...// rest of the code

       }
   });
lv.setOnItemClickListener(new OnItemClickListener() {

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

        int itemPosition = position;

           View view1=(View) lv.getItemAtPosition(itemPosition );
        TextView outname = (TextView)view1.findViewById(R.id.outlet_name);
        TextView channel = (TextView)view1.findViewById(R.id.sparkling_channel);
        TextView clas = (TextView)view1.findViewById(R.id.sparkling_classification);

        String foutname = outname.getText().toString();
        String fchannel = channel.getText().toString();
        String fclass = clas.getText().toString();


         Intent i = new Intent(Outlets.this, ScoreSheet.class);
         i.putExtra("outlt", foutname);
         i.putExtra("chnl", fchannel);
         i.putExtra("cls", fclass);
         startActivity(i);

    }
});