2012-12-02 14 views
9

signore, come posso aggiornare la mia list list personalizzata utilizzando baseadapter. non so cosa posizionare o dove inserirlo nel mio codice. mi aiuti per favore. grazie in anticipocome aggiornare listview personalizzato utilizzando baseadapter in Android

public class EditDetails extends Activity{ 
public String nameChanged; 
public String numChanged; 
public String name; 
public String num; 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.editdetails); 
    final EditText sqlName = (EditText)findViewById(R.id.editName); 
    final EditText sqlNumber = (EditText)findViewById(R.id.editNumber); 
    name = CustomListView.name; 
    num = CustomListView.number; 
    Button bUpdate = (Button)findViewById(R.id.editUpdate); 
    Button bView = (Button)findViewById(R.id.editView); 
    sqlName.setText(name); 
    sqlNumber.setText(num); 

    bUpdate.setOnClickListener(new OnClickListener() { 

     public void onClick(View arg0) { 
      nameChanged = sqlName.getText().toString(); 
      numChanged = sqlNumber.getText().toString(); 
      GroupDb info = new GroupDb(EditDetails.this); 
      info.open(); 
      long rowid = info.getRowId(name, num); 
      info.updateNameNumber(rowid, nameChanged, numChanged); 
      ArrayList<Contact> searchResults = info.getView(); 
      MyCustomBaseAdapter mcba = new MyCustomBaseAdapter(EditDetails.this, searchResults); 
      Toast.makeText(getApplicationContext(), "Update Successful!", Toast.LENGTH_LONG).show(); 
      info.close(); 
      } 
     }); 
    bView.setOnClickListener(new OnClickListener() { 

     public void onClick(View arg0) { 
      Intent intent = new Intent(); 
      intent.setClass(EditDetails.this, CustomListView.class); 

      startActivityForResult(intent, 0); 
      } 
     }); 
} 

} 

qui è dove ho visualizzato il mio ListView

public class CustomListView extends Activity { 
final Context context = this; 
public static String name; 
public static String number; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    GroupDb info = new GroupDb(this); 
    info.open(); 
    ArrayList<Contact> searchResults = info.getView(); 


    final ListView lv = (ListView) findViewById(R.id.srListView); 
    lv.setAdapter(new MyCustomBaseAdapter(this, searchResults)); 
    info.close(); 

