问题ID:资源类型

时间:2019-06-05 18:06:58

标签: java android

我想在数组适配器上放置一些参数,但是我的问题是:我没有布局,希望我们可以将其视为listView的资源,在一个布局(ConstraintLayout)中只有一些小部件。 / p>

我试图在ConstraintLayout内创建一个内部布局,但仍然没有结果。

public void populateListView(){

        mDataBaseHelper = new DataBaseHelper(MainActivity.this);
        list = (ListView) findViewById(R.id.myListId);
        data = mDataBaseHelper.getData();

        if (mListAdapter == null){
            mListAdapter = new ArrayAdapter<>(this,R.id.myListId,R.id.checkBox,data); //the probleme is the R.id.myListId
            list.setAdapter(mListAdapter);

        }

}

我想将数据库中的数据设置到此列表视图中,它们应显示为复选框。

如果您想查看整个代码:

import android.widget.Toast;

import java.util.ArrayList;

公共类MainActivity扩展了AppCompatActivity {

DataBaseHelper mDataBaseHelper;
ListView list;
EditText mEditText;
ListAdapter mListAdapter;
ArrayList<String> data;
CheckBox mCheckBox ;
private static final String TAG = "MainActivity";

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

    mDataBaseHelper = new DataBaseHelper(this);
    populateListView();

    mCheckBox =(CheckBox) findViewById(R.id.checkBox);

    Button button = (Button)findViewById(R.id.enterId);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d(TAG, "onClick: button click");
            mEditText =(EditText)findViewById(R.id.editText);
            String str = mEditText.getText().toString();
            if(str.length()!=0){
                addData(str);
                mEditText.setText("");
            }
            populateListView();
        }});

}
 public void addData(String newEntry) {
    boolean insertData = mDataBaseHelper.addData(newEntry);
    if (insertData==true){
        toastMessage("Data successfully Inserted");
    }else{
        toastMessage("Something went Wrong");
    }
}



public void populateListView() {

        mDataBaseHelper = new DataBaseHelper(MainActivity.this);
        list = (ListView) findViewById(R.id.myListId);
        data = mDataBaseHelper.getData();

        if (mListAdapter == null){
            mListAdapter = new ArrayAdapter<>(this,R.id.myListId,R.id.checkBox,data);
            list.setAdapter(mListAdapter);

        }

}
private void toastMessage(String message){
    Toast.makeText(this,message,Toast.LENGTH_SHORT).show();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.parm_menu, menu);
        return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    try {

        int id = item.getItemId();
        switch (id) {
            case R.id.finish:
                Intent intent = new Intent(this, FinishClass.class);
                startActivity(intent);
            break;
            case R.id.delete:
                mDataBaseHelper.deleteAll();
                refresh();
                break;
        }
        return super.onOptionsItemSelected(item);
    }catch (Exception t){
        Log.e(TAG, "onOptionsItemSelected: error "+ t.getStackTrace() );
        return false ;
    }
}

public void refresh(){
    data.clear();
    ListAdapter listAdapter = new ArrayAdapter(MainActivity.this, R.layout.list_items, R.id.checkBox, data);
    list = (ListView) findViewById(R.id.myListId);
    list.setAdapter(listAdapter);
}
public void onCheckboxClicked(View view){
    boolean checked = ((CheckBox)view).isChecked();
    switch (view.getId()){
        case R.id.checkBox:
            if (checked){
                toastMessage("is checked");

                }
            else{
                toastMessage("unchecked");
            }
            break;
    }
}

}

0 个答案:

没有答案