我在 db 中使用数据并在列表视图中显示每一行我给了一个 sms 按钮以转到 smsactivity,但是在单击 sms 按钮时它不会进入任何活动,它只是在那里,没有显示任何错误或 logcat 错误也
这是我的活动.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_item2);
mDbHelper = new GinfyDbAdapter(this);
mDbHelper.open();
fillData();
registerForContextMenu(getListView());
}
@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
private void fillData() {
mDbHelper.open();
Cursor projectsCursor = mDbHelper.fetchAllProjects();
//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_COUNT};
// and an array of the fields we want to bind those fields to (in this case just text1)
int[] to = new int[]{R.id.text2,R.id.text1,R.id.count};
dataAdapter = new SimpleCursorAdapter(
this, R.layout.activity_row2,
projectsCursor,
from,
to,
0);
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.text1);
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!");
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
super.onCreateOptionsMenu(menu);
MenuInflater mi = getMenuInflater();
mi.inflate(R.menu.activity_main1, menu);
return true;
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
createProject();
return super.onMenuItemSelected(featureId, item);
}
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 {
private LayoutInflater mInflater;
@SuppressWarnings("deprecation")
public CustomAdapter(Context context, int layout, Cursor c, String[] from,
int[] to) {
super(context, layout, c, from, to);
// TODO Auto-generated constructor stub
mInflater= LayoutInflater.from(context);
}
public View getView(final int position, View convertView, ViewGroup parent, Cursor cursor)
{
ViewHolder holder;
if(convertView==null){
convertView= mInflater.inflate(R.layout.activity_row2, null);
convertView = inflater.inflate(R.layout.activity_row2, parent, false);
holder = new ViewHolder();
holder.tv = (TextView) convertView.findViewById(R.id.text1);
holder.tv1 = (TextView) convertView.findViewById(R.id.text2);
holder.buttonsms = (Button) convertView.findViewById(R.id.buttonsms);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
int col1 = cursor.getColumnIndex("title");
final String title = cursor.getString(col1 );
int col2 = cursor.getColumnIndex("content");
final String content = cursor.getString(col2 );
holder.tv.setText( title);
holder.tv1.setText( content);
holder.buttonsms.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);
}
});
// bindView(v,context,cursor);
return convertView;
}
public class ViewHolder {
TextView tv,tv1;
Button buttonsms;
}
}
public void sendsmsdata(String strContactList1) {
Intent intent3=new Intent(YourPrayerActivity.this,SendSMSActivity.class);
intent3.putExtra("firstKeyName", strContactList1);
startActivity(intent3);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
我正在使用 simpleadapter 从 db 中获取数据,之后对于按钮功能我使用 customadapter 从 listview 中获取数据,类 customadapter extper SimpleCursorAdapter
在这里我也提到了我的 xml 文件。
<LinearLayout
android:id="@+id/Share"
android:layout_width="0.0dip"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:background="@drawable/button_new_feed"
android:orientation="horizontal" >
<Button
android:id="@+id/buttonsms"
android:layout_width="25.0dip"
android:layout_height="fill_parent"
android:layout_marginLeft="4.0dip"
android:src="@drawable/sms" />
<ImageButton
android:id="@+id/mail"
android:layout_width="25.0dip"
android:layout_height="fill_parent"
android:layout_marginLeft="6.0dip"
android:src="@drawable/mail" />
</LinearLayout>