    lv.setOnItemClickListener(new OnItemClickListener() { 

     public void onItemClick(AdapterView<?> a, View v, int position, long id) { 
      // TODO Auto-generated method stub 
      Object o = lv.getItemAtPosition(position); 
      final Contact fullObject = (Contact)o; 
      AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context); 
      alertDialogBuilder 
      .setMessage("Select action") 
      .setCancelable(false) 
      .setPositiveButton("Edit", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog,int id) { 
        Toast.makeText(getApplicationContext(), "Edit ", Toast.LENGTH_LONG).show(); 
        name = fullObject.getName(); 
        number = fullObject.getPhoneNumber(); 
        Intent contactIntent = new Intent("myfolder.proj.EDITDETAILS"); 
        startActivity(contactIntent); 
       } 
       }) 

e qui è la mia classe baseadapter

public class MyCustomBaseAdapter extends BaseAdapter { 
private static ArrayList<Contact> searchArrayList; 

private LayoutInflater mInflater; 

public MyCustomBaseAdapter(Context context, ArrayList<Contact> results) { 
    searchArrayList = results; 
    mInflater = LayoutInflater.from(context); 
} 

public int getCount() { 
    return searchArrayList.size(); 
} 

public Object getItem(int position) { 
    return searchArrayList.get(position); 
} 

public long getItemId(int position) { 
    return position; 
} 

public View getView(int position, View convertView, ViewGroup parent) { 
    ViewHolder holder; 
    if (convertView == null) { 
     convertView = mInflater.inflate(R.layout.custom_row_view, null); 
     holder = new ViewHolder(); 
     holder.txtName = (TextView) convertView.findViewById(R.id.name); 

     holder.txtPhone = (TextView) convertView.findViewById(R.id.phone); 

     holder.status = (TextView) convertView.findViewById(R.id.status); 
     convertView.setTag(holder); 
    } else { 
     holder = (ViewHolder) convertView.getTag(); 
    } 

    holder.txtName.setText(searchArrayList.get(position).getName()); 

    holder.txtPhone.setText(searchArrayList.get(position).getPhoneNumber()); 

    holder.status.setText(searchArrayList.get(position).getStatus()); 

    return convertView; 
} 

static class ViewHolder { 
    TextView txtName; 
    TextView txtPhone; 
    TextView status; 
} 
} 
+2

Perché non invochi il metodo 'notifyDataSetChanged()' dell'adattatore per aggiornare i dati nella listview? – Abhijit

+0

dove lo metto? ho provato a metterlo nella mia classe EditDetails come mcba.notifyDataSetChanged() e non succede nulla. non so se lo sto facendo bene, ma penso di no. –

+1

Credo sia perché è necessario utilizzarlo nell'attività 'CustomListView', poiché ListView è definito in quell'attività. 'EditDetails' non ha accesso a ListView o al suo adattatore. – Abhijit

risposta

31

due opzioni: o tenere sul riferimento per la ArrayList che si passato al costruttore in modo da poter modificare i dati dell'elenco effettivo in un secondo momento (poiché l'elenco non viene copiato, la modifica dei dati al di fuori dell'adattatore aggiorna ancora il puntatore che l'adattatore sta ri ferencing), o riscrivere l'adattatore per consentire la reimpostazione della lista su un altro oggetto.

In entrambi i casi, dopo che lo ArrayList è stato modificato, è necessario chiamare notifyDataSetChanged() per aggiornare ListView con le modifiche. Questo può essere fatto all'interno o all'esterno dell'adattatore. Così, per esempio:

public class MyCustomBaseAdapter extends BaseAdapter { 
    //TIP: Don't make this static, that's just a bad idea 
    private ArrayList<Contact> searchArrayList; 

    private LayoutInflater mInflater; 

    public MyCustomBaseAdapter(Context context, ArrayList<Contact> initialResults) { 
     searchArrayList = initialResults; 
     mInflater = LayoutInflater.from(context); 
    } 

    public void updateResults(ArrayList<Contact> results) { 
     searchArrayList = results; 
     //Triggers the list update 
     notifyDataSetChanged(); 
    } 

    /* ...The rest of your code that I failed to copy over... */ 
} 

HTH

+0

è così che uso notifyDataSetChanged(), grazie. ma ancora non aggiorna la mia lista, mi chiedo se è qui che il mio problema si trova –

+0

oh, ho capito. sembra che il rowid sqlite inizi a 0 e la visualizzazione elenco a 1 in modo che non cambi. ora sta funzionando bene. grazie btw –

+1

@UsuiTakumi: non ho ricevuto il tuo ultimo commento, che modifica ha funzionato per te ??? Non funziona ancora per me. – astuter

7

creare un metodo personalizzato in BaseAdapter

come:

public void updateAdapter(ArrayList<Contact> arrylst) { 
     this.arrylst= arrylst; 

     //and call notifyDataSetChanged 
     notifyDataSetChanged(); 
    } 

e questa funzione di chiamata dove u desidera chiamare: ad esempio

adapterObject.updateAdapter (qui passare ArrayList);

terminato.

0

Ho risolto questo problema aggiungendo questa funzione per il mio adattatore personalizzato

public void newCursor(Cursor cursor) 
{ 
    this.cursor=cursor; 
    this.notifyDataSetChanged(); 
} 

Dalla mia classe principale creo un nuovo cursore facendo un re-query per database e poi mando il mio adattatore personalizzato tramite questa funzione.

buona fortuna

