0

I have a listview, basically I would like to have the Listview populated and display a TextView that says "New" if the user has not clicked on that position yet, and then make the TextView visibility set to gone after the user has clicked on that Listview item. could anyone help me?

I have tried quite a bit of messing with some codes, of course with trial and error, comes erasing the code so I wasnt able to show you everything that I have tried. This is the last thing I tried:

        // selecting single ListView item
    final ListView lv = getListView();



    // Launching new screen on Selecting Single ListItem
    lv.setOnItemClickListener(new OnItemClickListener() {

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

            if (lv.isPressed(view.hasFocus(position))) { 
                newjob.setVisibility(View.VISIBLE); 
            } else { 
                newjob.setVisibility(View.GONE); 
            } 


               vib.vibrate(40); 

                // Starting new intent 
                try { 
                Intent in = new Intent(getApplicationContext(), 
                        SingleMenuItemActivity.class); 
                in.putExtra("jobInfo", JOBS.getJSONObject(position).toString()); 
                in.putExtra(pays, JOBS.getJSONObject(position).toString());
                in.putExtra(job, JOBS.getJSONObject(position).toString());
                in.putExtra(ready_at, JOBS.getJSONObject(position).toString());
                in.putExtra(due_by, JOBS.getJSONObject(position).toString());
                in.putExtra(customer_reference, JOBS.getJSONObject(position).toString());
2
  • don't expect people to do all the work for you
    – ColdFire
    Aug 10, 2012 at 22:26
  • sorry, I wasnt trying to get fed witha golden spoon or anything, just wanted to ask how would i go about to achieve what i want. Aug 10, 2012 at 22:31

1 Answer 1

1

Here is one way, simply override the OnItemClickListener:

lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // Change your TextView's visibility to gone
        view.findViewById(R.id.textView).setVisibility(View.GONE);
    }
});

Updated from new code

It looks like you are using a ListActivity or ListFragment. If so, I recommend using onListItemClick() instead of onItemClick() since it is already set up:

@Override
protected void onListItemClick (ListView listView, View view, int position, long id) {
    view.findViewById(R.id.textView).setVisibility(View.GONE);
}
8
  • I would then also need to know the position of what tab was clicked so it would disappear accordingly. Aug 10, 2012 at 22:36
  • Does each row have its own text view?
    – Sam
    Aug 10, 2012 at 22:36
  • Not nessecarily its own textview, the listview recycles the textview for every tab it populates. Aug 10, 2012 at 22:38
  • Indeed ListView recycles its rows, but the row will be built from one row layout so you can use findViewById() inside the row (in this case the row is view).
    – Sam
    Aug 10, 2012 at 22:41
  • Very nice Sam! what you suggested actually does work however, the TextView is dissapearing in the wrong positions lol. Ill try to work around what you suggested and report back to you. Thanks! Aug 10, 2012 at 22:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.