Code:
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class MainActivity extends Activity
{
TextView red;
StringBuilder sb;
MyCustomAdapter dataAdapter = null;
[USER=1021285]@override[/USER]
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Generate list View from ArrayList
displayListView();
checkButtonClick();
}
private void displayListView()
{
//Array list of countries
ArrayList<States> stateList = new ArrayList<States>();
States _states = new States("GA","Atlanta",false);
stateList.add(_states);
_states = new States("MA","Boston",false);
stateList.add(_states);
_states = new States("OK","Oklahoma",false);
stateList.add(_states);
_states = new States("SC","Columbia",false);
stateList.add(_states);
_states = new States("TX","Texas",false);
stateList.add(_states);
_states = new States("TN","Nashville",false);
stateList.add(_states);
_states = new States("AR","Little rock",false);
stateList.add(_states);
_states = new States("HI","Honolulu",false);
stateList.add(_states);
//create an ArrayAdaptar from the String Array
dataAdapter = new MyCustomAdapter(this,R.layout.customlist, stateList);
ListView listView = (ListView) findViewById(R.id.listView1);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
listView.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
// When clicked, show a toast with the TextView text
States state = (States) parent.getItemAtPosition(position);
Toast.makeText(getApplicationContext(),"Clicked on Row: " + state.getName(),
Toast.LENGTH_LONG).show();
}
});
}
private class MyCustomAdapter extends ArrayAdapter<States>
{
private ArrayList<States> stateList;
public MyCustomAdapter(Context context, int textViewResourceId,
ArrayList<States> stateList)
{
super(context, textViewResourceId, stateList);
this.stateList = new ArrayList<States>();
this.stateList.addAll(stateList);
}
private class ViewHolder
{
TextView code;
TextView red;
CheckBox name;
}
[USER=1021285]@override[/USER]
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder = null;
Log.v("ConvertView", String.valueOf(position));
if (convertView == null)
{
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.customlist, null);
holder = new ViewHolder();
holder.code = (TextView) convertView.findViewById(R.id.code);
holder.name = (CheckBox) convertView.findViewById(R.id.checkBox1);
holder.red = (TextView) convertView.findViewById(R.id.flower);
convertView.setTag(holder);
final ViewHolder finalHolder = holder;
holder.name.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
final CharSequence[] dilogList = {"Red", "Yellow", "Green","Purple", "Brown"};
AlertDialog.Builder multChoiceDialog = new AlertDialog.Builder(MainActivity.this);
multChoiceDialog.setTitle("Flower grow in this states");
boolean[] _selections = new boolean[dilogList.length];
multChoiceDialog.setMultiChoiceItems(dilogList, _selections, new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog,
int whichButton, boolean isChecked) {
}
});
multChoiceDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
[USER=1021285]@override[/USER]
public void onClick(DialogInterface dialog, int which) {
// getting listview from alert box
ListView list = ((AlertDialog) dialog).getListView();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < list.getCount(); i++) {
boolean checked = list.isItemChecked(i);
// get checked list value
if (checked) {
if (sb.length() > 0)
sb.append(",");
sb.append(list.getItemAtPosition(i));
}
}
Toast.makeText(getApplicationContext(), "Selected city:"
+ sb.toString(), Toast.LENGTH_SHORT).show();
finalHolder.red.setText(sb);
}
});
multChoiceDialog.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
[USER=1021285]@override[/USER]
public void onClick(DialogInterface dialog, int which) {
// cancel code here
}
});
AlertDialog alert1 = multChoiceDialog.create();
alert1.show();
} else {
finalHolder.red.setText("");
}
}
});
holder.name.setOnClickListener( new View.OnClickListener()
{
public void onClick(View v)
{
CheckBox cb = (CheckBox) v;
States _state = (States) cb.getTag();
_state.setSelected(cb.isChecked());
}
});
}
else
{
holder = (ViewHolder) convertView.getTag();
}
States state = stateList.get(position);
holder.code.setText(" (" + state.getCode() + ")");
holder.name.setText(state.getName());
holder.name.setChecked(state.isSelected());
holder.name.setTag(state);
return convertView;
}
}
private void checkButtonClick()
{
Button myButton = (Button) findViewById(R.id.findSelected);
myButton.setOnClickListener(new OnClickListener()
{
[USER=1021285]@override[/USER]
public void onClick(View v)
{
StringBuffer responseText = new StringBuffer();
responseText.append("The following were selected...\n");
ArrayList<States> stateList = dataAdapter.stateList;
for(int i=0;i<stateList.size();i++)
{
States state = stateList.get(i);
if(state.isSelected())
{
responseText.append("\n" + state.getName());
}
}
Toast.makeText(getApplicationContext(),
responseText, Toast.LENGTH_LONG).show();
}
});
}
}
MY ADAPTER IS
Code:
public class States
{
String code = null;
String name = null;
boolean selected = false;
public States(String code, String name, boolean selected)
{
super();
this.code = code;
this.name = name;
this.selected = selected;
}
public String getCode()
{
return code;
}
public void setCode(String code)
{
this.code = code;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public boolean isSelected()
{
return selected;
}
public void setSelected(boolean selected)
{
this.selected = selected;
}
}
MAIN XML
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:eek:rientation="vertical"
android:gravity="center"
android:background="#ffeeeeee">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:padding="10dp"
android:text="USA" android:textSize="20sp" />
<Button android:id="@+id/findSelected"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="USA States Grow Flower" />
<ListView android:id="@+id/listView1" android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
CUSTOM XML
Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:eek:rientation="vertical"
android:padding="6dip">
<CheckBox android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:focusable="false"
android:textColor="#ff00bb88"
android:focusableInTouchMode="false"
android:text="checkbox" />
<TextView android:id="@+id/code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@id/checkBox1"
android:layout_alignBottom="@id/checkBox1"
android:layout_toRightOf="@id/checkBox1"
android:text="textview"
android:textColor="#ff000000"/>
<TextView android:id="@+id/flower"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ff000000"
android:singleLine="true"
android:layout_alignTop="@+id/code"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
MY QUESTION IS HOW TO SEE WHEN I CLIK A BUTTON
ATLANTA RED,YELLOW
BOSTON YELLOW,BROWN
AND SO ON
HOW I DO THAT
Last edited by a moderator:
rientation="vertical"
adding="6dip">