0

Any one can help me in listview the first item should be selected by default

package com.globalscholar.announcement;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.AdapterView.OnItemClickListener;

import com.globalscholar.R;

public class PublishAnnouncement extends ListActivity implements OnClickListener, OnItemClickListener{
    private ListView lViewPub;
    private String lv_items[] = {"Teachers" , "Students", "Administration"};
    RadioButton radioSSP;
    RadioButton radioPubAll1;
    Button backPub;
    Button donePub;
    int role = 0;

    //venkat
    int selectedPublishOption = -1;
    AudienceWrapper audienceDetails = null; 

    ArrayList<Boolean> checkedStates = new ArrayList<Boolean>();
    boolean checkedArray[];

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.publishannouncement);

    backPub=(Button)findViewById(R.id.buttPubBack);
    backPub.setOnClickListener(this);

    donePub=(Button)findViewById(R.id.buttPubDone);
    donePub.setOnClickListener(this);

    radioSSP=(RadioButton)findViewById(R.id.radioPubSSM);
    radioSSP.setOnClickListener(this);

    radioPubAll1=(RadioButton)findViewById(R.id.radioPubAll);
    radioPubAll1.setOnClickListener(this);
    radioPubAll1.setChecked(true);


    lViewPub = (ListView) findViewById(android.R.id.list);
    lViewPub.setOnItemClickListener(this);
    //  Set option as Multiple Choice. So that user can be able to select more the one option from list
    lViewPub.setAdapter(new ArrayAdapter<String>(this,
            R.layout.announcementlistviewstyle, lv_items));
    lViewPub.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    lViewPub.setVerticalScrollBarEnabled(false);
    getListView().setSelection(0);
    //venkat
    Bundle b = getIntent().getExtras();

    checkedStates.add(0, false);
    checkedStates.add(1, false);
    checkedStates.add(2, false);

    if(b!= null){
        audienceDetails = (AudienceWrapper) b.getSerializable("audienceDetails");
        selectedPublishOption = b.getInt("selectedPublishOption");
        //  checked = b.getLongArray("selectedPublishOptionsList");
        checkedArray =b.getBooleanArray("selectedPublishOptionsList");
        if(checkedArray!=null){
            Log.d("PA","Bundle Checked Array Size :"+checkedArray.length);
            for(int i=0;i<checkedArray.length;i++){
                checkedStates.set(i, checkedArray[i]);
                Log.w("PA","CheckedStatus index:"+i+"Status"+checkedStates.get(i));
                if(checkedStates.get(i))
                    lViewPub.setItemChecked(i, true);
            }
        }

        Log.d("selected publishoption in PublishAnnouncement--->", selectedPublishOption+"");
        if(selectedPublishOption == 0){
            radioPubAll1.setChecked(false);
            radioSSP.setChecked(true);
            lViewPub.setVisibility(BIND_AUTO_CREATE);
            //for checking the options                  
        }
    }
    //venkatend
    PublishAnnouncement.this.getListView().setOnItemClickListener(this);
}



public void onItemClick(AdapterView<?> arg0, View arg1, int position,
        long arg3) {        
    Log.d("onItemClick position in PublishAnnouncement----->",position+"");
    //role value updated in wrong way when u double click on Item
    //solved role problem in done button
    /* if(position == 0){
         role+= 4;
     }
     else if(position == 1){
         role+=8;
     }
     else if(position == 2){
         role+=2;
     }else{
         role+=14;
     }  
     Log.d("PA","Role :"+role);*/

    //venkat for selected publishes
    boolean currentlyChecked = checkedStates.get(position);  
    checkedStates.set(position, !currentlyChecked); 
    if(checkedArray!=null)
        checkedArray[position] = !currentlyChecked;



}

public void onClick(View v) {

    switch (v.getId()) {
    case R.id.radioPubSSM:
        lViewPub.setVisibility(BIND_AUTO_CREATE);
        break;
    case R.id.buttPubBack:
        onKeyDown(KeyEvent.KEYCODE_BACK, 
                new KeyEvent(KeyEvent.ACTION_DOWN,
                        KeyEvent.KEYCODE_BACK));
        break;
    case R.id.buttPubDone:
        //venkat
        int allSelect = 0;
        int sppCount=0;
        //venkatend
        Intent newPubDone= new Intent(PublishAnnouncement.this,Audience.class);
        Bundle b = new Bundle();
        if(radioPubAll1.isChecked()){
            role = 14;
            //venkat
            allSelect = 1;
        }else if(radioSSP.isChecked()){
            int len = checkedStates.size();     
            if(checkedArray == null)
                checkedArray = new boolean [len];
            for(int i=0;i<len;i++){
                checkedArray[i] = checkedStates.get(i);
                if(checkedStates.get(i)==true){
                    sppCount++;
                }
            }
            Log.d("PA","Count :"+sppCount);
        }
        if(sppCount==0 && radioSSP.isChecked()){
            AlertDialog.Builder alertBox = new AlertDialog.Builder(this);
            alertBox.setMessage("Please select Specific Memebers");
            alertBox.setNeutralButton("Ok", null);
            alertBox.show();

        }else{
            b.putInt("SelectedPublishOption", allSelect);
            int finalRole = 0;
            if(allSelect== 0){
                // b.putLongArray("SelectedPublishOptionsList", checked);
                b.putBooleanArray("SelectedPublishOptionsList", checkedArray);
                if(checkedArray!=null){
                    if(checkedArray[0]){
                        finalRole+=4;
                    }
                    if(checkedArray[1]){
                        finalRole+=8;
                    }
                    if(checkedArray[2]){
                        finalRole+=2;
                    }
                }
                Log.d("PA","After selecting final Role Value :"+finalRole);
                if(audienceDetails!=null)
                    audienceDetails.setRoleValue(finalRole);

            }
            b.putInt("role", finalRole);
            //venkatend
            newPubDone.putExtras(b);
            setResult(RESULT_OK, newPubDone);
            System.out.println("Role value is" + role);
            finish();

        }
        break;
    case R.id.radioPubAll:
        lViewPub.setVisibility(-1);

        //venkat testing logic
        if(checkedArray!=null){
            int len= checkedArray.length;
            if(len > 0){
                for(int i=0;i<len;i++){                             
                    lViewPub.setItemChecked(i, false);
                    System.out.println("all selected Position selected "+checkedArray[i]);
                }
            }
        }
        break;
    default:
        break;
    }
}
}

thanks

3
  • so you want first item selected or not? Commented May 18, 2011 at 7:56
  • s i want first item to be selected as teacher getListView().setItemChecked(0, true); i gave list these but validation is not comming for me Commented May 18, 2011 at 7:59
  • Sry frnd actually what i want is there is a listview in that by default teacher should be selected,i have done these by using lViewPub.setItemChecked(0,true); these but even if its selected also validation message occurs – Commented May 18, 2011 at 9:32

1 Answer 1

1

Have you tried to use:

    ListView lv = getListView();
    lv.setFocusable(true);
    lv.setFocusableInTouchMode(true);
    lv.setSelection(0);
Sign up to request clarification or add additional context in comments.

7 Comments

if i select teacher and click on done button also validation occurs
setSelection sets the currently selected item. If in touch mode, the item will not be selected but it will still be positioned appropriately. So it is not selected because your list is in touch mode, so use lViewPub.setFocusable(true); first
and this too lViewPub.setFocusableInTouchMode(true);
Also change the checkedStates.add(0, false); to checkedStates.add(0, true);
Sry frnd actually what i want is there is a listview in that by default teacher should be selected,i have done these by using lViewPub.setItemChecked(0,true); these but even if its selected also validation message occurs
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.