我想创建应用程序,当按下按钮时它会发出每个声音,每个按钮都包含在列表中(如图:enter link description here)所以我写了Im_SensShow.java,它扩展了ListActivity并拥有imsen_main.xml和imsen_row.xml它不起作用(强制关闭)请帮助
package com.android.proj; public class Im_SensShow extends ListActivity {
/** Called when the activity is first created. */
private DBAdapter db ;
private Cursor cursor ;
private static final int INSERT_ID = Menu.FIRST;
private static final int CHANGE_ID = Menu.FIRST + 1;
private static final int EDIT_ID = Menu.FIRST + 2;
private static final int DELETE_ID = Menu.FIRST + 3;
//this will store situation id that is sent from main
private long thim_sens_id;
private ImageButton menuCarBtn;
TextView MyOutputText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.imsen_main);
MyOutputText = (TextView)findViewById(R.id.OutputText);
db = new DBAdapter(this);
setBinding();
//setOnEvent();
listData();
registerForContextMenu(getListView());
}
private void setBinding(){
//Binding Objects
menuCarBtn = (ImageButton)findViewById(R.id.menuCarBtn);
}
private void listData(){
db.open();
Bundle dataBundle = Im_SensShow.this.getIntent().getExtras();
thim_sens_id = dataBundle.getLong("THIMSENSID");
cursor = db.getIm_Sen_List(thim_sens_id,1);
MyOutputText.setText(thim_sens_id+"t");
startManagingCursor(cursor);
ListAdapter list = new SimpleCursorAdapter(
this,
R.layout.imsen_row,
cursor,
new String[] {DBAdapter.KEY_IM_SENS, DBAdapter.KEY_READING},
new int[] {R.id.im_sens, R.id.reading});
setListAdapter(list);
db.close();
menuCarBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(getBaseContext(), "hello", Toast.LENGTH_LONG).show() ;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
menu.add(0, INSERT_ID,0, R.string.menu_insert);
menu.add(0, CHANGE_ID,0, R.string.menu_change_lan);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case INSERT_ID:
Cursor c = cursor;
Intent create_Im_Sens = new Intent(Im_SensShow.this,Im_SensShow.class);
Bundle dataBundle = new Bundle(0);
// we want to create record in sit 2 so send a sit 1 primary key is enough
dataBundle.putLong("THIMSENSID", thim_sens_id);
//send 0 since we will notify create activity that this is for create : data does not exist in sit 2 table yet
dataBundle.putLong("IMSENSID", 0);
create_Im_Sens.putExtras(dataBundle); //
startActivity(create_Im_Sens);
break;
case CHANGE_ID:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final CharSequence[] items = { "อังกฤษ", "ฝรั่งเศส", "เยอรมัน",
"อิตาลี", "สเปน", "แสดงทุกภาษา" };
builder.setTitle("เลือกภาษาที่แสดง");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
// Toast.makeText(getApplicationContext(), items[item],
// Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert = builder.create();
alert.show();
break;
default:
break;
}
return true;
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("จัดการข้อมูล");
menu.add(0, EDIT_ID, 0, R.string.menu_edit);
menu.add(0, DELETE_ID, 0, R.string.menu_delete);
}
/*
@Override
public boolean onContextItemSelected(MenuItem item){
switch(item.getItemId()){
case EDIT_ID:{
Cursor c = cursor;
Intent edit_Th_Im_Sens = new Intent(Im_SensShow.this,Th_Im_SensEdit.class);
Bundle bundle = new Bundle();
bundle.putLong("SITUATION2ID", sit2_id);
bundle.putLong("THIMSENSID", c.getLong(c.getColumnIndexOrThrow(DBAdapter.KEY_TH_IM_SENS_ID)));
bundle.putString("THIMSENS",c.getString(c.getColumnIndexOrThrow(DBAdapter.KEY_TH_IM_SENS)));
edit_Th_Im_Sens.putExtras(bundle);
startActivityForResult(edit_Th_Im_Sens,1);
return true;
}
case DELETE_ID:{
db.open();
db.deleteRecord(DBAdapter.TH_IM_SENS_TABLE,DBAdapter.KEY_TH_IM_SENS_ID,cursor.getLong(cursor.getColumnIndexOrThrow(DBAdapter.KEY_TH_IM_SENS_ID)));
Toast.makeText(getBaseContext(), getString(R.string.save_data), Toast.LENGTH_LONG).show() ;
listData();
return true;
}
}
return super.onContextItemSelected(item);
}
/*
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Cursor c = cursor;
c.moveToPosition(position);
Intent showTh_Im_Sens = new Intent(Th_Im_SensShow.this,Th_Im_SensShow.class);
Bundle dataBundle = new Bundle(0);
dataBundle.putLong("SITUATIONID", 0);
showTh_Im_Sens.putExtras(dataBundle); //
startActivity(showTh_Im_Sens);
}
* / @覆盖 public void onResume(){ super.onResume(); 的ListData(); } }
imsen_main.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:text="ค้นหา "
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<AutoCompleteTextView
android:text=""
android:id="@+id/AutoCompleteTextView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
</AutoCompleteTextView>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#98CAF3">
<TextView
android:text="imsentences"
android:id="@+id/TextView03"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="27dip"
android:textColor="#FFFFFF"
android:textColorHighlight="#98CAF3">
</TextView>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:footerDividersEnabled="true"
android:cacheColorHint="#00000000"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:id="@+id/im_sens"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1."/>
imsen_row.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="@+id/lang"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1."
android:textSize="16dip" />
<TextView android:id="@+id/im_sens"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1."
android:textSize="24dip" />
<TextView android:id="@+id/reading"
android:layout_width="fill_parent"
android:textSize="24dip"
android:layout_height="wrap_content"
android:text="reading"
android:layout_weight="1."/>
<ImageButton
android:id="@+id/menuCarBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/car"
android:background="@null"
/>
答案 0 :(得分:0)
您需要一个自定义适配器,您可以在其中拥有每个列表项的持有者,该列表项包含项目并允许它们被按下并拥有听众......等等。
查看此SO主题。
Android: ListView elements with multiple clickable buttons
在页面的第一个答案中,他有一个链接到他的博客,它可以很好地解释,并且还有一个指向android开发者页面的链接,在那里他们编写了一个自定义适配器。