2011-05-18 81 views
0

任何一個可以幫助我在列表視圖中的第一項應該是默認選擇的Android的ListView

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

感謝

+0

所以你要第一個項目選擇或不? – 2011-05-18 07:56:39

+0

s我想要選擇第一項作爲教師getListView()。setItemChecked(0,true);我列出這些但驗證不是爲我而來 – nikhil 2011-05-18 07:59:17

+0

Sry frnd實際上我想要的是有一個列表視圖,默認情況下,教師應該被選中,我已經通過使用lViewPub.setItemChecked(0,true)完成了這些;這些但即使其選中也驗證消息發生 - – nikhil 2011-05-18 09:32:30

回答

1

您是否嘗試過使用:

ListView lv = getListView(); 
    lv.setFocusable(true); 
    lv.setFocusableInTouchMode(true); 
    lv.setSelection(0); 
+0

如果我選擇老師並點擊完成按鈕也驗證發生 – nikhil 2011-05-18 08:21:37

+0

setSelection設置當前選擇的項目。如果在觸摸模式下,該項目不會被選中,但它仍然會被適當定位。所以它沒有被選中,因爲你的列表處於觸摸模式,所以使用lViewPub.setFocusable(true);第一個 – 2011-05-18 08:29:16

+0

,這也是lViewPub.setFocusableInTouchMode(true); – 2011-05-18 08:31:12