Android Custom Listview,无法在单行中更改元素

时间:2015-06-01 09:58:38

标签: android listview android-listview

我正在使用排球创建一个像自定义列表视图Feed这样的脸谱。

listview位于Fragment上,我使用customListAdapter.java和SearchItem.java类将内容加载到listview。

问题是,当我单击相似按钮或连续保存按钮时,它不仅会更改为喜欢并保存在该行中,还会更改为其他行。

在我的片段中

final ViewHolder holder = new ViewHolder();

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.newsfeed_popular_frag, container, false);            

        listView = (ListView) v.findViewById(R.id.popular_list);
        adapter = new CustomListAdapter(getActivity(), PopularList);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);

在片段的onItemClick

holder.likes_layout = (LinearLayout) parent.findViewById(R.id.likes_layout);
holder.saves_layout = (LinearLayout) parent.findViewById(R.id.saves_layout);

holder.likes_layout.setOnClickListener(new OnClickListener() {          
                public void onClick(View v) {
                    put_like();     
                }                           
            });     

在customListAdapter

public View getView(int position, View convertView, ViewGroup parent) {
        //post_id,name,user_prof_pic,post_description,post_datetime
        if (inflater == null)
            inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);


        if (imageLoader == null)
            imageLoader = AppController.getInstance().getImageLoader();



        if (convertView == null){
            convertView = inflater.inflate(R.layout.newsfeed_list_row, null);

    holder = new ViewHolder();

    holder.comment_layout = (LinearLayout) parent.findViewById(R.id.comment_layout);
    holder.likes_layout = (LinearLayout) parent.findViewById(R.id.likes_layout);
    holder.saves_layout = (LinearLayout) parent.findViewById(R.id.saves_layout);

        convertView.setTag(holder);
        }
        else {
        holder = (ViewHolder) convertView.getTag();
        }

enter image description here

1 个答案:

答案 0 :(得分:0)

请提供您的代码的更多详细信息以获得正确答案。  但正如我在这里看到的那样..

holder.likes_layout.setOnClickListener(new OnClickListener(){

public void onClick(查看v){

   put_like();     
            }                           
        }); 

你没有在你的代码中存储用户状态。所以我建议你使用bean类来实现这个