Android listfragment ghost element



I am implementing a project in android using ListFragments.


I have a problem when the list is small enough that is does not cover the screen (when there are lots of elements and the screen gets covered it does not appear). The problem is that a "ghost" element is added at the end of the list.


For example, implementing a chat, I can see 2 chats with friends and a third "ghost" element appears.


The problem


So the first two elements are ok but the third one is not in my DB.


Here is my XML code:



<LinearLayout
xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
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>


Here is my Java Code:



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

}


Any help would be appreciated. Thanks.


No comments:

Post a Comment