0

我是新近進入這種編程語言有人可以幫我做什麼。更新列表視圖時,我刪除它的項目

這裏是我的截圖#http://imgur.com/hGQCHpk

ListView控件沒有更新,如果我單擊主頁片段,其中含有的ListView

MainActivity.Java

public class MainActivity extends Activity { 

private DrawerLayout mDrawerLayout; 
private ListView mDrawerList; 
private ActionBarDrawerToggle mDrawerToggle; 

// nav drawer title 
private CharSequence mDrawerTitle; 

// used to store app title 
private CharSequence mTitle; 

// slide menu items 
private String[] navLogout; 
String[] navMenuTitles; 
TypedArray navMenuIcons; 

ArrayList<NavDrawerItem> navDrawerItems; 
private NavDrawerListAdapter adapter; 

@SuppressLint("NewApi") 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.activity_main); 

    mTitle = mDrawerTitle = getTitle(); 

    // load slide menu items 
    navMenuTitles = getResources().getStringArray(
      R.array.nav_drawer_items_not_login); 

    // nav drawer icons from resources 
    navMenuIcons = getResources() 
      .obtainTypedArray(R.array.nav_drawer_icons); 

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
    mDrawerList = (ListView) findViewById(R.id.list_slidermenu); 

    navDrawerItems = new ArrayList<NavDrawerItem>(); 

    // adding nav drawer items to array 
    // Home 
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons 
      .getResourceId(0, -1))); 
    // login 
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons 
      .getResourceId(1, -1))); 
    // register 
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons 
      .getResourceId(2, -1))); 
    // classic brownies 
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons 
      .getResourceId(3, -1))); 
    // cupcakes 
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons 
      .getResourceId(3, -1))); 
    // cookies 
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons 
      .getResourceId(3, -1))); 
    // feedback 
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[6], navMenuIcons 
      .getResourceId(4, -1))); 
    // about 
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[7], navMenuIcons 
      .getResourceId(5, -1))); 

    // Recycle the typed array 
    navMenuIcons.recycle(); 

    mDrawerList.setOnItemClickListener(new SlideMenuClickListener()); 

    // setting the nav drawer list adapter 
    adapter = new NavDrawerListAdapter(getApplicationContext(), 
      navDrawerItems); 
    mDrawerList.setAdapter(adapter); 

    // enabling action bar app icon and behaving it as toggle button 
    getActionBar().setDisplayHomeAsUpEnabled(true); 
    getActionBar().setHomeButtonEnabled(true); 

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, 
      R.drawable.ic_drawer, // nav menu toggle icon 
      R.string.app_name, // nav drawer open - description for 
           // accessibility 
      R.string.app_name // nav drawer close - description for 
           // accessibility 
    ) { 
     public void onDrawerClosed(View view) { 
      getActionBar().setTitle(mTitle); 
      // calling onPrepareOptionsMenu() to show action bar icons 
      invalidateOptionsMenu(); 
     } 

     public void onDrawerOpened(View drawerView) { 
      getActionBar().setTitle(mDrawerTitle); 
      // calling onPrepareOptionsMenu() to hide action bar icons 
      invalidateOptionsMenu(); 
     } 
    }; 
    mDrawerLayout.setDrawerListener(mDrawerToggle); 

    if (savedInstanceState == null) { 

     displayView(0); 

    } 

} 

/** 
* Slide menu item click listener 
* */ 
private class SlideMenuClickListener implements 
     ListView.OnItemClickListener { 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, 
      long id) { 
     // display view for selected nav drawer item 

     displayView(position); 

    } 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.main, menu); 

    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // toggle nav drawer on selecting action bar app icon/title 

    if (mDrawerToggle.onOptionsItemSelected(item)) { 
     return true; 
    } 
    Fragment fragment = null; 
    Fragment newFragment = new AboutFragment(); 
    // Handle action bar actions click 
    switch (item.getItemId()) { 
    case R.id.action_search: 
     Toast.makeText(this, "search selected", Toast.LENGTH_SHORT).show(); 

     break; 

    case R.id.action_shoppingcart: 




      FragmentTransaction transaction = getFragmentManager() 
        .beginTransaction(); 

      transaction.replace(R.id.frame_container, newFragment, 
        "AboutFragment"); 

      transaction.commit(); 

      getActionBar().setTitle("Shopping cart"); 



     return true; 

    default: 
     break; 

    } 
    return true; 
} 

/* * 
* Called when invalidateOptionsMenu() is triggered 
*/ 
@Override 
public boolean onPrepareOptionsMenu(Menu menu) { 
    // if nav drawer is opened, hide the action items 
    boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList); 

    menu.findItem(R.id.action_search).setVisible(!drawerOpen); 
    menu.findItem(R.id.action_shoppingcart).setVisible(!drawerOpen); 

    return super.onPrepareOptionsMenu(menu); 
} 

