4

zu bekommen Ich versuche, eine einfache SMS-App zu erstellen, für die Bearbeitungsseite alle gespeicherten SMS auflistet. Ich habe 2 Buttons in meinem Layout für Bearbeiten und Löschen, aber wenn ich auf sie klicke, kann ich die Zeilennummer (Zeilennummer von oben) nicht abrufen, um die SMS zu dieser bestimmten Zeile abzurufen. Jemand bitte helft mir ... meine SMS Adapter erweitert nicht nicht anything..Here meiner Klasse und xML-Dateien ..Nicht in der Lage, die Nummer der angeklickte Zeile in der Listenansicht von Android

public class SMSEdit extends ListActivity{ 

private SMSDbAdapter mDbHelper; 
//private Button editsmsbtn, deletesmsbtn; 

public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.editsmslist); 

    mDbHelper = new SMSDbAdapter(this); 
    mDbHelper.open(); 
    fillData(); 

} 

public void fillData() { 
    Cursor smsCursor = mDbHelper.fetchAllNotes(); 
    startManagingCursor(smsCursor); 

    // Create an array to specify the fields we want to display in the list (only TITLE) 
    String[] from = new String[]{SMSDbAdapter.KEY_TITLE, 
     SMSDbAdapter.KEY_BODY, 
     SMSDbAdapter.KEY_DATE, 
     SMSDbAdapter.KEY_TIME}; 

    // and an array of the fields we want to bind those fields to (in this case just text1) 
    int[] to = new int[]{R.id.smseditlistTO, 
     R.id.smseditlistMessage, 
     R.id.smseditlistDay, 
     R.id.smseditlistTime}; 

    // Now create a simple cursor adapter and set it to display 
    SimpleCursorAdapter smsadap = 
     new SimpleCursorAdapter(this, R.layout.smsrow, smsCursor, from, to); 
    setListAdapter(smsadap); 

} 

    public void myEditClickHandler(View v) 
{ 
    //get the row the clicked button is in 
    LinearLayout vwParentRow = (LinearLayout)v.getParent(); 

    Button btnChild = (Button)vwParentRow.getChildAt(4); 

    btnChild.setText("I've been clicked!"); 

    ListAdapter pos = getListAdapter(); 

    Intent intent = new Intent(); 
     intent.setClass(SMSEdit.this, SMS.class); 
    // intent.putExtra(SMSDbAdapter.KEY_ROWID, id); 

     startActivity(intent); 
     finish(); 
} 
    public void myDeleteClickHandler(View v) 
{ 

    //get the row the clicked button is in 
    LinearLayout vwParentRow = (LinearLayout)v.getParent(); 

    //TextView child = (TextView)vwParentRow.getChildAt(0); 
    Button btnChild = (Button)vwParentRow.getChildAt(5); 
    //btnChild.setText(child.getText()); 
    btnChild.setText("I've been clicked!"); 

int c = Color.CYAN; 

    vwParentRow.setBackgroundColor(c); 
vwParentRow.refreshDrawableState(); 

     } 
} 

editsmslist.xml - >>

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
<ListView android:id="@+id/android:list" 
      android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 
    <TextView android:id="@+id/android:empty" 
      android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="No Future SMS Yet" android:textSize="35px"/> 
</LinearLayout> 

smsrow.xml - >>

<?xml version="1.0" encoding="utf-8"?> 
    <ScrollView 
android:id="@+id/ScrollView01" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
xmlns:android="http://schemas.android.com/apk/res/android"> 

<LinearLayout 
android:id="@+id/LinearLayout02" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 
     <LinearLayout android:id="@+id/LinearLayout03"      android:layout_width="wrap_content" android:layout_height="wrap_content"> 
      <TextView android:layout_height="wrap_content"      android:layout_width="fill_parent" android:text="@string/smsdetailseditlist"  android:id="@+id/smseditlistTO" android:paddingLeft="5px"> 
       </TextView> 

       <TextView android:layout_height="wrap_content"  android:text="@string/smsdetailseditlist" android:id="@+id/smseditlistMessage"  android:paddingLeft="20px" android:maxLines="1" android:layout_width="wrap_content"> 
      </TextView> 
