首先我创建了一个计数器应用程序,它有两个Button和一个EditText,其中一个Button有增加计数的功能,而另一个有减少计数,这些结果显示在EditText中。 现在我希望这个计数器App放在ListView中。 Atleast需要将十个Counter App放在ListView中。我尝试了一些代码,下面附有一些图片。
我试过的代码附在下面
CounterActivity
public class CounterActivity extends AppCompatActivity {
private ListView lstCount;
private CustomAdapter adapter;
private ArrayList<CountModel> mCount = new ArrayList<CountModel>();
final Context context = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_counter);
lstCount = (ListView)findViewById(R.id.lst_counter);
adapter = new CustomAdapter(this,mCount);
lstCount.setAdapter(adapter);
}}
CounterModel
public class CountModel {
public EditText edtCount;
public Button btnDec;
public Button btnInc;
public EditText getEdtCount(){
return edtCount;
}
public void setEdtCount(EditText edtCount){
this.edtCount = edtCount;
}
public Button getBtnDec(){
return btnDec;
}
public void setBtnDec(Button btnDec){
this.btnDec = btnDec;
}
public Button getBtnInc(){
return btnInc;
}
public void setBtnInc(Button btnInc){
this.btnInc = btnInc;
}
public CountModel(Button btnDec ,EditText edtCount , Button btnInc){
this.btnDec = btnDec;
this.edtCount = edtCount;
this.btnInc = btnInc;
}
public CountModel(){
}}
CustomAdapter
public class CustomAdapter extends BaseAdapter {
int counter = 0;
int maxcounter=20;
private ArrayList<CountModel>mCount = new ArrayList<CountModel>();
Context context;
public CustomAdapter(Context context, ArrayList<CountModel> count) {
this.context = context;
this.mCount=count;
}
@Override
public int getCount() {
return mCount.size();
}
@Override
public Object getItem(int position) {
return mCount.get(position);
}
@Override
public long getItemId(int position) {
return mCount.indexOf(getItemId(position));
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
MyViewHolder myViewHolder = null;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.activity_counter, null);
myViewHolder = new MyViewHolder();
myViewHolder.btnDec = (Button) convertView.findViewById(R.id.btn_dec);
myViewHolder.btnInc = (Button) convertView.findViewById(R.id.btn_inc);
myViewHolder.edtCount = (EditText) convertView.findViewById(R.id.edt_count);
convertView.setTag(myViewHolder);
} else {
myViewHolder = (MyViewHolder) convertView.getTag();
}
final String mEdtCount = myViewHolder.edtCount.toString();
myViewHolder.edtCount.setOnEditorActionListener(mCount.get(position).getEdtCount());
myViewHolder.btnDec.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (counter >0 && counter <=20) {
counter--;
mEdtCount.setText(Integer.toString(counter));
}else
if (counter<0){
mEdtCount.setText(Integer.toString(counter));
}
}
}.mCount.get(position).getBtnDec());
myViewHolder.btnInc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
counter++;
if (counter >0 && counter <=20) {
mEdtCount.setText(Integer.toString(counter));
}else if(counter >20){
counter--;
mEdtCount.setText(Integer.toString(maxcounter));
}
}
}.mCount.get(position).getBtnInc());
return convertView;
}
private class MyViewHolder {
Button btnInc;
Button btnDec;
EditText edtCount;
}}
我正面临这些错误
C:\Users\Rahul\Documents\AndroidStudioWorkSpace\CounterInListView\app\src\main\java\com\example\rahul\counterinlistview\CustomAdapter.java
Error:(61, 21) error: cannot find symbol variable mEdtCount
Error:(67, 30) error: cannot find symbol method setText(String)
Error:(70, 30) error: cannot find symbol method setText(String)
Error:(74, 10) error: cannot find symbol variable mCount
Error:(80, 30) error: cannot find symbol method setText(String)
Error:(83, 30) error: cannot find symbol method setText(String)
Error:(87, 10) error: cannot find symbol variable mCount
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
Information:BUILD FAILED
答案 0 :(得分:0)
尝试更改此内容:
final String mEdtCount = myViewHolder.edtCount.toString();
到此:
final EditText mEdtCount = myViewHolder.edtCount;
答案 1 :(得分:0)
将此编码部分替换为您的代码
final String mEdtCount = myViewHolder.edtCount.getText().toString();
myViewHolder.edtCount.setOnEditorActionListener(mCount.get(position).getEdtCount());
myViewHolder.btnDec.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (counter >0 && counter <=20) {
counter--;
myViewHolder.edtCount.setText(Integer.toString(counter));
}else
if (counter<0){
myViewHolder.edtCount.setText(Integer.toString(counter));
}
}
}.mCount.get(position).getBtnDec());
myViewHolder.btnInc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
counter++;
if (counter >0 && counter <=20) {
myViewHolder.edtCount.setText(Integer.toString(counter));
}else if(counter >20){
counter--;
myViewHolder.edtCount.setText(Integer.toString(maxcounter));
}
}
}.mCount.get(position).getBtnInc());