如何使用if语句填充ListView?

时间:2017-10-27 23:46:15

标签: android listview if-statement

基本上这就是我想要发生的事情:

if (condition is met){
    //add string to listview to be displayed
}


if (condition is met){
    //remove string from listview and replace with a different string
}

将我对listview的代码作为我的MainActivity。

  @Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    clock_TV = (TextView) findViewById(R.id.clock_TV);
    timeOfDay_TV = (TextView) findViewById(R.id.timeOfDay_TV);
    characterButton = (Button) findViewById(R.id.characterButton);
    updatePerSec();

  ArrayAdapter adapter = new ArrayAdapter<String>(this,R.layout.inventory_list_view, inventoryCatagories);
  ListView listView = (ListView) findViewById(R.id.listview);
  listView.setAdapter(adapter);

}

2 个答案:

答案 0 :(得分:0)

如果您正在操作列表视图后面的数据,最好的方法是在填充之前进行所有数据过滤。如果您需要在呈现数据后更改数据,请记得在适配器上调用notifydatasetchanged

答案 1 :(得分:0)

如果您在列表视图中使用RecylerAdapter,那么会发生什么以及为什么会发生ListView中的项目绑定到视图持有者,如果添加了项目,则Recyleradapter会更新ListView,如果项目被删除,则使用RecyclerAdapter更新列表视图我们将首先发布ListActivity的代码,然后回收不确定数据来自何处的RecyclerAdapter代码是否在SQLite DB中

public class ListActivity extends AppCompatActivity {

DBHelper helper;
static List<DBModel>dbList;
RecyclerView mRecyclerView;
private static RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list);

    addListenerOnButtonAdd();
    TextView tvNoData = (TextView)findViewById(R.id.tvNoData);

    setTitle("");// This sets the title of the toolbar
    Toolbar topToolBar = (Toolbar)findViewById(R.id.toolbar);
    setSupportActionBar(topToolBar);
    //topToolBar.setLogo(R.drawable.keyss);// See Notes in MainActivity

    setRequestedOrientation( ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    // Line of code above is used when you navigate back from DetailsActivity with
    // soft keyboard visible on DA IT WILL NOT BECOME VISIBLE ON ListActivity
    // Data Entry NOT required in ListActivity
    // also SEE settings in Styles for this issue;


    helper = new DBHelper(this);
    dbList = new ArrayList<>();
    dbList = helper.getDataFromDB();

    mRecyclerView = (RecyclerView)findViewById(R.id.recycleview);
    mRecyclerView.setHasFixedSize(true);

    // use a linear layout manager
    mLayoutManager = new LinearLayoutManager(this);
    mRecyclerView.setLayoutManager(mLayoutManager);

    // Code below defines the adapter
    mAdapter = new RecyclerAdapter(this,dbList);
    mRecyclerView.setAdapter(mAdapter);

    int sz = dbList.size();
    if(sz == 0){/* No Data in DB let the user know better than a Toast or Blank Screen*/
        tvNoData.setVisibility(View.VISIBLE);
        tvNoData.setText("No Data");
    }
}

// This method is called from DetailsActivity and notifies Recycler View that the DB was changed
// and the method makes the same changes to the Recycler View kind of a sync of DB and Recycler View
public static void removeListRow(int position) {
    dbList.remove(position);
    mAdapter.notifyItemRemoved(position);
    mAdapter.notifyItemRangeChanged(position, dbList.size());
}

/* this BUTTON is on the ToolBar click to ADD new record */
private void addListenerOnButtonAdd() {
    // Navigate to DetailsActivity to ADD new DATA
    Toolbar tb = (Toolbar) findViewById( R.id.toolbar );
    setSupportActionBar( tb );

    tb.findViewById( R.id.btnAdd ).setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intentSP = new Intent(ListActivity.this, DetailsActivity.class );
            Bundle extras = new Bundle();
            extras.putString("FROM_LIST_ACTIVITY","true" );
            intentSP.putExtras(extras);
            startActivity( intentSP );
        }
    } );
}

public void onBackPressed(){
    Intent intent = new Intent( ListActivity.this, MainActivity.class );
    startActivity( intent );
}

}

RecyclerAdapter

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {

static List<DBModel> dbList;
static private Context context;
int sz;

RecyclerAdapter(Context context, List<DBModel> dbList) {

    RecyclerAdapter.dbList = new ArrayList<>();
    RecyclerAdapter.context = context;
    RecyclerAdapter.dbList = dbList;
}

@Override
public RecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_row, null);
    // create ViewHolder
    ViewHolder viewHolder = new ViewHolder(itemLayoutView);

    return viewHolder;
}

@Override
public void onBindViewHolder(RecyclerAdapter.ViewHolder holder, int position) {
    holder.rowid.setText(String.valueOf(dbList.get(position).getRowid()));
    holder.station.setText(dbList.get(position).getStation_Name());
    System.out.println("RecyclerAdapter BindViewHolder FIRST position "+position);
}

@Override
public int getItemCount() {
    return dbList.size();
}

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    public TextView station, rowid;

    public ViewHolder(View itemLayoutView) {
        super(itemLayoutView);

        rowid = (TextView) itemLayoutView.findViewById(R.id.rvROWID);
        station = (TextView) itemLayoutView.findViewById(R.id.rvSTATION);
        // Attach a click listener to the entire row view
        itemLayoutView.setOnClickListener(this);

    }

   @Override // When an item in DetailsActivity is touched (selected) the RecyclerView has
    //  a OnClickListener attached in the above Code that implements the method below
    public void onClick(View v) {

       Intent intentN = new Intent(context, DetailsActivity.class);
       Bundle extras = new Bundle();
       extras.putInt("POSITION", getAdapterPosition());
       extras.putString("FROM_LIST_ACTIVITY", "false");
       intentN.putExtras(extras);
       context.startActivity(intentN);
   }
}