2012-12-02 58 views
9

先生,我如何刷新我的自定义列表视图使用baseadapter。我不知道该放什么,或者把它放在我的代码中。请帮帮我。在此先感谢如何刷新自定义列表视图使用baseadapter在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); 
      } 
     }); 
} 

} 

这里是我显示我的列表视图

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); 
       } 
       }) 

,这里是我的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

为什么你不用调用adapater的'notifyDataSetChanged()'方法来更新列表视图中的数据吗? – Abhijit

+0

我把它放在哪里?我试着把它放在我的EditDetails类中,比如mcba.notifyDataSetChanged()并没有任何反应。我不知道我是否做得对,但我认为我没有。 –

+1

我认为这是因为你需要在'CustomListView' Activity中使用它,因为ListView是在该Activity中定义的。 'EditDetails'无法访问ListView或其适配器。 – Abhijit

回答

31

两个选择:要么守住了ArrayList参考你传递给构造函数,以便稍后修改实际的列表数据(因为不复制列表,修改适配器外部的数据仍会更新适配器的指针ferencing),或重写适配器以允许列表重置为另一个对象。

无论哪种情况,在ArrayList发生变化后,您必须致电notifyDataSetChanged()更新您的ListView并进行更改。这可以在适配器内部或外部完成。因此,举例来说:

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

这就是我使用notifyDataSetChanged()的方式,谢谢。但它仍然没有刷新我的列表,我想知道这是我的问题在哪里 –

+0

哦,我明白了。它似乎像sqlite rowid从0开始,列表视图在1,所以它不会改变。它现在工作正常。谢谢btw –

+1

@UsuiTakumi:我没有得到你最后的评论,哪些改变使这项工作为你?还没有为我工作。 – astuter

7

在BaseAdapter创建一个自定义的方法

,如:

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

     //and call notifyDataSetChanged 
     notifyDataSetChanged(); 
    } 

,并调用这个函数,其中u要拨打:如

adapterObject.updateAdapter(这里传递ArrayList);

完成。

0

我解决了这个问题,增加这个功能,我的自定义适配器

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

从我的主类创建一个新的光标做一个重新查询数据库,然后通过此功能,发送到我的 自定义适配器。

好运只需使用BaseAdapter

0

,没有必要使用情境

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

只是把这个代码在setOnClickListner

1

谢谢你们与解决方案上面为我工作。我打电话listupdate方法在每一个事件

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

和updatng列表后我还刷新我的按钮动作在每一次触摸。比如我有很多按钮,在我的列表视图项所以每次触摸换款其他款式点击

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); 
    } 

} 

在我的按钮,一个只是一个例子

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只是List<MyModel> talebeList

相关问题