Android listfragment ghost元素

时间:2015-01-23 21:01:34

标签: java android xml

我正在使用ListFragments在android中实现一个项目。

我有一个问题,当列表足够小而不覆盖屏幕时(当有很多元素并且屏幕被覆盖时它不会出现)。问题是"幽灵"元素添加在列表的末尾。

例如,实施聊天,我可以看到2个与朋友聊天,第3个"幽灵"元素出现。

The problem

所以前两个元素没问题,但第三个元素不在我的数据库中。

这是我的XML代码:

<LinearLayout
    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:orientation="vertical"
    android:background="@color/background_color_general"
    android:onClick="insideChat"
    tools:context="com.hissacclaim.pfc.pfchissacclaim.chat_messages_list">

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <ImageView
            android:id="@+id/NodeImage"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:paddingTop="10dp"
            android:background="@color/Black"
            android:layout_marginLeft="10dp"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="5dp" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginTop="4dp"
            android:layout_marginRight="10dp"
            android:layout_marginLeft="10dp"
            android:orientation="vertical">


            <TextView
                android:id="@+id/NodeChat_id"
                android:visibility="gone"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/NodeName"
                android:textSize="16dp"
                android:maxLines="1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/NodeLastMessage"
                android:textSize="12dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="3dp"
                android:maxLines="2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>

        <TextView
            android:id="@+id/NodeHissDate"
            android:textSize="10dp"
            android:layout_marginTop="50dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>
</LinearLayout>

这是我的Java代码:

package com.hissacclaim.pfc.pfchissacclaim;


import android.app.Fragment;
import android.app.ListFragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;


/**
 * A simple {@link Fragment} subclass.
 */
public class chats extends ListFragment {

        private MyAdapter mAdapter = null;

        public class HissNode {
            public String NodeChat_id;
            public String NodeName;
            public String NodeLastMessage;
            public Integer NodeImg;
            public String NodeMyDate;
        }

        private static ArrayList<HissNode> mArray = new ArrayList<HissNode>();

        private void setData () throws JSONException {
            JSONArray request = null;

            try{
                DownloadTask dlTask = new DownloadTask();
                dlTask.getChats();
                dlTask.execute();
                request= dlTask.get();
            }catch(Exception e){
                Toast.makeText(getActivity().getApplicationContext(), "Error al recuperar BD data de los chats", Toast.LENGTH_LONG).show();
            }

            JSONObject json_data;
            mArray.clear();

            for(int i=0;i<request.length();i++) {
                json_data = request.getJSONObject(i);
                HissNode mynode = new HissNode();
                mynode.NodeChat_id=json_data.getString("id");
                mynode.NodeName = json_data.getString("first_name")+ " " +json_data.getString("last_name");
                mynode.NodeLastMessage = json_data.getString("last_message");
                mynode.NodeMyDate = json_data.getString("updated_at");
                mynode.NodeImg = R.drawable.ic_launcher;

                mArray.add(mynode);
            }


       /* HissNode mynode3 = new HissNode();
        mynode3.Name="Third";
        mynode3.Description="Tercer nodo muy grande a ver que tal sale esta jodida mierda sabes loco del culo";
        mynode3.Img=R.drawable.ic_launcher;

        mArray.add(mynode3);
*/

        }

        public static class MyAdapter extends BaseAdapter {
            private Context myContext;
            public MyAdapter(Context c) {
                myContext=c;
            }
            public int getCount() {
                return mArray.size();
            }
            public Object getItem(int position) {
                return mArray.get(position);
            }
            public long getItemId(int position) {
                return 0;
            }
            public View getView(int position, View convertView, ViewGroup parent) {
                View view = null;
                if (convertView == null) {
                    LayoutInflater inflater = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    view = inflater.inflate(R.layout.chats_layout,null);
                }
                else {
                    view = convertView;
                }

                //ImageView img = (ImageView) view.findViewById(R.id.imageLanding);
                //img.setImageDrawable(myContext.getResources().getDrawable(mArray.get(position).Img));

                TextView chat_id = (TextView) view.findViewById(R.id.NodeChat_id);
                chat_id.setText(mArray.get(position).NodeChat_id);

                TextView name = (TextView) view.findViewById(R.id.NodeName);
                name.setText(mArray.get(position).NodeName);

                TextView description = (TextView) view.findViewById(R.id.NodeLastMessage);
                description.setText(mArray.get(position).NodeLastMessage);

                TextView mydate = (TextView) view.findViewById(R.id.NodeHissDate);
                mydate.setText(mArray.get(position).NodeMyDate);

                return view;
            }
        }

    public chats() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        try {
            setData();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mAdapter = new MyAdapter(this.getActivity());
        setListAdapter(mAdapter);

        return inflater.inflate(
                R.layout.chats_layout, container, false);
    }

}

任何帮助将不胜感激。感谢。

1 个答案:

答案 0 :(得分:2)

我看到的第一件事是您为ListView项目和片段使用相同的布局文件:

R.layout.chats_layout

绝对不应该这样。