我在db中使用了数据,并在listview中显示了每一行,我给了sms按钮转到smsactivity,但是单击sms按钮时,它并没有进入任何激活状态,只是在那里,没有显示任何错误或logcat错误
这是我的activity.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>
最佳答案
很难找出出了什么问题
您有这个和CustomAdapter
。 activity_row2
中的行项目已经膨胀了。
dataAdapter = new SimpleCursorAdapter(
this, R.layout.activity_row2,
projectsCursor,
from,
to,
0);
您已经定义了
CustomAdapter
,但是从不使用它。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 CustomAdapter (YourPrayerActivity .this, R.layout.activity_row2, projectsCursor, from, to);
还要如下更改您的CustomAdapter
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);
mInflater= LayoutInflater.from(context);
Toast.makeText(YourPrayerActivity.this, "text", 1000).show();
// TODO Auto-generated constructor stub
}
@Override
public void bindView(View view, Context context, final Cursor cursor){
int row_id = cursor.getColumnIndex("_id"); //Your row id (might need to replace)
TextView tv = (TextView) view.findViewById(R.id.text1);
final TextView tv1 = (TextView) view.findViewById(R.id.text2);
int col1 = cursor.getColumnIndex("title");
String title = cursor.getString(col1 );
int col2 = cursor.getColumnIndex("content");
final String content = cursor.getString(col2 );
tv.setText( title);
tv1.setText( content);
Button button = (Button) view.findViewById(R.id.buttonsms);
button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
Log.i("....................",""+cursor.getCount());
Toast.makeText(YourPrayerActivity.this, "text",400000).show();
}
});
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent){
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.activity_row2, parent, false);
bindView(v,context,cursor);
return v;
}
}
关于java - 我的动态 ListView 的每一行中的按钮不起作用?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18123443/