gpt4 book ai didi

java - Checkbox-未选中当前位置

转载 作者:行者123 更新时间:2023-11-30 03:22:25 24 4
gpt4 key购买 nike

我在 ListView 中使用复选框,同时单击它必须选择行中的内容的复选框。但没有占据当前位置。但是第一次没有选择当前位置,第二次没有选择任何东西。不是选择当前位置,是随机选择。

  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);
}

同时单击复选框,然后必须单击共享按钮后,它会显示短信或电子邮件,如果我们单击短信,在该内容中我们检查了哪些内容必须包含在消息内容中。

我在调试中检查过,如果我选择第一行或第二行,它会获取内容,否则如果我先选择第三行,它不会获取内容。

最佳答案

有一个计数变量作为类成员

int count;

然后在fillData

count = projectsCursor.getCount();

当你点击一个按钮时

    StringBuilder result = new StringBuilder();
if(count>0) // check if count is greater than o
// count can be 0 if you don't select any check box
{
for(int i=0;i<count;i++)

{Log.i("checked content Inside on click of share ",""+aa.get(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");

}

}
}

您正在使用一个 SparseBoolean 数组,该数组对于您检查的行是正确的。然后根据选中的项目检索数据。

这是我从中挑选的样本

https://groups.google.com/forum/#!topic/android-developers/No0LrgJ6q2M

您所做的是您没有遍历整个项目列表来检查复选框是否被选中。

        for(int i=0;i<mCheckStates.size();i++) // this was the problem

如果只选中两项,您将获得前两项。

关于java - Checkbox-未选中当前位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18890939/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com