我有一个listview作为左边的文本和右边的复选框。我的列表视图显示带有短信主体和地址的收件箱。当我点击复选框并使用它保存时,我想要获取地址button.my复选框是可点击的,我的列表视图无法点击,因为它显示为文本。 我的代码如下:
public class SMSInbox extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.inbox);
final ListView lViewSMS = (ListView) findViewById(R.id.list);
if(fetchInbox()!=null)
{
String[] from = new String[]{};
int to = R.id.text1;
ArrayAdapter adapter = new ArrayAdapter(this,R.layout.row,to,fetchInbox());
lViewSMS.setAdapter(adapter);
}
}
public ArrayList fetchInbox()
{
ArrayList sms = new ArrayList();
Uri uriSms = Uri.parse("content://sms/inbox");
Cursor cursor = getContentResolver().query(uriSms, new String[]{"_id", "address", "date", "body"},null,null,null);
cursor.moveToFirst();
while (cursor.moveToNext())
{
String address = cursor.getString(1);
String body = cursor.getString(3);
System.out.println("======> Mobile number => "+address);
System.out.println("=====> SMS Text => "+body);
sms.add("Address=> "+address+"\n SMS => "+body);
}
return sms;
}
}
我的XML代码inbox.xml:
<?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:orientation="vertical" >
<Button android:id="@+id/get_view_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get CheckBox" />
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
我的row.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="wrap_content" >
<TextView
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip">
</TextView>
<CheckBox
android:id="@+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="4px"
android:layout_marginRight="10px" >
</CheckBox>
</RelativeLayout>
查看代码:
public class InteractiveArrayAdapter extends ArrayAdapter<Model> {
private final List<Model> list;
private final Activity context;
public InteractiveArrayAdapter(Activity context, List<Model> list) {
super(context, R.layout.row, list);
this.context = context;
this.list = list;
}
static class ViewHolder {
protected TextView text;
protected CheckBox checkbox;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.row, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) view.findViewById(R.id.text1);
viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
viewHolder.checkbox
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
Model element = (Model) viewHolder.checkbox
.getTag();
element.setSelected(buttonView.isChecked());
}
});
view.setTag(viewHolder);
viewHolder.checkbox.setTag(list.get(position));
} else {
view = convertView;
((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.text.setText(list.get(position).getName());
holder.checkbox.setChecked(list.get(position).isSelected());
return view;
}}
Model.java
public class Model {
private String name;
private boolean selected;
public Model(String name) {
this.name = name;
selected = false;
}
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;
}}
让我知道如何通过复选框点击从smsinbox.java获取地址。如何确认是复选框获取地址?
请帮我解决这个问题。
答案 0 :(得分:0)
答案 1 :(得分:0)
在适配器中添加此方法:
首先为所选项目列表定义新ArrayList
:
ArrayList<String> listChosen = new ArrayList<>();
创建一个返回列表的方法:
/**
* create this method for return new value in list item selected
* @return
*/
private List<String> getSelectedString(){
return itemSelected;
}
在复选框条件中,请执行以下代码:
holder.cbItem.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(b){
itemSelected.add(itemName);
String jsonString = gson.toJson(getSelectedString());
edit.putString(Constants.KEY_SHARED,jsonString);
edit.commit();
}else{
itemSelected.remove(itemName);
String jsonString = gson.toJson(getSelectedString());
edit.putString(Constants.KEY_SHARED,jsonString);
edit.commit();
}
}
});
如果你仍然混淆了这个过程,我发现了一篇有用的文章,请参考this article。也许你会找到你需要的东西。希望它有所帮助。