简体   繁体   English

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

[英]Change text of a textview present in a custom listview ,dynamically

This is the list class. 这是列表类。

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" ;
}

}

Here is the list_Adapter class. 这是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;
}

  }

Main Class - 主类-

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

}

In the list_Adapter class there is a button. 在list_Adapter类中,有一个按钮。 On click of the button the number present in the textview txtNo should increase by one. 单击按钮后,textview txtNo中显示的数字应增加一。 In the logcat I can see the value increasing but the text in the textview is remaining constant. 在logcat中,我可以看到该值正在增加,但是textview中的文本保持不变。 I want the text to change as and when the button is clicked. 我希望在单击按钮时更改文本。

In your list_Adapter class, replace 在您的list_Adapter类中,替换

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

with

list_Adapter.this.notifyDatasetChanged();

This tells the list, that data in the adapter has changed, and so it should refresh itself. 这告诉列表,适配器中的数据已更改,因此应该刷新自身。 By refreshing, the holder objects are also rebuilt, so it is not necessary to update the data there. 通过刷新,还可以重建Holder对象,因此不必在那里更新数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM