动态更改自定义列表视图中存在的textview的文本

时间:2015-12-27 20:10:24

标签: android listview button onclicklistener custom-lists

这是列表类。

public class list {
public String title;
public String no;

public list( String title,String no) {
    super();
    this.title = title;
    this.no=no;
}
public String getTitle() {
    return title;
}
public void setTitle(String title) {
    this.title = title;
}
public String getNo() {
    return no;
}
public void setNo(String  no) {
    this.no = no;
}

@Override
public String toString() {
    return title + "\n" ;
}

}

这是list_Adapter类。

 public class list_Adapter extends ArrayAdapter<list> {

Context context;
int layoutResourceId;


public list_Adapter(Context context, int layoutResourceId, List<list> items) {
    super(context, layoutResourceId, items);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
}

/*private view holder class*/
private class ViewHolder {
    TextView txtTitle;
    TextView txtNo;
}
ViewHolder holder = null;

public View getView(int position, View convertView, ViewGroup parent) {
    final list lists = getItem(position);

    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.items_row, null);
        holder = new ViewHolder();
        holder.txtTitle = (TextView) convertView.findViewById(R.id.textTitle);
        holder.txtNo=(TextView) convertView.findViewById(R.id.no);
        convertView.setTag(holder);
    } else
        holder = (ViewHolder) convertView.getTag();

    Button addi=(Button)convertView.findViewById(R.id.addi);
    addi.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
        String s;
        s= holder.txtNo.getText().toString();
            int c=Integer.parseInt(s);
            c=c+1;
            Log.e("rgr",Integer.toString(c));
            lists.setNo(Integer.toString(c));
            holder.txtNo.setText(lists.getNo());
        }
    });


    holder.txtTitle.setText(lists.getTitle());
    holder.txtNo.setText(lists.getNo());
    return convertView;
}

  }

主类 -

 public class Main extends AppCompatActivity implements FragmentDrawer.FragmentDrawerListener, AdapterView.OnItemClickListener {

private Toolbar toolbar;
private FragmentDrawer drawerFragment;
private ListView listView1;
static int position;
List<list> rowitems;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cart);
     Intent intent=getIntent();
     position=intent.getExtras().getInt("position");


    toolbar = (Toolbar) findViewById(R.id.tool_bar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowHomeEnabled(true);

    rowitems = new ArrayList<list>();

    listView1 = (ListView) findViewById(R.id.listView);
    list_Adapter adapter = new list_Adapter(this,
            R.layout.items_row, MyAdaptertwo.rowitems);
    listView1.setAdapter(adapter);
    listView1.setOnItemClickListener(this);
    drawerFragment = (FragmentDrawer)
            getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
    drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), toolbar);
    drawerFragment.setDrawerListener(this);

}
public void onItemClick(AdapterView<?> parent, View view, int position,
                        long id) {
   }


public void onDrawerItemSelected(View view, int position) {

}

在list_Adapter类中有一个按钮。单击按钮时,textview txtNo中的数字应增加1。在logcat中,我可以看到值增加但textview中的文本保持不变。我想要在单击按钮时更改文本。

1 个答案:

答案 0 :(得分:0)

在list_Adapter类中,替换

holder.txtNo.setText(lists.getNo());

list_Adapter.this.notifyDatasetChanged();

这告诉列表,适配器中的数据已更改,因此它应自行刷新。通过刷新,还可以重建持有者对象,因此不必更新那里的数据。