/** 
* Diplaying fragment view for selected nav drawer list item 
* */ 
private void displayView(int position) { 
    // update the main content by replacing fragments 
    Fragment fragment = null; 
    switch (position) { 
    case 0: 

     fragment = new HomeFragment(); 

     break; 
    case 1: 
     fragment = new LoginFragment(); 
     break; 
    case 2: 
     fragment = new RegisterFragment(); 
     break; 
    case 3: 
     fragment = new ClassicBrowniesFragment(); 

     break; 
    case 4: 
     fragment = new CupcakesFragment(); 
     break; 
    case 5: 
     fragment = new CookiesFragment(); 
     break; 
    case 6: 
     fragment = new FeedbackFragment(); 
     break; 
    case 7: 
     fragment = new AboutFragment(); 
     break; 

    default: 
     break; 
    } 

    if (fragment != null) { 
     FragmentManager fragmentManager = getFragmentManager(); 
     fragmentManager.beginTransaction() 
       .replace(R.id.frame_container, fragment).commit(); 

     // update selected item and title, then close the drawer 

     mDrawerList.setItemChecked(position, true); 
     mDrawerList.setSelection(position); 
     setTitle(navMenuTitles[position]); 
     mDrawerLayout.closeDrawer(mDrawerList); 

    } 

    else { 
     // error in creating fragment 
     Log.e("MainActivity", "Error in creating fragment"); 
    } 
} 

@Override 
public void setTitle(CharSequence title) { 
    mTitle = title; 
    getActionBar().setTitle(mTitle); 
} 

/** 
* When using the ActionBarDrawerToggle, you must call it during 
* onPostCreate() and onConfigurationChanged()... 
*/ 

@Override 
protected void onPostCreate(Bundle savedInstanceState) { 
    super.onPostCreate(savedInstanceState); 
    // Sync the toggle state after onRestoreInstanceState has occurred. 
    mDrawerToggle.syncState(); 
} 

@Override 
public void onConfigurationChanged(Configuration newConfig) { 
    super.onConfigurationChanged(newConfig); 
    // Pass any configuration change to the drawer toggls 
    mDrawerToggle.onConfigurationChanged(newConfig); 
} 
} 

HomeFragment.java

public class HomeFragment extends Fragment implements OnItemClickListener { 

String[] member_names; 
TypedArray product_icons; 
String[] statues; 
String[] contactType; 

List<RowItem> rowItems; 
ListView mylistview; 
CustomAdapter myadapter; 
CustomAdapter newadapter; 

EditText editsearch; 

public HomeFragment() { 
} 

View rootView; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

    rootView = inflater.inflate(R.layout.fragment_home, container, false); 

    rowItems = new ArrayList<RowItem>(); 

    member_names = getResources().getStringArray(R.array.Member_names); 

    product_icons = getResources().obtainTypedArray(R.array.product_icons); 

    statues = getResources().getStringArray(R.array.statues); 

    for (int i = 0; i < member_names.length; i++) { 
     RowItem item = new RowItem(member_names[i], 
       product_icons.getResourceId(i, -1), statues[i]); 
     rowItems.add(item); 
    } 

    mylistview = (ListView) rootView.findViewById(R.id.list); 

    mylistview.setItemsCanFocus(true); 
    // 
    final CustomAdapter adapter = new CustomAdapter(getActivity(), rowItems); 
    mylistview.setAdapter(adapter); 
    product_icons.recycle(); 
    mylistview.setOnItemClickListener(this); 

    mylistview.setScrollingCacheEnabled(false); 

    mylistview.invalidateViews(); 
    adapter.notifyDataSetChanged(); 

    this.setRetainInstance(true); 

    return rootView; 

} 

public void updateList(List adapter) { 
    mylistview = (ListView) rootView.findViewById(R.id.list); 
    mylistview.setAdapter((ListAdapter) adapter); 
} 

@Override 
public void onItemClick(AdapterView<?> parent, View view, int position, 
     long id) { 

    String member_name = rowItems.get(position).getMember_name(); 
    int product_icons = rowItems.get(position).getProfile_pic_id(); 
    String status = rowItems.get(position).getStatus(); 

    RowItem item = rowItems.get(position); 

    Intent intent = new Intent(getActivity(), HomeFragment.class); 
    intent.putExtra("member_name", member_name); 
    intent.putExtra("product_icons", product_icons); 
    intent.putExtra("status", status); 

    rowItems.remove(item); 

    newadapter = new CustomAdapter(getActivity(), rowItems); 
    mylistview.setAdapter(newadapter); 

    newadapter.notifyDataSetChanged(); 

    Toast.makeText(getActivity().getApplicationContext(), 
      "Added to Cart: " + member_name, Toast.LENGTH_SHORT).show(); 

    } 
} 

