Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在另一个活动中删除后更新ListView_Android_Listview - Fatal编程技术网

Android 在另一个活动中删除后更新ListView

Android 在另一个活动中删除后更新ListView,android,listview,Android,Listview,我试图在另一个活动中从列表中删除一个对象,该活动通过单击ListView中的某个对象触发,但即使删除成功,我似乎也无法更新ListView 具有ListView的活动: public class YourList extends AppCompatActivity { String name; String token; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(saved

我试图在另一个活动中从列表中删除一个对象,该活动通过单击ListView中的某个对象触发,但即使删除成功,我似乎也无法更新ListView

具有ListView的活动:

public class YourList extends AppCompatActivity {
String name;
String token;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_your_list);
    Bundle intent = getIntent().getExtras();
    token = intent.getString("tokenID");
    name = intent.getString("nameIDagain");
    TextView welcome = (TextView) findViewById(R.id.welcomeListText);
    welcome.setText("Welcome to your list, " + name);
    System.out.println(token);
}

@Override
protected void onStart() {
    super.onStart();
    ReadTask task = new ReadTask();
    task.execute("http://api.evang.dk/v2/catches?token=" + token);

}

private class ReadTask extends ReadHttpTask {

    @Override
    protected void onPostExecute(CharSequence charSequence) {
        List<Catch> catches;
        ArrayAdapter<Catch> arrayAdapter;
        catches = new ArrayList<>();
        try {
            JSONArray array = new JSONArray(charSequence.toString());
            System.out.println(array.length());
            for (int i = 0; i < array.length(); i++) {
                JSONObject obj = array.getJSONObject(i);
                Integer id = obj.getInt("id");
                String angler_name = obj.getString("name");
                String email = obj.getString("email");
                String dateTime = obj.getString("datetime");
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date jsonDate = sdf.parse(dateTime);
                String fishingMethod = obj.getString("fishing_method");
                String fishBreed = obj.getString("breed");
                String length = obj.getString("length");
                String weight = obj.getString("weight");
                String weather = obj.getString("weather");
                String location = obj.getString("location");
                Double latitude = obj.getDouble("latitude");
                Double longitude = obj.getDouble("longitude");
                Catch fishCatch = new Catch(id, angler_name, jsonDate, fishingMethod, fishBreed, length, weight, weather, location, latitude, longitude);
                catches.add(fishCatch);

            }
            ListView listView = (ListView) findViewById(R.id.yourFishList);
            arrayAdapter = new ArrayAdapter(YourList.this, android.R.layout.simple_list_item_1, catches);
            listView.setAdapter(arrayAdapter);

            listView.setOnItemClickListener((parent, view, position, id) -> {
                Intent intent = new Intent(getBaseContext(), YourDetailsCatch.class);
                intent.putExtra("YourCatch", catches.get((int) id));
                intent.putExtra("token", token);
                startActivity(intent);
            });
            //newAdapter = new Adapter();
            //listView.setAdapter(newAdapter);
            arrayAdapter.notifyDataSetChanged();
        } catch (JSONException ex) {
            ex.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
   }
}
public类YourList扩展了appcompative活动{
字符串名;
字符串标记;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u您的列表);
Bundle intent=getIntent().getExtras();
token=intent.getString(“tokenID”);
name=intent.getString(“nameIDagain”);
TextView欢迎=(TextView)findViewById(R.id.welcomeListText);
welcome.setText(“欢迎加入您的列表,”+姓名);
System.out.println(令牌);
}
@凌驾
受保护的void onStart(){
super.onStart();
ReadTask任务=新建ReadTask();
任务。执行(“http://api.evang.dk/v2/catches?token=“+代币);
}
私有类ReadTask扩展了ReadHttpTask{
@凌驾
受保护的void onPostExecute(CharSequence CharSequence){
列出渔获物清单;
ArrayAdapter ArrayAdapter;
catch=newarraylist();
试一试{
JSONArray数组=新的JSONArray(charSequence.toString());
System.out.println(array.length());
对于(int i=0;i{
Intent Intent=新的Intent(getBaseContext(),YourDetailsCatch.class);
intent.putExtra(“YourCatch”,catch.get((int)id));
意向。putExtra(“令牌”,令牌);
星触觉(意向);
});
//newAdapter=新适配器();
//setAdapter(newAdapter);
arrayAdapter.notifyDataSetChanged();
}捕获(JSONException ex){
例如printStackTrace();
}捕获(解析异常){
e、 printStackTrace();
}
}
}
}
删除活动:

public class YourDetailsCatch extends AppCompatActivity {

private Catch fishCatch;
private String token;
int id;
Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_your_details_catch);
    intent = getIntent();
    fishCatch = (Catch) intent.getSerializableExtra("YourCatch");
    token = intent.getStringExtra("token");

    id = fishCatch.getID();
    System.out.println(id);
    TextView anglerName = (TextView) findViewById(R.id.yournameOfAngler);
    anglerName.setText(fishCatch.getAngler_name());

    EditText breed = (EditText) findViewById(R.id.yourfishBreedDetail);
    breed.setText(" " +fishCatch.getBreed());

    EditText method = (EditText) findViewById(R.id.yourfishMethodDetail);
    method.setText(fishCatch.getSpearfishing());

    EditText weight = (EditText) findViewById(R.id.yourfishWeightDetail);
    weight.setText(" " +fishCatch.getWeight());

    EditText length = (EditText) findViewById(R.id.yourfishLengthDetail);
    length.setText(" " +fishCatch.getLength());

    EditText location = (EditText) findViewById(R.id.yourfishLocationDetail);
    location.setText(" " + fishCatch.getLocation());

    EditText latitude = (EditText) findViewById(R.id.yourfishLatitudeDetail);
    String parseLatitude = Double.toString(fishCatch.getLatitude());
    latitude.setText(" " +parseLatitude);

    EditText longitude = (EditText) findViewById(R.id.yourfishLongitudeDetail);
    String parseLongitude = Double.toString(fishCatch.getLongitude());
    longitude.setText(" " +parseLongitude);

    EditText weather = (EditText) findViewById(R.id.yourfishWeatherDetail);
    weather.setText(" " + fishCatch.getWeather());

    EditText dataTime = (EditText) findViewById(R.id.yourfishDateTimeDetail);
    String str = String.format(String.valueOf(fishCatch.getDateTime()));
    dataTime.setText(" " +str);
}

public void deleteCatch(View view) {

DeleteCatchTask deleteCatchTask = new DeleteCatchTask();
 deleteCatchTask.execute("http://api.evang.dk/v2/catches/" + id +  "?token=" + token);

    AlertDialog.Builder alert = new AlertDialog.Builder(YourDetailsCatch.this);
    alert.setTitle("Success");
    alert.setMessage("Deletion Successful");
    alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            finish();
        }
    });
    alert.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
alert.show();

}
private class DeleteCatchTask extends AsyncTask<String, Void, CharSequence> {

    @TargetApi(Build.VERSION_CODES.CUPCAKE)
    @Override
    protected CharSequence doInBackground(String... params) {
        String urlString = params[0];
        try {
            URL url = new URL(urlString);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("DELETE");
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setRequestProperty("Accept", "application/json");

            int responseCode = connection.getResponseCode();
            if(responseCode / 100 != 2){
                String responseMessage = connection.getResponseMessage();
                throw  new IOException("HTTP response code: " + responseCode + " " + responseMessage);
            }

        } catch (MalformedURLException e) {
            cancel(true);
            String message = e.getMessage() + " " + urlString;
            Log.e("Catches", message);
            return message;
        } catch (IOException ex) {
            cancel(true);
            Log.e("Catches", ex.getMessage());
            return ex.getMessage();
        }
        return null;
    }
}
}
public类YourDetailsCach扩展了AppCompative活动{
私人捕鱼;
私有字符串令牌;
int-id;
意图;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u您的详细信息\u捕获);
intent=getIntent();
fishCatch=(Catch)intent.getSerializableExtra(“YourCatch”);
token=intent.getStringExtra(“token”);
id=fishCatch.getID();
系统输出打印项次(id);
TextView anglerName=(TextView)findViewById(R.id.yournameOfAngler);
anglerName.setText(fishCatch.getAngler_name());
EditText品种=(EditText)findViewById(R.id.yourfishBreedDetail);
bread.setText(“+fishCatch.getbread());
EditText方法=(EditText)findViewById(R.id.yourfishMethodDetail);
方法.setText(fishCatch.getSpearfishing());
EditText权重=(EditText)findViewById(R.id.yourfishWeightDetail);
weight.setText(“+fishCatch.getWeight());
EditText长度=(EditText)findViewById(R.id.yourfishLengthDetail);
length.setText(“+fishCatch.getLength());
EditText位置=(EditText)findViewById(R.id.yourfishLocationDetail);
location.setText(“+fishCatch.getLocation());
EditText纬度=(EditText)findViewById(R.id.yourfishLatitudeDetail);
字符串parseLatitude=Double.toString(fishCatch.getLatitude());
纬度.setText(“+parseLatitude);
EditText经度=(EditText)findViewById(R.id.yourfishLongitudeDetail);
字符串parseLongitude=Double.toString(fishCatch.getLongitude());
经度.setText(“+parseLongitude);
EditText天气=(EditText)findViewById(R.id.yourfishWeatherDetail);
weather.setText(“+fishCatch.getWeather());
EditText dataTime=(EditText)findViewById(R.id.yourfishDateTimeDetail);
String str=String.format(String.valueOf(fishCatch.getDateTime());
dataTime.setText(“+str”);
}
公共void deleteCatch(视图){
DeleteCatchTask DeleteCatchTask=新的DeleteCatchTask();
deleteCatchTask.execute(“http://api.evang.dk/v2/catches/“+id+”?令牌=“+token”);
AlertDialog.Builder alert=新建AlertDialog.Builder(YourDetailsCatch.this);
警报。设置标题(“成功”);
alert.setMessage(“删除成功”);
alert.setPositiveButton(“确定”,新的DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
完成();
}
});
alert.setNeutralButton(“取消”,新建DialogInterface.OnClickListener(){
@凌驾
公共void onClick(对话框接口)
// Can be any value of your choosing
private static final int REQUEST_CODE = 112;
Intent intent = new Intent(getBaseContext(), YourDetailsCatch.class);
intent.putExtra("YourCatch", catches.get((int) id));
intent.putExtra("token", token);
startActivityForResult(intent, REQUEST_CODE);
Intent intent = new Intent("deletedId", id);
setResult(RESULT_OK, intent);
finish();
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == REQUEST_CODE = resultCode == RESULT_OK && data != null){
        int id = data.getIntExtra("deletedId", 0);
       // Add logic to remove item from your list

        // Notify adapter of new changes
        adapter.notifiyDataSetChanged();
    }
}