</LinearLayout> 

     <LinearLayout android:id="@+id/LinearLayout01" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" android:paddingLeft="5px"> 

     <TextView android:layout_width="wrap_content"  android:layout_height="wrap_content" android:text="Date: "></TextView><TextView     android:text="@string/smsdetailseditlist" android:id="@+id/smseditlistDay"   android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> 
      <TextView android:layout_width="wrap_content"    android:layout_height="wrap_content" android:text="Time: " android:paddingLeft="5px">    </TextView><TextView android:text="@string/smsdetailseditlist"     android:id="@+id/smseditlistTime" android:layout_width="wrap_content"    android:layout_height="wrap_content" android:paddingRight="10px"></TextView> 
      <Button android:layout_width="wrap_content"   android:layout_height="wrap_content" android:id="@+id/EditSMSButton"   android:text="@string/editsms" android:onClick="@string/myEditClickHandler"></Button> 
      <Button android:layout_width="wrap_content"  android:layout_height="wrap_content" android:id="@+id/DeleteSMSButton"   android:text="@string/deletesms" android:onClick="@string/myDeleteClickHandler"></Button> 
     </LinearLayout> 
     </LinearLayout> 
     </ScrollView> 

Auch bitte vorschlagen, wenn es gibt einen besseren Weg, dies zu implementieren oder das Layout besser aussehen zu lassen ...

Antwort

0

Ich hatte ein ähnliches Problem mit meinem ListView.

Angenommen, Sie füllen eine ListView dynamisch. Sie haben 5 Zeilen (zum Beispiel). Die ID der ersten Zeile ist 1 Sekunde ist 2 und so weiter.

Wenn Sie scrollen, welche Zeilen für Sie sichtbar sind, ist die ID der ersten Zeile (die Sie gerade sehen) immer 1. Sie müssen das kontrollieren, indem Sie irgendeine andere Klasse verwenden (XYZ).

Ich einfach ArrayList<XYZ> verwendet und das hat funktioniert.

können Sie diese Methode überschreiben in Ihrem SMSAdapter:

public View getView(int position, View convertView, ViewGroup parent) 

und rufen Sie dann die get(position) Methode, die ein ArrayList<XYZ> zurückgibt.

0

Sie sollten ListView verwenden, um Ihre Nachrichten anzuzeigen.

Speichern Sie die Daten abgerufen aus der Datenbank in einem Array von Strings sagen s1. Dann

private ArrayList<String> arr_sort = new ArrayList<String>(); Und kopieren Sie ganze S1 in arr_sort mit der Methode add().

Während Adapter Einstellung

lv.setAdapter(new ArrayAdapter<String>(DataAttach.this, android.R.layout.simple_list_item_1, arr_sort)); wo lv das Listview refernce ist.

Dann diese Methode für onClick verwenden

lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() { 
       public void onItemClick(AdapterView<?> parent, View view, 
         int position, long id) { 
        String item = (String) lv.getItemAtPosition(position); 

        Toast.makeText(*.this, 
          "You selected - " + item + " ,Please wait...", 
          Toast.LENGTH_LONG).show(); 

        Intent intent = new Intent(*.this, *.class); 
        *.this.startActivity(intent); 
       } 
      }); 

this helps ..

1

Sie einen Adapter Cursor wie folgt erweitern können ...

public class exampleAdapter extends CursorAdapter 
{ 
    LayoutInflater inflater; 
    public exampleAdapter (Context context, Cursor c) { 
     super(context, c); 
     inflater = LayoutInflater.from(context); 
    } 

    @Override 
    public void bindView(View view, Context context, Cursor cursor) { 
     // TODO Auto-generated method stub 
     Button btn_del = (Button)view.findViewById(R.id.button1); 
btn_del.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       SMSDbAdapter vb = new SMSDbAdapter(context); 
       vb.open(); 
       String xyz = cursor.getString(0); //Persumably your ID 

       vb.Delete(Integer.parseInt(cursor.getString(0))); 
       Toast.makeText(context, xyz + " Deleted!", Toast.LENGTH_SHORT).show(); 
       vb.close(); 

      } 
     }); 


    } 

    @Override 
    public View newView(Context context, Cursor cursor, ViewGroup parent) { 

     return inflater.inflate(R.layout.smsrow, parent, false); 

    }   
} 
Verwandte Themen