因此,为了在listView中显示项目:我在一个片段和一个下载器,一个解析器和一个CustomAdapter中有一个listView。下载程序与php通信并下载数据,数据正在编码并在解析器中解码,因此customAdapter可以将这些内容放入listView中。我用Collections.reverse(玩家)排序我的列表;一切正常,我可以观看,我可以用另一个PHP和另一个类删除东西。如果我现在在MySQL中添加一些内容(因为我还使用了另一个片段,另一个类和另一个php),它将删除内容的位置而不是放在顶部。即使在MySQL中也需要它,所以我能做些什么才能将新内容放在顶端? 我不知道问题是MySQL,添加过程,删除过程还是排序过程。
listView所在的片段:
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);
}
}
};
}
解析器:
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;
}
}
php下载数据:
$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);
我有很多课程和phps,所以如果你想告诉我你想在哪里仔细看看我会很高兴,我会在这里发布代码。
感谢您的关注!