CustomAdapter.java

public class CustomAdapter extends BaseAdapter { 

HomeFragment home = new HomeFragment(); 

Context context; 
List<RowItem> rowItems; 

public CustomAdapter(Context context, List<RowItem> rowItems) { 
    this.context = context; 
    this.rowItems = rowItems; 

} 

public CustomAdapter() { 
    // TODO Auto-generated constructor stub 
} 

@Override 
public int getCount() { 
    return rowItems.size(); 
} 

@Override 
public Object getItem(int position) { 
    return rowItems.get(position); 
} 

@Override 
public long getItemId(int position) { 
    return rowItems.indexOf(getItem(position)); 
} 

/* private view holder class */ 
private class ViewHolder { 
    ImageView product_icons; 
    TextView member_name; 
    TextView status; 
    Button addtocart; 
} 

ViewHolder holder = null; 

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 

    LayoutInflater mInflater = (LayoutInflater) context 
      .getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 
    if (convertView == null) { 
     convertView = mInflater.inflate(R.layout.list_item, null); 
     holder = new ViewHolder(); 

     holder.member_name = (TextView) convertView 
       .findViewById(R.id.member_name); 
     holder.product_icons = (ImageView) convertView 
       .findViewById(R.id.product_icons); 
     holder.status = (TextView) convertView.findViewById(R.id.status); 
     holder.addtocart = (Button) convertView 
       .findViewById(R.id.btnaddtocart); 

     RowItem row_pos = rowItems.get(position); 

     holder.product_icons.setImageResource(row_pos.getProfile_pic_id()); 
     holder.member_name.setText(row_pos.getMember_name()); 
     holder.status.setText(row_pos.getStatus()); 
     holder.addtocart.setText(" add to cart"); 

     convertView.setTag(holder); 

    } else { 

     holder = (ViewHolder) convertView.getTag(); 

    } 

    return convertView; 
    } 

} 

RowItem.java

public class RowItem { 

private String member_name; 
private int profile_pic_id; 
private String status; 



public RowItem(String member_name, int profile_pic_id, String status) { 

    this.member_name = member_name; 
    this.profile_pic_id = profile_pic_id; 
    this.status = status; 



} 

public String getMember_name() { 
    return member_name; 
} 

public void setMember_name(String member_name) { 
    this.member_name = member_name; 
} 

public int getProfile_pic_id() { 
    return profile_pic_id; 
} 

public void setProfile_pic_id(int profile_pic_id) { 
    this.profile_pic_id = profile_pic_id; 
} 

public String getStatus() { 
    return status; 
} 

public void setStatus(String status) { 
    this.status = status; 
} 

} 

請幫我這件事情對我的軟件工程學科,我希望有人能幫助我。

+0

首先解釋它應該如何工作。我們是否需要通過挖掘所有這些代碼來發現自己? '列表視圖不更新'?該項目不見了,所以listview是好的。 – greenapps 2014-10-02 15:44:13

+0

我想看看listview仍然更新,如果我去其他片段,並再次回到listview包含的主頁片段。 – 2014-10-02 15:52:18

回答

0

看來你只是刪除了保存數據的視圖,而沒有真正改變底層數據集。如果您希望刪除項目,請撥打rowItems.remove(),但要記住片段重新加載時的情況,例如您點擊運行HomeFragment.createView()中代碼的Home導航選項。

在該代碼中,你可以看到rowItems這裏初始化

member_names = getResources().getStringArray(R.array.Member_names); 

for (int i = 0; i < member_names.length; i++) { 
    RowItem item = new RowItem(member_names[i], 
      product_icons.getResourceId(i, -1), statues[i]); 
    rowItems.add(item); 
} 

所以,除非你是從Member_names陣列資源中刪除的項目時,它被刪除,那麼它就會出現在您每次重新創建的片段。

+0

是的,先生,我不知道如何更新變量或字符串數​​組。 :(對於初學者很抱歉 – 2014-10-02 15:54:16

+0

這取決於你是否希望項目被永久刪除,如果你不希望項目在重新啓動整個應用程序後再次出現,那麼我認爲你將不得不使用其他的東西而不是Android資源文件,因爲我認爲它們不能以編程方式進行編輯,或者至少不是很容易。 – 2014-10-02 16:09:43

+0

我想僅在運行時刪除項目,但是當我重新啓動應用程序時,它會再次返回 – 2014-10-02 16:11:57

相關問題