获取ListView的索引位置单击项目不获取字符串值

时间:2013-06-21 04:59:06

标签: android android-listview

我正在尝试将 KEY_TITLE 的值从一个活动传递到另一个活动,使用点击列表项我点击的特定行标题,在我的情况下我试图传递此值:static final String KEY_TITLE =“title”;从 OldEventActivity OldUploadActivity ,但我总是得到“索引位置如: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;
        }

3 个答案:

答案 0 :(得分:5)

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不是空的。

答案 1 :(得分:2)

请不要使用静态关键字更好地发送意图

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")

答案 2 :(得分:0)

问题在于这一行:

File f = new File (string + OldEventActivity.KEY_TITLE + "/");

您正在追加OldEventActivity.KEY_TITLE,您可以看到它是一个静态的最终字符串。

static final String KEY_TITLE = "title";

所以基本上,你的f正在查看路径/mnt/sdcard/Pictures/MyPics/title/,正如你的代码告诉它的那样。

看起来您正在尝试发送不同的值,而不是KEY_TITLE的值。而不是发送值(即“标题”),而是使用它作为KEY并发送实际值。即,而不是

Intent mViewCartIntent = new Intent(OldEventActivity.this, OldUploadActivity.class);
mViewCartIntent.putExtra("KEY", KEY_TITLE); 
startActivity(mViewCartIntent);

你应该写一些类似的东西:

Intent mViewCartIntent = new Intent(OldEventActivity.this, OldUploadActivity.class);
mViewCartIntent.putExtra(KEY_TITLE, "whatever title value you want to send here."); 
startActivity(mViewCartIntent);

然后在您想要获取价值的活动中,您只需通过

访问它
String passedTitle = intent.getStringExtra(OldEventActivity.KEY_TITLE);