0

リストアイテムをクリックしてクリックした特定の行タイトルを使用して、あるアクティビティから別のアクティビティにKEY_TITLEの値を渡そうとしています。私の場合、この値を渡そうとしています: static final String KEY_TITLE = "title"; OldEventActivityからOldUploadActivityまでですが、常に "index position like: 0, 1" を取得しており、 KEY_TITLEの実際の文字列を取得していません。

OldEventActivity.java:

        final ArrayList<HashMap<String, String>> itemsList = new ArrayList<HashMap<String, String>>();

        @Override
        protected ArrayList<HashMap<String, String>> doInBackground(
                String... params) {
            HttpClient client = new DefaultHttpClient();
            // Perform a GET request for a JSON list
            HttpUriRequest request = new HttpGet(URL);
            // Get the response that sends back
            HttpResponse response = null;
            try {
                response = client.execute(request);
            } catch (ClientProtocolException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            // Convert this response into a readable string
            String jsonString = null;
            try {
                jsonString = StreamUtils.convertToString(response.getEntity()
                        .getContent());
            } catch (IllegalStateException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            // Create a JSON object that we can use from the String
            JSONObject json = null;
            try {
                json = new JSONObject(jsonString);
            } catch (JSONException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            try {

                JSONArray jsonArray = json.getJSONArray(KEY_CATEGORY);

                for (int i = 0; i < jsonArray.length(); i++) {

                    HashMap<String, String> map = new HashMap<String, String>();
                    JSONObject jsonObject = jsonArray.getJSONObject(i);

                    map.put("id", String.valueOf(i));
                    map.put(KEY_TITLE, jsonObject.getString(KEY_TITLE));

                    itemsList.add(map);

                }
                return itemsList;
            } catch (JSONException e) {
                Log.e("log_tag", "Error parsing data " + e.toString());
            }
            return null;
        }


        @Override
        protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
            list = (ListView) findViewById(R.id.listView1);
            adapter = new LazyAdapters(OldEventActivity.this, itemsList);
            list.setAdapter(adapter);
            // Show Item Title in Header

            this.progressDialog.dismiss();
            list.setOnItemClickListener(new OnItemClickListener() {

                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) 
                {       
                  Intent mViewCartIntent = new Intent(OldEventActivity.this, OldUploadActivity.class);
                  mViewCartIntent.putExtra("KEY", KEY_TITLE);
                  startActivity(mViewCartIntent);

                }
            }); 
        }   
    }
}

OldUploadActivity.java:

public class OldUploadActivity extends Activity  {
        public static final String LOG_TAG = "OldUploadActivity";
        private ListView lstView;
        private Handler handler = new Handler();;
        Bundle bundle;
        String keytitle;
        List <String> ImageList;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);       
            setContentView(R.layout.activity_uploads);
            bundle = getIntent().getExtras();
            keytitle = bundle.getString("KEY");
            /*** Get Images from SDCard ***/
            ImageList = getSD();
            Log.d(OldUploadActivity.LOG_TAG, "getSD() :::--- " + ImageList);

            // ListView and imageAdapter
            lstView = (ListView) findViewById(R.id.listView1);
            lstView.setAdapter(new ImageAdapter(this));
        }


        private List <String> getSD()
        {
            List <String> it = new ArrayList <String>();            
            String string = "/mnt/sdcard/Pictures/PicsAngels/"; 

            File f = new File (string + keytitle + "/");

            Log.d(OldUploadActivity.LOG_TAG, "KEY_TITLE :::--- " + f);
            File[] files = f.listFiles ();
            Log.d(OldUploadActivity.LOG_TAG, "getListImages() :::--- " + files);

            for (int i = 0; i <files.length; i++)           
            {
                File  file = files[i];
                Log.d(Upload.LOG_TAG, "i<files.length() :::--- " + i);
                Log.d("Count",file.getPath());
                it.add (file.getPath());
            }
            Log.d(Upload.LOG_TAG, "List <String> it :::--- " + it);
            return it;
        }
4

3 に答える 3

4
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
      HashMap<String, String> map = itemsList.get(position);
      Intent mViewCartIntent = new Intent(OldEventActivity.this, OldUploadActivity.class);
      mViewCartIntent.putExtra("KEY", map.get(KEY_TITLE));
      startActivity(mViewCartIntent);
     }
});

上記のコードを試して、OldUploadActivity.class が空でないことを確認してください。

于 2013-06-21T06:23:12.467 に答える
2

次のような意図で送信するために static キーワードを使用しないでください。

public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) 
{       
       String KEY_TITLE= lv1.getItemAtPosition(position).toString();
       Intent mViewCartIntent = new Intent(OldEventActivity.this, OldUploadActivity.class);

       mViewCartIntent.putExtra("KEY", KEY_TITLE)
       startActivity(mViewCartIntent);
 }

そして他の活動に入る」

Bundle bundle=getIntent().getExtras();
String kettitle=bundle.getString("KEY")
于 2013-06-21T05:07:15.260 に答える