So, for showing items in listView: I have a listView in a fragment and a downloader, a parser and a CustomAdapter. The downloader communicates with the php and downloads data, the data is being encoded and gets decoded in parser so the customAdapter can put the stuff in the listView. I sort my list with Collections.reverse(players); Everything works fine, I can watch and I can delete stuff with another php and another class. If I add something in MySQL now (for what I also use another fragment, another class and another php) it takes the deleted content's position instead of being put at the top. Even in MySQL it takes it's place so what can I do to make the new content be put at the top? I do not know if the problem is either MySQL, the adding process, the delete process or the sort process.
The fragment where the listView is:
public class BiologyFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {
String url = "http://tomeapp.esy.es/connectbiology.php";
private SwipeRefreshLayout ll;
private ListView lv;
private boolean isRefreshing;
private Handler refreshhandler = new Handler();
FloatingActionButton addfab;
FloatingActionButton help;
FragmentManager manager;
public BiologyFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ll = (SwipeRefreshLayout)inflater.inflate(R.layout.fragment_subject, container, false);
ll.setOnRefreshListener(this);
lv = (ListView) ll.findViewById(R.id.lv);
manager = getActivity().getSupportFragmentManager();
new Downloader(getContext(), url, lv, manager).execute();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getContext());
SharedPreferences.Editor editor = preferences.edit();
editor.putString("last", "biology"); //TODO CHANGE ! IN EVERY SUBJECT DIFFERENT
editor.apply();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//THE ONCLICK LISTENER IS INTEGRATED IN PARSER!!!
}
});
addfab = (FloatingActionButton) ll.findViewById(R.id.add);
addfab.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getContext());
SharedPreferences.Editor editor = preferences.edit();
editor.putString("subjects", "biology"); //TODO CHANGE ! IN EVERY SUBJECT DIFFERENT
editor.apply();
AddFragment addFragment = new AddFragment();
FragmentManager manager = getActivity().getSupportFragmentManager();
manager.beginTransaction().replace(R.id.relative_layout_content_main, addFragment).commit();
}
});
lv.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
boolean enable = false;
if (lv != null && lv.getChildCount() > 0) {
// check if the first item of the list is visible
boolean firstItemVisible = lv.getFirstVisiblePosition() == 0;
// check if the top of the first item is visible
boolean topOfFirstItemVisible = lv.getChildAt(0).getTop() == 0;
// enabling or disabling the refresh layout
enable = firstItemVisible && topOfFirstItemVisible;
}
ll.setEnabled(enable);
}
});
return ll;
}
@Override
public void onRefresh() {
new Downloader(getContext(), url, lv, manager).execute();
refreshhandler.post(refreshing);
}
Runnable refreshing = new Runnable() {
@Override
public void run() {
if(isRefreshing){
// re run the verification after 1 second
}else{
// stop the animation after the data is fully loaded
ll.setRefreshing(false);
}
}
};
}
The parser:
public class Parser extends AsyncTask<Void,Integer,Integer> {
Context c;
ListView lv;
String data;
String result;
public static int id;
public static int idcounter;
public static String[] url = new String[100]; //TODO MAKE THE SIZE OF ARRAY EQUAL TO ID's AVAILABLE
List<String> authors = new ArrayList<>();
List<String> dates = new ArrayList<>();
List<String> titles = new ArrayList<>();
List<String> contents = new ArrayList<>();
ArrayList<SubjectsItem> players = new ArrayList<>(); //ÄNDERN
ProgressDialog pd;
FragmentManager manager2;
public Parser(Context c, String data, ListView lv, FragmentManager manager) {
this.c = c;
this.data = data;
this.lv = lv;
manager2 = manager;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pd = new ProgressDialog(c);
pd.setTitle("Parser");
pd.setMessage("Aktualisieren.. Bitte warten!");
pd.show();
}
@Override
protected Integer doInBackground(Void... params) {
return this.parse();
}
@Override
protected void onPostExecute(Integer integer) {
super.onPostExecute(integer);
if(integer == 1) {
//ERSTELLE LV MIT ARRAY LIST
ArrayAdapter<SubjectsItem> adapter = new SubjectAdapter(c, R.layout.fragment_subjects_listview, players);
System.out.println(players);
if(adapter != null && c != null) {
lv.setAdapter(adapter);
Collections.reverse(players); //TURNS AROUND THE LIST (PLAYERS)!
System.out.println("PLAYERLIST : " + players);
}
else System.out.println("Adapter nicht gesetzt.");
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Toast.makeText(c,"CLICKED "+ position, Toast.LENGTH_SHORT).show();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(c);
SharedPreferences.Editor editor = preferences.edit();
//editor.putString("", );
editor.apply();
System.out.println(authors.get(position));
System.out.println(titles.get(position));
editor.putString("author", authors.get(position));
editor.putString("date", dates.get(position));
editor.putString("title", titles.get(position));
editor.putString("content", contents.get(position));
editor.apply();
FragmentManager manager = manager2;
ContentFragment cFragment = new ContentFragment();
manager.beginTransaction().replace(R.id.relative_layout_content_main, cFragment).commit();
} /*Mit getX kann man entweder NewsItem, ID oder gar Text anzeigen!*/
});
} else {
Toast.makeText(c,"Etwas ist beim Parsen schiefgelaufen!", Toast.LENGTH_SHORT).show();
}
pd.dismiss();
}
//PARSING RECEIVED DATA
private int parse() { //PROBLEM ! (FIXED)
try {
//ADD DATA TO JSON ARRAY
System.out.println(data);
//CREATE JO OBJECT TO HOLD ONE ITEM
JSONObject object = new JSONObject(data);
JSONArray ja = object.getJSONArray("result");
players.clear(); //ÄNDERN
//LOOP ARRAY
for(int i = 0; i < ja.length(); i++) {
JSONObject jo = ja.getJSONObject(i);
//RETRIEVE NAME
id = jo.getInt("id");
String author = jo.getString("author");
String content = jo.getString("content");
String title = jo.getString("title");
String date = jo.getString("date");
authors.add(author);
dates.add(date);
titles.add(title);
contents.add(content);
//ADD IT TO ARRAYLIST
SubjectsItem eintrag = new SubjectsItem(id, author, content, title, date);
players.add(eintrag);
System.out.println(url[idcounter]);
System.out.println(author + " " + content + " " + title + " " + date + "___________");
}
Collections.reverse(authors);
Collections.reverse(dates);
Collections.reverse(titles);
Collections.reverse(contents);
return 1;
} catch (JSONException e) {
e.printStackTrace();
}
return 0;
}
}
The php to download data:
$con = mysqli_connect(HOST,USER,PASS,DB);
$sql = "SELECT title, content, author, id, date FROM biology";
$res = mysqli_query($con,$sql);
$result = array();
while($row = mysqli_fetch_array($res)){
array_push($result,
array('title'=>$row[0],
'content'=>$row[1],
'author'=>$row[2],
'id'=>$row[3],
'date'=>$row[4]
));
}
echo json_encode(array("result"=>$result));
mysqli_close($con);
I got many classes and phps so I'd be happy if you would tell me where you would like to take a closer look at, I will post the code here then.
Thanks for your attention!