0

Semplicemente con BaseAdapter, senza bisogno di utilizzare contesto

listsOfNotes.remove(listsOfNotes.get(position)); 
notifyDataSetChanged(); 

basta inserire il codice su setOnClickListner

1

Grazie ragazzi con soluzione di cui sopra ha funzionato per me. Sto chiamando il metodo listupdate in ogni evento

public void updateResults(List<TalebeDataUser> results) { 
    talebeList = results; 
    //Triggers the list update 
    notifyDataSetChanged(); 
} 

e dopo la lista updatng ho anche la mia azione rinfrescante pulsante in ogni tocco.Per esempio ho un sacco di pulsanti per fare clic nel mio oggetto ListView così ogni tocco chaging altri stile

private void setColor(TalebeDataUser talebeDataUser) { 
    if (talebeDataUser.isVar()) { 
     holder.mVar.setBackgroundResource(R.drawable.aw_secili); 
     holder.mGorevli.setBackgroundResource(R.drawable.aw_shadow); 
     holder.mYok.setBackgroundResource(R.drawable.aw_shadow); 
     holder.mIzinli.setBackgroundResource(R.drawable.aw_shadow); 
     holder.mHatimde.setBackgroundResource(R.drawable.aw_shadow); 
    } else if (talebeDataUser.isGorevli()) { 
     holder.mVar.setBackgroundResource(R.drawable.aw_shadow); 
     holder.mGorevli.setBackgroundResource(R.drawable.aw_secili); 
     holder.mYok.setBackgroundResource(R.drawable.aw_shadow); 
     holder.mIzinli.setBackgroundResource(R.drawable.aw_shadow); 
     holder.mHatimde.setBackgroundResource(R.drawable.aw_shadow); 
    } else if (talebeDataUser.isYok()) { 
     holder.mVar.setBackgroundResource(R.drawable.aw_shadow); 
     holder.mGorevli.setBackgroundResource(R.drawable.aw_shadow); 
     holder.mYok.setBackgroundResource(R.drawable.aw_secili); 
     holder.mIzinli.setBackgroundResource(R.drawable.aw_shadow); 
     holder.mHatimde.setBackgroundResource(R.drawable.aw_shadow); 
    } else if (talebeDataUser.isIzinli()) { 
     holder.mVar.setBackgroundResource(R.drawable.aw_shadow); 
     holder.mGorevli.setBackgroundResource(R.drawable.aw_shadow); 
     holder.mYok.setBackgroundResource(R.drawable.aw_shadow); 
     holder.mIzinli.setBackgroundResource(R.drawable.aw_secili); 
     holder.mHatimde.setBackgroundResource(R.drawable.aw_shadow); 
    } else if (talebeDataUser.isHatimde()) { 
     holder.mVar.setBackgroundResource(R.drawable.aw_shadow); 
     holder.mGorevli.setBackgroundResource(R.drawable.aw_shadow); 
     holder.mYok.setBackgroundResource(R.drawable.aw_shadow); 
     holder.mIzinli.setBackgroundResource(R.drawable.aw_shadow); 
     holder.mHatimde.setBackgroundResource(R.drawable.aw_secili); 
    } 

} 

Solo un esempio all'interno di uno dei miei pulsante

holder.mYok.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      //talebeList.remove(currentTalebe); 
      setOgrenciNameByDurum(talebeList.get(i)); 
      talebeList.get(i).setYok(true); 
      //setOgrenciNameByDurum(currentTalebe); 
      talebeList.get(i).setVar(false); 
      talebeList.get(i).setGorevli(false); 
      talebeList.get(i).setIzinli(false); 
      talebeList.get(i).setHatimde(false); 
      updateResults(talebeList); 
      setColor(talebeList.get(i)); 
      //saveCurrentTalebeOnShare(currentTalebe); 
     } 
    }); 

talebeList è solo di List<MyModel> talebeList

Problemi correlati