activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click"
android:text="getCount" />
</LinearLayout>
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
row_photo.xml
<?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="fill_parent" >
<TextView
android:id="@+id/thumbImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_alignParentLeft="true"
android:text="sample text"
android:textSize="12dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<CheckBox
android:id="@+id/itemCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" />
<TextView
android:id="@+id/txtindexid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/itemCheckBox"
android:layout_alignBottom="@+id/itemCheckBox"
android:layout_toRightOf="@+id/thumbImage"
android:text="-1" />
</RelativeLayout>
NameBean.java
public class NameBean {
private String name;
private String id;
public NameBean(){}
public NameBean(String name, String id) {
this.name = name;
this.id = id;
}
@Override
public String toString() {
return getId();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
MainActivity.java
public class MainActivity extends Activity {
private ListView listview;
ArrayList<NameBean> items = new ArrayList<NameBean>();
private int count;
private boolean[] thumbnailsselection;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fillarray();
count = items.size();
thumbnailsselection = new boolean[count];
listview = (ListView) findViewById(R.id.listView1);
listview.setAdapter(new ImageAdapter(MainActivity.this));
}
private void fillarray() {
// TODO Auto-generated method stub
items.clear();
items.add(new NameBean("Android alpha1","1"));
items.add(new NameBean("Android alpha2","2"));
items.add(new NameBean("Android alpha3","3"));
items.add(new NameBean("Android alpha4","4"));
items.add(new NameBean("Android alpha5","5"));
items.add(new NameBean("Android alpha6","6"));
items.add(new NameBean("Android alpha7","7"));
items.add(new NameBean("Android alpha8","8"));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public class ImageAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private Context mContext;
public ImageAdapter(Context context) {
mContext = context;
}
public int getCount() {
return count;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(mContext).inflate(
R.layout.row_photo, null);
holder.textview = (TextView) convertView
.findViewById(R.id.thumbImage);
holder.textIndexId = (TextView) convertView
.findViewById(R.id.txtindexid);
holder.checkbox = (CheckBox) convertView
.findViewById(R.id.itemCheckBox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.checkbox.setId(position);
holder.textview.setId(position);
holder.textIndexId.setId(position);
holder.checkbox.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
CheckBox cb = (CheckBox) v;
int id = cb.getId();
if (thumbnailsselection[id]) {
cb.setChecked(false);
thumbnailsselection[id] = false;
} else {
cb.setChecked(true);
thumbnailsselection[id] = true;
}
}
});
holder.textview.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
int id = v.getId();
}
});
holder.textview.setText(items.get(position).getName());
holder.textIndexId.setText(items.get(position).getId());
holder.checkbox.setChecked(thumbnailsselection[position]);
holder.id = position;
return convertView;
}
}
class ViewHolder {
TextView textview , textIndexId;
CheckBox checkbox;
int id;
}
public void click(View v) {
if (v.getId() == R.id.button1) {
final ArrayList<NameBean> posSel = new ArrayList<NameBean>();
posSel.clear();
boolean noSelect = false;
for (int i = 0; i < thumbnailsselection.length; i++) {
if (thumbnailsselection[i] == true) {
noSelect = true;
Log.e("sel pos thu-->", "" + i);
posSel.add(items.get(i));
// break;
}
}
if (!noSelect) {
Toast.makeText(MainActivity.this, "Please Select Item!",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this,
"Selected Items:" + posSel.toString(),
Toast.LENGTH_LONG).show();
}
}
}
}
No comments:
Post a Comment