Sun
Sun

Reputation: 6888

Getting Index Position of ListView Clicked Item not getting String Value

I am trying to pass value of KEY_TITLE from one activity to another activity, using click on List Item the particular row title which i have clicked, in my case i am trying to pass value of this: static final String KEY_TITLE = "title"; from OldEventActivity to OldUploadActivity but always i am getting "index position like: 0, 1", not getting actual string for 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;
        }

Upvotes: 0

Views: 1894

Answers (3)

Tycoon
Tycoon

Reputation: 538

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);
     }
});

try the above code and see that your OldUploadActivity.class is not empty.

Upvotes: 4

brianestey
brianestey

Reputation: 8302

The problem is that in this line:

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

You are appending OldEventActivity.KEY_TITLE, which you can see is a static final String.

static final String KEY_TITLE = "title";

So basically, your f is looking at the path /mnt/sdcard/Pictures/MyPics/title/, as your code is telling it to do.

It looks like you are trying to send a different value, not the value of KEY_TITLE. Instead of sending the value (ie. "title"), use that as the KEY and send the real value. Ie, instead of

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

You should write something like:

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

And then in your activity where you want to get the value, you simply access it via

String passedTitle = intent.getStringExtra(OldEventActivity.KEY_TITLE);

Upvotes: 0

Sunil Kumar
Sunil Kumar

Reputation: 7092

Please do not use the static keyword better to send with intent like

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);
 }

and get in otheractivty'

Bundle bundle=getIntent().getExtras();
String kettitle=bundle.getString("KEY")

Upvotes: 2

Related Questions