Я пытаюсь удалить элемент списка из списка, используя меню удаления в списке. Удаление необходимо для моего приложения.
Мне нужно удалить элемент из фрагмента.
Проблема в списке предметов, я не знаю, как определить это List<Item> listtache
.
Я использую это, я могу запустить приложение, и все загружается правильно. Помогите мне, пожалуйста.
public class Tachefragment extends Fragment implements AdapterView.OnItemClickListener {
String supprimertache = "http://192.168.1.15/projet/supprimertache.php";
private static final int CODE_GET_REQUEST = 1024;
Httppars ht = new Httppars();
TextView txtmen;
ProgressBar progressBar;
ListView listview;
Typeface type;
ImageView img;
HashMap<String,String> hashMap = new HashMap<>();
ItemAdapter adapter ;
String listviewid;
List<Item> listtache;
List<String> idlist = new ArrayList<>();
public Tachefragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_tachefragment, container, false);
txtmen = (TextView) rootView.findViewById(R.id.textmenu);
img = (ImageView) rootView.findViewById(R.id.imgmenu);
progressBar = (ProgressBar) rootView.findViewById(R.id.ProgressBar1);
listview = (ListView) rootView.findViewById(R.id.listtask);
type = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Montserrat-Light.otf");
txtmen.setTypeface(type);
listview.setOnItemClickListener(this);
listtache = new ArrayList<Item>();
new ParseJSonDataClass(container.getContext()).execute();
return rootView;
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
listviewid = idlist.get(i).toString();
Toast.makeText(getContext() , listviewid.toString() , Toast.LENGTH_SHORT).show();
}
private class ParseJSonDataClass extends AsyncTask<Void, Void, Void> {
public Context context;
String FinalJSonResult;
List<Item> item;
public ParseJSonDataClass(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... voids) {
httpwebservice htp = new httpwebservice(Link.affichertache);
try {
htp.ExecutePostRequest();
if (htp.getResponseCode() == 200) {
FinalJSonResult = htp.getResponse();
if (FinalJSonResult != null) {
JSONArray jsonArray = null;
try {
jsonArray = new JSONArray(FinalJSonResult);
JSONObject jsonObject;
Item itm;
item = new ArrayList<Item>();
for (int i = 0; i < jsonArray.length(); i++) {
itm = new Item();
jsonObject = jsonArray.getJSONObject(i);
idlist.add(jsonObject.getString("idt").toString());
itm.setNomt(jsonObject.getString("nomt"));
itm.setDatet(jsonObject.getString("datet"));
itm.setHeuredebut(jsonObject.getString("heuredebut"));
itm.setTempstotal(jsonObject.getString("tempstotal"));
itm.setDescription(jsonObject.getString("description"));
item.add(itm);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Toast.makeText(context, htp.getErrorMessage(), Toast.LENGTH_SHORT).show();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
if (item != null) {
progressBar.setVisibility(View.GONE);
listview.setVisibility(View.VISIBLE);
adapter = new ItemAdapter(item, context);
listview.setAdapter(adapter);
adapter.notifyDataSetChanged();
registerForContextMenu(listview);
} else {
progressBar.setVisibility(View.GONE);
txtmen.setVisibility(View.VISIBLE);
img.setVisibility(View.VISIBLE);
}
}
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater minfalter = getActivity().getMenuInflater();
minfalter.inflate(R.menu.menucontext , menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
final Item selectitem = (Item) adapter.getItem(info.position);
switch (item.getItemId()){
case R.id.supprimer:
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setMessage("Voulez vous supprimer cette tache" + " " + selectitem.getNomt()+" !")
.setCancelable(false)
.setPositiveButton("Oui", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
listtache.remove(selectitem);
adapter.notifyDataSetChanged();
}
})
.setNegativeButton("Non", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
return true;
default:
return super.onContextItemSelected(item);
}
}
}