如何在 ListView 中设置 Counter 应用

Posted

技术标签:

【中文标题】如何在 ListView 中设置 Counter 应用【英文标题】:How set Counter app inside ListView 【发布时间】:2016-03-25 01:45:13 【问题描述】:

首先我创建了一个计数器应用程序,它有两个 Button 和一个 EditText,其中一个 Button 具有增加计数的功能,而另一个具有减少计数的功能,这些结果显示在 EditText 中。 现在我希望将此计数器应用程序放置在 ListView 中。至少需要十个 Counter App 才能放置在 ListView 中。我尝试了一些代码,下面会附上一些图片。

我试过的代码附在下面

反活动

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

计数器模型

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()


自定义适配器

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

【问题讨论】:

【参考方案1】:

尝试改变这个:

final String mEdtCount = myViewHolder.edtCount.toString();

到这里:

final EditText mEdtCount = myViewHolder.edtCount;

【讨论】:

【参考方案2】:

将此编码部分替换为您的代码

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

【讨论】:

你做错了,你没有正确获得edittext,而是你使用了字符串,这就是发生错误的原因。 仍然面临这些错误Error:(61, 89) error: incompatible types: EditText cannot be converted to OnEditorActionListener Error:(74, 10) error: cannot find symbol variable mCount Error:(87, 10) error: cannot find symbol variable mCount Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output Error:Execution failed for task ':app:compileDebugJavaWithJavac'. &gt; Compilation failed; see the compiler error output for details. 为什么要使用 setOnEditorActionListener?它的目的是什么? 点击增减按钮显示计数器结果 setOnEditorActionListener 用于 TextViews,因此最好将 TextView 显示为查看您的代码,它将解决您的问题,因为一切正常

以上是关于如何在 ListView 中设置 Counter 应用的主要内容,如果未能解决你的问题,请参考以下文章

Android:如何在这个 ListView 中设置图像?

如何在 listView 中设置最终滚动的位置?

如何在listview控件中设置表格的宽和高

如何在 ListView 和 AlertDialog 中设置自定义字体?

如何在自定义 ListView 中设置按钮的可见性?

如何在ListView中设置item的点击事件