2014-04-20 1 views
0

je réussi à obtenir la boîte de dialogue pour afficher sur le clic long en mettant en œuvre ce code:fichier de dialogue pour mettre à jour les lignes de la liste ne fonctionne pas/OnItemLongClick

ListView list = getListView(); 
     list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { 
       public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { 
        new EditListItemDialog(view.getContext()).show(); 

       return true;  
      } 
     }); 

Malheureusement, l'ouverture du dialogue et invite à nouveau saisie de texte Lorsque vous cliquez sur OK, aucune modification n'est apportée à la liste. Voici le fichier de dialogue:

package com.example.classorganizer; 

import android.app.Dialog; 
import android.content.Context; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

class EditListItemDialog extends Dialog implements View.OnClickListener { 

private View editText; 

public EditListItemDialog(Context context) { 
    super(context); 
} 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.edit_text_dialog);//here is your xml with EditText and 'Ok' and 'Cancel' buttons 
    View btnOk = findViewById(R.id.button_ok); 
    editText = findViewById(R.id.edit_text); 
    btnOk.setOnClickListener(this); 
} 

@Override 
public void onClick(View v) { 
    ((TextView) editText).getText().toString();//here is your updated(or not updated) text 
    dismiss(); 
} 

}

Voici la liste contenant du fichier:

package com.example.classorganizer; 


import java.util.ArrayList; 

import android.app.ListActivity; 
import android.content.Context; 
import android.content.Intent; 
import android.database.Cursor; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.BaseAdapter; 
import android.widget.ListView; 
import android.widget.TextView; 

import com.cookbook.data.Constants; 
import com.cookbook.data.MyDB; 




public class Monday extends ListActivity { 



private static final int MyMenu = 0; 
MyDB dba; 
DiaryAdapter myAdapter; 

private class MyDiary{ 
    public MyDiary(String t, String c){ 
     title=t; 
     content=c; 

     ListView list = getListView(); 
     list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { 
       public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { 
        new EditListItemDialog(view.getContext()).show(); 

       return true;  
      } 
     }); 

} 

    public String title; 
    public String content; 

} 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    dba = new MyDB(this); 
    dba.open(); 
    setContentView(R.layout.fragment_monday); 





    super.onCreate(savedInstanceState); 
    myAdapter = new DiaryAdapter(this); 
    this.setListAdapter(myAdapter); 
} 



private class DiaryAdapter extends BaseAdapter { 
    private LayoutInflater mInflater; 
    private ArrayList<MyDiary> fragment_monday; 
    public DiaryAdapter(Context context) { 
     mInflater = LayoutInflater.from(context); 
     fragment_monday = new ArrayList<MyDiary>(); 
     getdata(); 


    } 

    public void getdata(){ 
     Cursor c = dba.getdiaries(); 
     startManagingCursor(c); 
     if(c.moveToFirst()){ 
      do{ 
       String title = 
         c.getString(c.getColumnIndex(Constants.TITLE_NAME)); 
       String content = 
         c.getString(c.getColumnIndex(Constants.CONTENT_NAME)); 

       MyDiary temp = new MyDiary(title,content); 
       fragment_monday.add(temp); 
      } while(c.moveToNext()); 
     } 

    } 



    @Override 
    public int getCount() {return fragment_monday.size();} 
    public MyDiary getItem(int i) {return fragment_monday.get(i);} 
    public long getItemId(int i) {return i;} 
    public View getView(int arg0, View arg1, ViewGroup arg2) { 
     final ViewHolder holder; 

     View v = arg1; 
     if ((v == null) || (v.getTag() == null)) { 
      v = mInflater.inflate(R.layout.diaryrow, null); 
      holder = new ViewHolder(); 
      holder.mTitle = (TextView)v.findViewById(R.id.name); 

      v.setTag(holder); 
     } else { 
      holder = (ViewHolder) v.getTag(); 
     } 

     holder.mdiary = getItem(arg0); 
     holder.mTitle.setText(holder.mdiary.title); 


     v.setTag(holder); 

     return v; 


    } 

    public class ViewHolder { 
     MyDiary mdiary; 
     TextView mTitle; 


    } 

} 




/** Called when the user clicks the Edit button */ 
public void visitDiary(View view) { 
    Intent intent = new Intent(this, Diary.class); 
    startActivity(intent); 
} 
/** Called when the user clicks the back button */ 
public void visitSchedule(View view) { 
    Intent intent = new Intent(this, DisplayScheduleScreen.class); 
    startActivity(intent); 
} 


} 

Voici un fichier xml pour dialogue:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 
<RelativeLayout 
         android:id="@+id/relativeLayout4" 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         android:layout_weight="1" > 
<EditText 
    android:id="@+id/edit_text" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:ems="10" > 

    <requestFocus /> 
</EditText> 

<Button 
    android:id="@+id/button_ok" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="OK" /> 
</RelativeLayout> 
</LinearLayout> 

Je don ne comprend pas pourquoi les lignes ne sont pas mises à jour avec les données saisies via le fichier de dialogue ...

Répondre

0
@Override 
public void onClick(View v) { 
    ((TextView) editText).getText().toString();//here is your updated(or not updated) text 
    dismiss(); 
} 

Vous obtenez la valeur de TextView de la boîte de dialogue, vous avez ajouté les données à la liste.

Mise à jour des données à la liste

fragment_monday.add(((TextView) editText).getText().toString()); 
+0

quand je mets à jour en ajoutant fragment_monday j'obtiens l'erreur qu'il ne peut pas être résolu ... – lisoslaw

+0

votre réponse semble raisonnable mais je reçois une erreur tout en ajoutant fragment_monday – lisoslaw

+0

Je viens Je vous donne un indice (je ne parle pas de fragment_monday exact). Vous devez analyser la liste qui va être ajouter les données entrées. Ensuite, vous pouvez résoudre le problème. – raja

Questions connexes