我在列表视图中使用复选框,同时单击复选框它必须选择行中的内容。但没有占据当前位置。但不是第一次选择当前位置,第二次没有选择任何东西。没有选择当前位置,随机选择。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_item2);
lv =(ListView)findViewById(R.id.list);
mDbHelper = new GinfyDbAdapter(this);
share = (Button)findViewById(R.id.btnget);
btnadd1 = (Button)findViewById(R.id.btnadd);
lv = getListView();
share.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
StringBuilder result = new StringBuilder();
for(int i=0;i<mCheckStates.size();i++)
{
if(mCheckStates.get(i)==true)
{
result.append("Title:");
result.append(bb.get(i));
result.append("\n");
result.append("Content:");
result.append(aa.get(i));
result.append("\n");
}
}
// }
showAlertView(result.toString().trim());
}
});
btnadd1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
createProject();
}
});
mDbHelper.open();
fillData();
registerForContextMenu(getListView());
}
@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
private void fillData() {
mDbHelper.open();
Cursor projectsCursor = mDbHelper.fetchAllProjects();
int count = projectsCursor.getCount();
Log.i(".................",""+count);
if (projectsCursor.moveToFirst()) {
do {
int col1 = projectsCursor.getColumnIndex("title");
String title = projectsCursor.getString(col1 );
bb.add(title);
int col2 = projectsCursor.getColumnIndex("content");
String content = projectsCursor.getString(col2 );
aa.add(content);
} while (projectsCursor.moveToNext());
}
//startManagingCursor(projectsCursor);
// Create an array to specify the fields we want to display in the list (only TITLE)
String[] from = new String[]{GinfyDbAdapter.CATEGORY_COLUMN_TITLE,GinfyDbAdapter.CATEGORY_COLUMN_CONTENT,GinfyDbAdapter.CATEGORY_COLUMN_DATE};
int[] to = new int[]{R.id.text22,R.id.text11,R.id.date};
dataAdapter = new CustomAdapter (YourPrayerActivity .this, R.layout.row2, projectsCursor, from, to);
setListAdapter(dataAdapter);
EditText myFilter = (EditText) findViewById(R.id.myFilter);
myFilter.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
public void onTextChanged(CharSequence s, int start,
int before, int count) {
dataAdapter.getFilter().filter(s.toString());
}
});
dataAdapter.setFilterQueryProvider(new FilterQueryProvider() {
public Cursor runQuery(CharSequence constraint) {
return mDbHelper.fetchProjectByName(constraint.toString());
}
});
tts = new TextToSpeech(this, this);
final ListView lv = getListView();
txtText = (TextView) findViewById(R.id.text11);
lv.setTextFilterEnabled(true);
}
@Override
public void onDestroy() {
// Don't forget to shutdown tts!
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
} else {
//btnaudioprayer.setEnabled(true);
speakOut();
}
} else {
Log.e("TTS", "Initilization Failed!");
}
}
private void createProject() {
Intent i = new Intent(this, AddyourprayerActivity.class);
startActivityForResult(i, ACTIVITY_CREATE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
fillData();
}
private void speakOut() {
// String text = txtText.getText().toString();
// String text = "Android speech";
tts.speak(typed, TextToSpeech.QUEUE_FLUSH, null);
}
class CustomAdapter extends SimpleCursorAdapter implements CompoundButton.OnCheckedChangeListener {
private LayoutInflater mInflater;
private ListView lv;
@SuppressWarnings("deprecation")
public CustomAdapter(Context context, int layout, Cursor c, String[] from, int[] to)
{
super(context, layout, c, from, to);
mInflater= LayoutInflater.from(context);
mCheckStates = new SparseBooleanArray(c.getCount());
}
@Override
public void bindView(View view, Context context, final Cursor cursor){
if (view != null) {
int row_id = cursor.getColumnIndex("_id"); //Your row id (might need to replace)
TextView tv = (TextView) view.findViewById(R.id.text22);
final TextView tv1 = (TextView) view.findViewById(R.id.text11);
TextView tv2 = (TextView) view.findViewById(R.id.date);
CheckBox cb = (CheckBox) view.findViewById(R.id.checkbox);
int col1 = cursor.getColumnIndex("title");
final String title = cursor.getString(col1 );
int col2 = cursor.getColumnIndex("content");
final String content = cursor.getString(col2 );
int col3 = cursor.getColumnIndex("date");
final String date = cursor.getString(col3);
cb.setTag(cursor.getPosition());
cb.setChecked(mCheckStates.get(cursor.getPosition()+1, false));
cb.setOnCheckedChangeListener(this);
tv.setText( title);
tv1.setText( content);
tv2.setText(date);
ImageButton button = (ImageButton) view.findViewById(R.id.sms1);
button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
StringBuffer sb2 = new StringBuffer();
sb2.append("Title:");
sb2.append(Html.fromHtml(title));
sb2.append(",Content:");
sb2.append(Html.fromHtml(content));
sb2.append("\n");
String strContactList1 = (sb2.toString().trim());
sendsmsdata(strContactList1);
}
});
ImageButton button1 = (ImageButton) view.findViewById(R.id.mail1);
button1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
StringBuffer sb3 = new StringBuffer();
sb3.append("Title:");
sb3.append(Html.fromHtml(title));
sb3.append(",Content:");
sb3.append(Html.fromHtml(content));
sb3.append("\n");
String strContactList2 = (sb3.toString().trim());
sendmaildata(strContactList2);
}
});
ImageButton button2 = (ImageButton) view.findViewById(R.id.btnaudioprayer1);
button2.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
//ADD STUFF HERE you know which row is clicked. and which button
typed = content;
speakOut();
}
});
}
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent){
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.row2, parent, false);
bindView(v,context,cursor);
return v;
}
public boolean isChecked(int position) {
return mCheckStates.get(position, false);
}
public void setChecked(int position, boolean isChecked) {
mCheckStates.put(position, isChecked);
}
public void toggle(int position) {
setChecked(position, !isChecked(position));
}
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
mCheckStates.put((Integer) buttonView.getTag(), isChecked);
}
在单击复选框时,然后必须单击共享按钮后,它将显示短信或电子邮件,如果我们单击短信,在该内容中,我们检查了哪些内容必须存在于 msg 内容中。
我在调试中检查,如果我选择第一行或第二行,则它会占用,否则如果我首先选择第三行,它不会占用内容。