нажмите кнопку в каждом списке в ListActivity - PullRequest
1 голос
/ 06 февраля 2011

Я хочу создать приложение, которое при нажатии на кнопку будет издавать каждый звук, и каждая кнопка будет содержаться в списке (как на этом рисунке: введите описание ссылки здесь ), поэтому я написал 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);
}

* / @Override 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"
                />

1 Ответ

0 голосов
/ 06 февраля 2011

Вам нужен специальный адаптер для списка, в котором у вас есть держатель для каждого элемента списка, который содержит элементы и позволяет их нажимать и иметь слушателей ... и т. Д.

Проверьте эту тему SO.

Android: элементы ListView с несколькими нажимаемыми кнопками

В первом ответе на странице он имеет ссылку на свой блог, которая хорошо объясняет, а такжеесть ссылка на страницу разработчика Android, где они пишут пользовательский адаптер.

...