android – ListView子对象可点击的confilct

android – ListView子对象可点击的confilct,第1张

概述在提出问题并花了15天时间来解决这个问题后,我再次寻求帮助和解决方案. 在MainActivity中,我创建了Json下载任务,它从http下载数据,并使用CustomListAdapter.class填充listview. 一切正常. 现在,在listview中我有2个textview,我想要点击,其中一个是“Accept”,textview只是在xml中,它没有填充Adapter或Json. 在提出问题并花了15天时间来解决这个问题后,我再次寻求帮助和解决方案.
在MainActivity中,我创建了Json下载任务,它从http下载数据,并使用Customlistadapter.class填充ListvIEw.
一切正常.
现在,在ListvIEw中我有2个textvIEw,我想要点击,其中一个是“Accept”,textvIEw只是在xml中,它没有填充Adapter或Json.
“接受”应该像这样工作“将文本更改为已接受并更改颜色”,其工作方式与其他所有内容相同.但是当我点击ListvIEw中的第一个“接受”(位置0)时
它会更改其他列表视图项目(位置4,9).这就像我点击了位置4,9上的文本视图.
在第一张图片上点击“接受”之前,第二张图片是点击之后.

///

public class MainActivity extends Activity {protected static final String TAG = null;public ArrayList<FeedItem> FeedList;public ListVIEw FeedListVIEw;private Progressbar progressbar; private Customlistadapter adap; private LayoutInflater mInflater;@OverrIDe public voID onCreate(Bundle savedInstanceState){      super.onCreate(savedInstanceState);      setContentVIEw(R.layout.activity_main);       FeedListVIEw= (ListVIEw) findVIEwByID(R.ID.custom_List);      mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);      String url = "...";      new DownloadfilesTask().execute(url);      getActionbar().setIcon(R.drawable.angel);      progressbar = (Progressbar)findVIEwByID(R.ID.progressbar);       public voID updateList() {    adap = new Customlistadapter(this,FeedList);           FeedListVIEw.setAdapter(adap);            }      public class DownloadfilesTask extends AsyncTask<String,Integer,VoID> {      ///....

Customlistadapter.class

public class Customlistadapter extends BaseAdapter   {private ArrayList<FeedItem> ListData;private LayoutInflater layoutInflater;private Context mContext;private ArrayList<String> data;protected ListVIEw FeedListVIEw;ArrayList<HashMap<String,String>> List;public Customlistadapter(Context context,ArrayList<FeedItem> ListData){    this.ListData = ListData;    layoutInflater = (LayoutInflater) context            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);    mContext = context;    data = new ArrayList<String>();    for (int i = 0; i < 10; i++) {        data.add("Sample Text " + String.valueOf(i));    }}@OverrIDepublic int getCount(){    return ListData.size();}@OverrIDepublic Object getItem(int position){    return ListData.get(position);}@OverrIDepublic long getItemID(int position){    return position;}public VIEw getVIEw( int position,VIEw convertVIEw,VIEwGroup parent){ final VIEwHolder holder; VIEw row=convertVIEw;    if ((row == null) || (row.getTag()==null)) {     convertVIEw = layoutInflater.inflate(R.layout.List_row_layout,null);     holder = new VIEwHolder();     holder.headlineVIEw = (TextVIEw)convertVIEw.findVIEwByID(R.ID.name);     holder.reportedDateVIEw = (TextVIEw) convertVIEw.findVIEwByID(R.ID.confID);     holder.accept= (TextVIEw) convertVIEw.findVIEwByID(R.ID.acceptTV);     convertVIEw.setTag(holder);    }    else    {        holder = (VIEwHolder) convertVIEw.getTag();    }   final FeedItem newsItem = (FeedItem) ListData.get(position);    holder.accept.setFocusable(true);    holder.accept.setClickable(true);    holder.headlineVIEw.setText(HTML.fromHTML(newsItem.getTitle()));    holder.reportedDateVIEw.setText(HTML.fromHTML(newsItem.getContent()));    holder.accept.setonClickListener(new VIEw.OnClickListener() {        @OverrIDe        public voID onClick(VIEw arg0) {                    holder.accept.setText(HTML.fromHTML(newsItem.getContent()));        }    });    return convertVIEw;}static class VIEwHolder{    TextVIEw accept;    TextVIEw headlineVIEw;    TextVIEw reportedDateVIEw;    ImageVIEw imageVIEw;    FeedItem newsItem;}
解决方法 您需要了解ListvIEw循环机制的工作原理

How ListView’s recycling mechanism works

使用模型类.假设您已经拥有以下内容

public class FeedItem {String Title,content;public String getTitle() {    return Title;}public voID setTitle(String Title) {    this.Title = Title;}public String getContent() {    return content;}public voID setContent(String content) {    this.content = content;}}

在getVIEw中

holder.accept.setText(ListData.get(position).getContent()); holder.accept.setTag(position);holder.accept.setonClickListener(mClickListener);

然后

private OnClickListener mClickListener = new OnClickListener() {public voID onClick(VIEw v) {    int pos = (Integer) v.getTag();    FeedItem newsItem = (FeedItem) ListData.get(pos);    newsItem.setContent("Accepted");    Customlistadapter.this.notifyDataSetChanged();}};

Exaplanation:

您使用具有getter和setter的模型类.

将setTag设置为具有位置的按钮.在onClick中,您获得标记即位置并相应地更改内容.您通过调用适配器上的notifyDataSetChanged@R_877_6419@视图.

为了别人的利益,这里有一个例子

public class MainActivity extends Activity {   ArrayList<Holder> List = new ArrayList<Holder>();   ListVIEw lv;   Customlistadapter cus;    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_main);        lv = (ListVIEw) findVIEwByID(R.ID.ListVIEw1);        for(int i=0;i<10;i++)        {            Holder h = new Holder();            h.setTitle("Title"+i);            h.setContent("Content"+i);            h.setcolor(color.BLACK);            List.add(h);        }        cus = new Customlistadapter(this,List);        lv.setAdapter(cus);    }}

