ユーザーが何かを追跡してアイテム行を削除できるlistViewがあります。削除ボタンを押すと、次のように問題が表示されます。Webで解決策を確認して見つけましたが、解決方法がわかりませんでした。私の状況は彼らとは違うかもしれません。この問題のアイデアはありますか??
Logcat 出力 (エラー):
java.lang.ArrayIndexOutOfBoundsException: length=1; index=-1
at java.util.ArrayList.get(ArrayList.java:306)
at android.widget.ArrayAdapter.getItem(ArrayAdapter.java:337)
at com.example.android.project.ThirdActivity$3$2
.onClick(ThirdActivity.java:295)
at com.android.internal.app.AlertController$ButtonHandler
.handleMessage(AlertController.java:174)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:4987)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller
.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
ThirdActivity.java
friend = new ArrayList<String>(Arrays.asList(result.split("[*]")));
public void onListItemClick(ListView parent, View v, final int position, long id){
AlertDialog.Builder adb = new AlertDialog.Builder(ThirdActivity.this);
adb.setItems(item, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if(item[which].equals("Track Location")){
...
}else{
AlertDialog.Builder adb=new AlertDialog.Builder(ThirdActivity.this);
adb.setTitle("Delete?");
adb.setMessage("Are you sure you want to delete " + (String) lv.getItemAtPosition(position));
final int positionToRemove = position;
adb.setNegativeButton("Cancel", null);
adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, final int which) {
new Thread(){
public void run(){
try{
httpclient = new DefaultHttpClient();
httppost = new HttpPost("http://www.kryptoquest.com/tracker/remove.php");
nameValuePairs = new ArrayList<NameValuePair>(2);
Log.d("12345678",user);
nameValuePairs.add(new BasicNameValuePair("Targetname", (String) lv.getItemAtPosition(position)));
nameValuePairs.add(new BasicNameValuePair("Username", user));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpclient.execute(httppost);
is = response.getEntity().getContent();
}catch(Exception e){
Log.e("log_tag", "Error:"+e.toString());
}
//convert response to string
try{
reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
sb = new StringBuilder();
line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
Log.d("test",sb.toString());
is.close();
result = sb.toString();
runOnUiThread(new Runnable()
{
public void run()
{
Toast.makeText(ThirdActivity.this, result + lv.getItemAtPosition(position), Toast.LENGTH_LONG).show();
}
});
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
}
}.start();
String delete = (String) ((lv.getAdapter()).getItem(which));
friend.remove(delete);
friend.trimToSize();
adapter.notifyDataSetChanged();
}});
adb.show();
}
}
});
AlertDialog ad = adb.create();
ad.show();
}