【问题标题】:Edit Text in Custom Listview Adapter Loses Value on Scroll自定义列表视图适配器中的编辑文本在滚动时丢失值
【发布时间】:2016-10-26 20:22:11
【问题描述】:

我有一个自定义列表视图适配器,我的编辑文本会自行更改其值,如果某一行填充了某个值,它也会出现在所有其他行上。

这是我的适配器 使用系统; 使用 System.Collections.Generic; 使用 System.Linq; 使用 System.Text; 使用 System.Data.SqlClient;

using Android.App;
 using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;

namespace Kites
{
public class Marks 
{
    // add any if you need more

    public string StudentName { get; set; }
    public string MarksScored { get; set; }
}
public class MarksListViewAdapter : BaseAdapter<Marks>
{
    private List<Marks> mstuduentmarks;

    private Context mcontext;

    public MarksListViewAdapter (Context context, List<Marks> stud)
    {
        mstuduentmarks = stud;
        mcontext = context;
    }

    public override int Count 
    {
        get 
        {
            return mstuduentmarks.Count;
            //              return mattendence.Count;

        }
    }
    public override long GetItemId (int position)
    {
        return position;
    }
    public override Marks this[int position] 
    {
        get 
        {
            return mstuduentmarks [position];
            //              return mattendence [position];

        }
    }



    public override View GetView (int position, View convertView, ViewGroup parent)
    {
        View view = convertView;
        if (view == null) // otherwise create a new one
            view = LayoutInflater.From(mcontext).Inflate(Resource.Layout.listview_Marks, null, false);

        // set view properties to reflect data for the given row
        TextView txtStudent = view.FindViewById<TextView>(Resource.Id.textStudentNameTeacherMarks);
        txtStudent.Text = mstuduentmarks[position].StudentName;
        EditText txtMarks = view.FindViewById<EditText> (Resource.Id.editTextTeacherMarks);
        txtMarks.Text = mstuduentmarks[position].MarksScored;

        txtMarks.TextChanged += (sender, e) => 
        {
            mstuduentmarks[position].MarksScored = txtMarks.Text;
        };


        return view;
    }






}
}

【问题讨论】:

  • 尝试它在构建视图支架中的 Recycler 视图,这将对您的情况有所帮助。
  • 先生,我该怎么做?

标签: c# android xamarin xamarin.android


【解决方案1】:

按照代码,您可以轻松地处理任何元素的点击事件:)

public class HomeRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {


private List<String> mItemList;

RecyclerView mRecyclerView;

Context context;
HomeItemViewHolder mHomeItemholder;

public HomeRecyclerAdapter(List<String> itemList) {
    mItemList = itemList;
}


public class HomeItemViewHolder extends RecyclerView.ViewHolder {

    ImageView mImage;


    public HomeItemViewHolder(View parent) {
        super(parent);

        mImage = (ImageView) parent.findViewById(R.id.Image); 

    }

}

public HomeRecyclerAdapter(List<String> itemList, Context context, RecyclerView mRecyclerView) {
    this.mItemList = itemList;
    this.context = context;
    this.mRecyclerView = mRecyclerView;


}


@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    Context context = parent.getContext();

    final View view = LayoutInflater.from(context).inflate(R.layout.item_home, parent, false);


    return new HomeItemViewHolder(view);

}


@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {

    mHomeItemholder = (HomeItemholder) viewHolder;         


    mHomeItemholder.mImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            //Handle your code
        }
    });

}       



public int getBasicItemCount() {
    return mItemList == null ? 0 : mItemList.size();
}


@Override
public int getItemCount() {
    return getBasicItemCount(); // header

} 

}

【讨论】:

  • 很抱歉,但我不知道如何使它与我的代码一起工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多