row_file.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/radioButton1"
/>
</RelativeLayout>
MainActivity.Java
package com.example.china;
import java.io.IOException;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.RadioButton;
import android.widget.Toast;
public class MainActivity extends Activity {
private Adapter gAdapter;
private GridView gBankLogo;
private int lastPosition = -1;
private View lastView;
ArrayList<ParserCategory>mList;
DatabaseConnectionAPI mApi;
Button mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gBankLogo = (GridView)findViewById(R.id.gridView1);
mButton=(Button)findViewById(R.id.btn);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int count = gBankLogo.getChildCount();
System.out.println("Count "+count);
for (int i = 0; i < count; i++) {
ViewGroup gridChild = (ViewGroup) gBankLogo.getChildAt(i);
RadioButton checkbox = (RadioButton)gridChild.findViewById(R.id.radioButton1);
if(checkbox.isChecked())
{
Log.d("Item "+String.valueOf(i), checkbox.getTag().toString());
System.out.println("ITEEMERM "+String.valueOf(i)+checkbox.getTag().toString());
Toast.makeText(MainActivity.this,checkbox.getTag().toString() ,2000).show();
}
}
}
});
mApi=new DatabaseConnectionAPI(getApplicationContext());
try {
mApi.createDataBase();
mApi.openDataBase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mList=mApi.getCategoryData();
gAdapter = new Adapter(this,mList);
//TEST
for (int i = 0; i < mList.size(); i++) {
String bank = "";
gAdapter.addObject("bank"+i, bank);
}
gBankLogo.setAdapter(gAdapter);
gBankLogo.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (lastPosition == position)
return;
if (lastPosition != position) {
if (lastPosition == -1) {
RadioButton tmp = (RadioButton) view.findViewById(R.id.radioButton1);
tmp.setChecked(true);
lastView = view;
lastPosition = position;
} else {
RadioButton tmp = (RadioButton) view.findViewById(R.id.radioButton1);
tmp.setChecked(true);
RadioButton tmp1 = (RadioButton) lastView.findViewById(R.id.radioButton1);
tmp1.setChecked(false);
lastView = view;
lastPosition = position;
}
}
}
});
}
}
Adapter.Java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RadioButton;
import android.widget.TextView;
public class Adapter extends BaseAdapter {
private ViewHolder holder;
private LayoutInflater mLayoutInflater;
private static Map<String, Object> M = new HashMap<String, Object>();
private static List<String> L = new ArrayList<String>();
ArrayList<ParserCategory>mList;
Activity mActivity;
public Adapter(Context context) {
mLayoutInflater = LayoutInflater.from(context);
}
public Adapter(MainActivity mainActivity, ArrayList<ParserCategory> mList) {
// TODO Auto-generated constructor stub
mActivity=mainActivity;
this.mList=mList;
}
public void addObject(String key, String bank) {
if (!L.contains(key)) {
L.add(key);
}
M.put(key, bank);
this.notifyDataSetChanged();
}
public void removeObject(String key) {
M.remove(key);
L.remove(key);
this.notifyDataSetChanged();
}
public void removeAllObject() {
if (getCount() != 0) {
M.clear();
L.clear();
this.notifyDataSetChanged();
}
}
public void removeAllSelect() {
if (getCount() != 0) {
holder.rSelect.setChecked(false);
this.notifyDataSetChanged();
}
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
// return mList.get(L.get(position));
return mList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) mActivity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.item_msgbanklogo, null);
}
// convertView = mLayoutInflater.inflate(R.layout.item_msgbanklogo,null);
holder.mTextView = (TextView) convertView
.findViewById(R.id.imageView1);
holder.rSelect = (RadioButton) convertView
.findViewById(R.id.radioButton1);
holder.mTextView.setText(mList.get(position).getCname());
holder.rSelect.setTag(mList.get(position).getCname());
holder.mTextView.setClickable(false);
holder.rSelect.setClickable(false);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
}
ViewHolder.java
public class ViewHolder {
TextView mTextView;
RadioButton rSelect;
}
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/radioButton1"
/>
</RelativeLayout>
MainActivity.Java
package com.example.china;
import java.io.IOException;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.RadioButton;
import android.widget.Toast;
public class MainActivity extends Activity {
private Adapter gAdapter;
private GridView gBankLogo;
private int lastPosition = -1;
private View lastView;
ArrayList<ParserCategory>mList;
DatabaseConnectionAPI mApi;
Button mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gBankLogo = (GridView)findViewById(R.id.gridView1);
mButton=(Button)findViewById(R.id.btn);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int count = gBankLogo.getChildCount();
System.out.println("Count "+count);
for (int i = 0; i < count; i++) {
ViewGroup gridChild = (ViewGroup) gBankLogo.getChildAt(i);
RadioButton checkbox = (RadioButton)gridChild.findViewById(R.id.radioButton1);
if(checkbox.isChecked())
{
Log.d("Item "+String.valueOf(i), checkbox.getTag().toString());
System.out.println("ITEEMERM "+String.valueOf(i)+checkbox.getTag().toString());
Toast.makeText(MainActivity.this,checkbox.getTag().toString() ,2000).show();
}
}
}
});
mApi=new DatabaseConnectionAPI(getApplicationContext());
try {
mApi.createDataBase();
mApi.openDataBase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mList=mApi.getCategoryData();
gAdapter = new Adapter(this,mList);
//TEST
for (int i = 0; i < mList.size(); i++) {
String bank = "";
gAdapter.addObject("bank"+i, bank);
}
gBankLogo.setAdapter(gAdapter);
gBankLogo.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (lastPosition == position)
return;
if (lastPosition != position) {
if (lastPosition == -1) {
RadioButton tmp = (RadioButton) view.findViewById(R.id.radioButton1);
tmp.setChecked(true);
lastView = view;
lastPosition = position;
} else {
RadioButton tmp = (RadioButton) view.findViewById(R.id.radioButton1);
tmp.setChecked(true);
RadioButton tmp1 = (RadioButton) lastView.findViewById(R.id.radioButton1);
tmp1.setChecked(false);
lastView = view;
lastPosition = position;
}
}
}
});
}
}
Adapter.Java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RadioButton;
import android.widget.TextView;
public class Adapter extends BaseAdapter {
private ViewHolder holder;
private LayoutInflater mLayoutInflater;
private static Map<String, Object> M = new HashMap<String, Object>();
private static List<String> L = new ArrayList<String>();
ArrayList<ParserCategory>mList;
Activity mActivity;
public Adapter(Context context) {
mLayoutInflater = LayoutInflater.from(context);
}
public Adapter(MainActivity mainActivity, ArrayList<ParserCategory> mList) {
// TODO Auto-generated constructor stub
mActivity=mainActivity;
this.mList=mList;
}
public void addObject(String key, String bank) {
if (!L.contains(key)) {
L.add(key);
}
M.put(key, bank);
this.notifyDataSetChanged();
}
public void removeObject(String key) {
M.remove(key);
L.remove(key);
this.notifyDataSetChanged();
}
public void removeAllObject() {
if (getCount() != 0) {
M.clear();
L.clear();
this.notifyDataSetChanged();
}
}
public void removeAllSelect() {
if (getCount() != 0) {
holder.rSelect.setChecked(false);
this.notifyDataSetChanged();
}
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
// return mList.get(L.get(position));
return mList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) mActivity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.item_msgbanklogo, null);
}
// convertView = mLayoutInflater.inflate(R.layout.item_msgbanklogo,null);
holder.mTextView = (TextView) convertView
.findViewById(R.id.imageView1);
holder.rSelect = (RadioButton) convertView
.findViewById(R.id.radioButton1);
holder.mTextView.setText(mList.get(position).getCname());
holder.rSelect.setTag(mList.get(position).getCname());
holder.mTextView.setClickable(false);
holder.rSelect.setClickable(false);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
}
ViewHolder.java
public class ViewHolder {
TextView mTextView;
RadioButton rSelect;
}
No comments:
Post a Comment