我有一个ListView,在每个列表视图中我都有一个按钮和一个TextView。单击按钮我想增加TextView中显示的te值。我能够在TextView中增加值,但问题是该值在TextView的下一行中从之前TextView中保留的位置开始递增。
但我想从每行1开始。我怎么能这样做?
这是我的代码
MainActivitty.java
public class MainActivity extends AppCompatActivity {
ListView lv;
TextView t;
int q=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv= (ListView) findViewById(R.id.list);
lv.setAdapter(new MyAd(this));
}
public void hand(View v)
{
final RelativeLayout rl= (RelativeLayout) v.getParent();
Button b= (Button) rl.getChildAt(2);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
t= (TextView) rl.getChildAt(1);
quantity();
}
});
}
public void quantity()
{
q=q+1;
t.setText(""+q);
}
}
修改1:
我的问题在某种程度上得到了解决。我能够为每个按钮单独增加单独的值以获得单独的行,但该值不会反映在文本视图中。如何在文本视图中反映出来?
我的代码:
MainActivity.java
public class MainActivity extends AppCompatActivity implements MyAd.customButtonListener {
ListView lv;
int q=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv= (ListView) findViewById(R.id.list);
MyAd ma=new MyAd(this);
ma.setCustomButtonListner(this);
lv.setAdapter(ma);
}
@Override
public void onButtonClickListner(int position,TextView t) {
q=q+1;
t.setText(""+q);
}
}
MyAd.java
class MyAd extends BaseAdapter {
int q=0,a[];
List<ListItem> li=new ArrayList<>();
Context c;
MyViewHolder mvh;
customButtonListener customListner;
MyAd(Context c)
{
this.c=c;
Resources res=c.getResources();
String title[]=res.getStringArray(R.array.title);
a=new int[title.length];
for(int i=0;i<title.length;i++)
{
li.add(new ListItem(title[i]));
}
}
public interface customButtonListener
{
void onButtonClickListner(int position, TextView t);
}
public void setCustomButtonListner(customButtonListener listener)
{
this.customListner = listener;
}
@Override
public int getCount() {
return li.size();
}
@Override
public Object getItem(int i) {
return i;
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(final int position, View view, ViewGroup parent) {
View row=view;
if(row==null) {
LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.list_item, parent, false);
mvh=new MyViewHolder();
mvh.txt=row.findViewById(R.id.txt);
mvh.quan=row.findViewById(R.id.quan);
mvh.bt=row.findViewById(R.id.button);
row.setTag(mvh);
}
else
{
mvh= (MyViewHolder) row.getTag();
}
ListItem sr=li.get(position);
//String s=sr.title;
mvh.txt.setText(sr.title);
mvh.bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
q=a[position];
mvh.quan.setText(String.valueOf(q++));
a[position]=q;
System.out.println("position:"+position+"-"+q);
notifyDataSetChanged(); }
});
return row;
}
class MyViewHolder
{
TextView txt,quan;
Button bt;
}
}
ListItem.java
public class ListItem {
String title;
ListItem(String title)
{
this.title=title;
}
}
答案 0 :(得分:1)
除非您发布自定义适配器代码,但我能理解您的问题,否则我无法完全告诉您代码有什么问题:
如果我能正确理解您的问题,此tutorial可能会帮助您实现所需目标。