import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
/**
* Demo Activity that will display the ListView and footer.
* @author techin
*/
public class Demo extends Activity {
ListView listView;
TextView textView;
Button Ok;
String array[]=new String[]{"a","b","c","d","e","f","g","h","i"};
String temp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView = (ListView)findViewById(R.id.ListView01);
textView = (TextView)findViewById(R.id.TextView01);
Ok = (Button)findViewById(R.id.okBT);
/**
* Setting ListView adapter using AttayAdapter.
*/
listView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,array));
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
/**
* OK button on click Listener.
*/
Ok.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
System.out.println("check Items:"+listView.getCheckItemIds().length);
getSelectedItems(listView.getCheckItemIds().length);
}
});
}
/**
* Method to get Selected Items from the ListView.
* @param length - Length of the number of CheckBox that are selected.
*/
private void getSelectedItems(int length){
temp="";
for(int i=0;i<length;i++){
temp+=array[(int) listView.getCheckItemIds()[i]];
}
textView.setText(temp);
}
}
Pages
▼
Perfect! Thank u. Amazing tutorial
ReplyDelete