型号类持有人

public class Holder {    String Title,content;    int color;    public int getcolor() {return color;    public voID setcolor(int color) {this.color = color;    }    public String getTitle() {        return Title;    }    public voID setTitle(String Title) {        this.Title = Title;    }    public String getContent() {        return content;    }    public voID setContent(String content) {        this.content = content;    }}

Customlistadapter

public class Customlistadapter extends BaseAdapter{    LayoutInflater inflater;    ArrayList<Holder> List;    public Customlistadapter(MainActivity mainActivity,ArrayList<Holder> List) {        inflater = LayoutInflater.from(mainActivity);        this.List =List;    }    @OverrIDe    public int getCount() {        // Todo auto-generated method stub        return List.size();    }    @OverrIDe    public Object getItem(int position) {        // Todo auto-generated method stub        return position;    }    @OverrIDe    public long getItemID(int position) {        // Todo auto-generated method stub        return position;    }    public VIEw getVIEw(int position,VIEwGroup parent) {         VIEwHolder holder;         if (convertVIEw == null) {             convertVIEw = inflater.inflate(R.layout.List_item,parent,false);            holder = new VIEwHolder();             holder.tv = (TextVIEw) convertVIEw.findVIEwByID(R.ID.textVIEw1);             holder.b = (button) convertVIEw.findVIEwByID(R.ID.button1);           convertVIEw.setTag(holder);        } else {            holder = (VIEwHolder) convertVIEw.getTag();        }        Holder h = List.get(position);       holder.tv.setText(h.getTitle());       holder.b.setText(h.getContent());       holder.b.setTextcolor(h.getcolor());       holder.b.setonClickListener(mClickListener);        holder.b.setTag(position);       return convertVIEw; }     private OnClickListener mClickListener = new OnClickListener() {            public voID onClick(VIEw v) {                int pos = (Integer) v.getTag();                Holder h = (Holder) List.get(pos);                h.setContent("Accepted");                    h.setcolor(color.BLUE);                Customlistadapter.this.notifyDataSetChanged();            }            };    static class VIEwHolder    {        TextVIEw tv;        button b;    }}

List_item.xml

<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent" >    <button        androID:ID="@+ID/button1"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:layout_alignParentRight="true"        androID:layout_alignParenttop="true"        androID:layout_marginRight="40dp"        androID:text="button" />    <TextVIEw        androID:ID="@+ID/textVIEw1"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:layout_alignBottom="@+ID/button1"        androID:layout_alignParentleft="true"        androID:layout_marginleft="22dp"        androID:text="TextVIEw" /></relativeLayout>

activity_main.xml中

<relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:tools="http://schemas.androID.com/tools"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    androID:paddingBottom="@dimen/activity_vertical_margin"    androID:paddingleft="@dimen/activity_horizontal_margin"    androID:paddingRight="@dimen/activity_horizontal_margin"    androID:paddingtop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <ListVIEw        androID:ID="@+ID/ListVIEw1"        androID:layout_wIDth="match_parent"        androID:layout_height="wrap_content"        >    </ListVIEw></relativeLayout>

快照

单击第1行和第5行的按钮,将其更改为“已接受”并为蓝色.

总结

以上是内存溢出为你收集整理的android – ListView子对象可点击的confilct全部内容,希望文章能够帮你解决android – ListView子对象可点击的confilct所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1122161.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存