Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/188.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
Java 如何使listview可单击,也可以删除已单击的项目?_Java_Android_Listview_Delete Row_Clickable - Fatal编程技术网

Java 如何使listview可单击,也可以删除已单击的项目?

Java 如何使listview可单击,也可以删除已单击的项目?,java,android,listview,delete-row,clickable,Java,Android,Listview,Delete Row,Clickable,food.java import java.util.ArrayList; import java.util.HashMap; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.app.Activity; import android.app.ListActivity; import android.app.ProgressDialog; imp

food.java

import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
//import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;



public class Drinks extends ListActivity 
    implements OnClickListener {
    // Progress Dialog
        private ProgressDialog pDialog;

    //testing on Emulator:
     private static final String READ_COMMENTS_URL = "http://10.0.2.2/pbda2/drinksordered.php";
    //my ip :192.168.43.176
     // url to delete product
     //   private static final String url_delete_product = "http://10.0.2.2/pbda2/delete_product.php";

     // private CheckBox chkFood, chkDrinks, chkServices;
      //private Button btnDisplay, chkClear, deliever, chkClearFood, fooddeliever, drinksdeliever, servicesdeliever, chkClearDrinks, chkClearServices;
      //private TextView clearThis,orderdisplay, clearThisFood, foodorderdisplay, drinksorderdisplay, servicesorderdisplay, clearThisDrinks, clearThisServices;

      private static final String TAG_SUCCESS = "success";
      private static final String TAG_POSTS = "posts";
        private static final String TAG_SEATNUMBER = "seatnumber";
        private static final String TAG_DRINKSORDERED = "drinksordered";
        //it's important to note that the message is both in the parent branch of 
        //our JSON tree that displays a "Post Available" or a "No Post Available" message,
        //and there is also a message for each individual post, listed under the "posts"
        //category, that displays what the user typed as their message.

      //An array of all of our comments
        private JSONArray mComments = null;
        //manages all of our comments in a list.
        private ArrayList<HashMap<String, String>> mCommentList;



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.drinks);



        View v = findViewById(R.id.backmain);
        //set event listener
            v.setOnClickListener(this);

        View x= findViewById(R.id.foodbtn);
            //set event listener
                x.setOnClickListener(this);

                View y= findViewById(R.id.servicebtn);
                //set event listener
                    y.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        //loading the comments via AsyncTask
        new LoadComments().execute();
    }


    /**
     * Retrieves recent post data from the server.
     */
    public void updateJSONdata() {

        // Instantiate the arraylist to contain all the JSON data.
        // we are going to use a bunch of key-value pairs, referring
        // to the json element name, and the content, for example,
        // message it the tag, and "I'm awesome" as the content..

        mCommentList = new ArrayList<HashMap<String, String>>();

        // Bro, it's time to power up the J parser 
        JSONParser jParser = new JSONParser();
        // Feed the beast our comments url, and it spits us
        //back a JSON object.  Boo-yeah Jerome.
        JSONObject json = jParser.getJSONFromUrl(READ_COMMENTS_URL);

        //when parsing JSON stuff, we should probably
        //try to catch any exceptions:
        try {

            //I know I said we would check if "Posts were Avail." (success==1)
            //before we tried to read the individual posts, but I lied...
            //mComments will tell us how many "posts" or comments are
            //available
            mComments = json.getJSONArray(TAG_POSTS);

            // looping through all posts according to the json object returned
            for (int i = 0; i < mComments.length(); i++) {
                JSONObject c = mComments.getJSONObject(i);

                //gets the content of each tag
                String seatnumber = c.getString(TAG_SEATNUMBER);
                String services = c.getString(TAG_DRINKSORDERED);



                // creating new HashMap
                HashMap<String, String> map = new HashMap<String, String>();

                map.put(TAG_SEATNUMBER, seatnumber);
                map.put(TAG_DRINKSORDERED, services);


                // adding HashList to ArrayList
                mCommentList.add(map);

                //annndddd, our JSON data is up to date same with our array list
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }









    @Override
            public void onClick(View arg0) {
        if(arg0.getId() == R.id.backmain){
            //define a new Intent for the second Activity
            Intent intent = new Intent(this,MainActivity.class);

            //start the second Activity
            this.startActivity(intent);

        }
            if(arg0.getId() == R.id.foodbtn){
                //define a new Intent for the second Activity
                Intent intent = new Intent(this,Food.class);

                //start the second Activity
                this.startActivity(intent);
            }
            if(arg0.getId() == R.id.servicebtn){
                //define a new Intent for the second Activity
                Intent intent = new Intent(this,Services.class);

                //start the second Activity
                this.startActivity(intent);
            }


    }

    /**
     * Inserts the parsed data into the listview.
     */
    private void updateList() {
        // For a ListActivity we need to set the List Adapter, and in order to do
        //that, we need to create a ListAdapter.  This SimpleAdapter,
        //will utilize our updated Hashmapped ArrayList, 
        //use our single_post xml template for each item in our list,
        //and place the appropriate info from the list to the
        //correct GUI id.  Order is important here.
        ListAdapter adapter = new SimpleAdapter(this, mCommentList,
                R.layout.single_post, new String[] { TAG_SEATNUMBER, TAG_DRINKSORDERED
                //TAG_DRINKSORDERED, TAG_SERVICES
                         }, new int[] { R.id.seatnumber, R.id.orders
                //R.id.drinkstv, R.id.servicestv,


         });

        // I shouldn't have to comment on this one:
        setListAdapter(adapter);

        // Optional: when the user clicks a list item we 
        //could do something.  However, we will choose
        //to do nothing...
        ListView lv = getListView();    
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                // This method is triggered if an item is click within our
                // list. For our example we won't be using this, but
                // it is useful to know in real life applications.

            }
        });
    }   




    public class LoadComments extends AsyncTask<Void, Void, Boolean> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(Drinks.this);
            pDialog.setMessage("Loading orders...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }
        @Override
        protected Boolean doInBackground(Void... arg0) {
            //we will develop this method in version 2
            updateJSONdata();
            return null;

        }


        @Override
        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);
            pDialog.dismiss();
          //we will develop this method in version 2
            updateList();
        }
    }


}
import java.util.ArrayList;
导入java.util.HashMap;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入android.app.Activity;
导入android.app.ListActivity;
导入android.app.ProgressDialog;
导入android.content.Intent;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.AdapterView;
导入android.widget.Button;
导入android.widget.CheckBox;
导入android.widget.EditText;
导入android.widget.ListAdapter;
导入android.widget.ListView;
导入android.widget.simpledapter;
//导入android.widget.ImageView;
导入android.widget.TextView;
导入android.widget.Toast;
导入android.widget.AdapterView.OnItemClickListener;
公共课活动
实现OnClickListener{
//进度对话框
私人对话;
//在模拟器上测试:
私有静态最终字符串读取\u注释\u URL=”http://10.0.2.2/pbda2/drinksordered.php";
//我的ip:192.168.43.176
//用于删除产品的url
//私有静态最终字符串url\u delete\u product=”http://10.0.2.2/pbda2/delete_product.php";
//私人复选框chkFood、chkDrinks、chkServices;
//私人按钮btnDisplay、chkClear、deliever、chkClearFood、fooddeliever、DrinkDeliever、servicesdeliever、chkClearDrinks、chkClearServices;
//私有文本查看clearThis、orderdisplay、clearThisFood、foodorderdisplay、drinksorderdisplay、servicesorderdisplay、clearThisDrinks、clearThisServices;
私有静态最终字符串标记_SUCCESS=“SUCCESS”;
私有静态最终字符串标记_POSTS=“POSTS”;
私有静态最终字符串标记_SEATNUMBER=“SEATNUMBER”;
私有静态最终字符串标记\u DRINKSORDERED=“DRINKSORDERED”;
//需要注意的是,消息都位于的父分支中
//我们的JSON树显示“Post Available”或“No Post Available”消息,
//每个帖子都有一条信息,列在“帖子”下面
//类别,显示用户键入的消息。
//我们所有评论的数组
私有JSONArray mComments=null;
//管理列表中的所有评论。
私有数组列表mCommentList;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
this.setContentView(R.layout.drinks);
视图v=findViewById(R.id.backmain);
//设置事件侦听器
v、 setOnClickListener(此);
视图x=findViewById(R.id.foodbtn);
//设置事件侦听器
x、 setOnClickListener(此);
视图y=findViewById(R.id.servicebtn);
//设置事件侦听器
y、 setOnClickListener(此);
}
@凌驾
受保护的void onResume(){
//TODO自动生成的方法存根
super.onResume();
//通过AsyncTask加载注释
新建LoadComments().execute();
}
/**
*从服务器检索最近发布的数据。
*/
public void updateJSONdata(){
//实例化arraylist以包含所有JSON数据。
//我们将使用一组键值对,引用
//例如,json元素名和内容,
//在标签上留言,并在内容上写上“我太棒了”。。
mCommentList=newarraylist();
//兄弟,是时候给J解析器通电了
JSONParser jParser=新的JSONParser();
//向野兽提供我们的评论url,它就会吐我们
//返回一个JSON对象。嘘,耶,杰罗姆。
JSONObject json=jParser.getJSONFromUrl(读取注释URL);
//在解析JSON内容时,我们可能应该
//尝试捕获任何异常:
试一试{
//我知道我说过我们会检查“帖子是否有用。”(success==1)
//在我们试图阅读个别帖子之前,但我撒谎了。。。
//McComments会告诉我们有多少“帖子”或评论
//可用
mComments=json.getJSONArray(TAG_POSTS);
//根据返回的json对象遍历所有帖子
对于(int i=0;i<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#f0f0f0"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"

        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/box"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="2dp"
            android:orientation="horizontal" >


            <LinearLayout
                android:id="@+id/box"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="2dp"
                android:orientation="vertical"
                android:padding="5dp" >

                <TextView
                    android:id="@+id/seatnumber"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom"
                    android:paddingBottom="2dip"
                    android:paddingLeft="5dp"
                    android:paddingTop="6dip"
                    android:textColor="#333"
                    android:textSize="16sp"
                    android:textStyle="bold" />



                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:paddingBottom="5dp" >

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:gravity="left"
                        android:paddingLeft="5dp"
                        android:text="Order: "
                        android:textColor="#5d5d5d"
                        android:textStyle="bold" >
                    </TextView>

                    <TextView
                        android:id="@+id/orders"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:gravity="left"
                        android:textColor="#acacac"
                        android:textStyle="bold" >
                    </TextView>
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android1="http://schemas.android.com/apk/res/android"
    android1:id="@+id/bg2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android1:background="#E0FFFF" >



    <Button
        android:id="@+id/servicebtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="358dp"
        android:layout_toRightOf="@+id/foodbutton"
        android:text="SERVICES" />

    <Button
        android1:id="@+id/drinksbtn"
        android1:layout_width="wrap_content"
        android1:layout_height="wrap_content"
        android1:layout_alignParentBottom="true"
        android1:layout_marginBottom="23dp"
        android1:layout_marginRight="24dp"
        android1:text="DRINKS ORDERS" />

    <LinearLayout
        android:id="@+id/top_layover"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:orientation="horizontal" >

        <TextView
            android1:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="@string/foodtitle"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </LinearLayout>

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/top_layover"
        android1:layout_alignBottom="@+id/servicesdelivered"
        android:background="#fff"
        android:divider="@android:color/transparent"
        android:scrollbars="none" />

    <LinearLayout
        android:id="@+id/bottom_layover"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:orientation="horizontal"
        android:weightSum="2" >

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical" >
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.69"
            android:orientation="vertical" >

            <Button
                android1:id="@+id/backmain"
                android1:layout_width="wrap_content"
                android1:layout_height="wrap_content"
                android1:layout_gravity="right"
                android1:text="@string/backtomain" />

        </LinearLayout>
    </LinearLayout>

    <Button
        android1:id="@+id/servicesdelivered"
        android1:layout_width="wrap_content"
        android1:layout_height="wrap_content"
        android1:layout_above="@+id/drinksbtn"
        android1:layout_alignParentLeft="true"
        android1:layout_marginLeft="111dp"
        android1:text="@string/servicesdelivered"
        android1:visibility="invisible" />

</RelativeLayout>
listView.setOnItemClickListener(this);
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
    adapter.remove(adapter.getItem(position));
    myDatabase.myRemoveMethod(adapter.getItem(position).getMyIDmethod());
}
private void updateList() {


    ListView lv = getListView();
    ListAdapter adapter = new SimpleAdapter(this, mCommentList,
            R.layout.single_post, new String[] { TAG_SEATNUMBER, TAG_DRINKSORDERED
            //TAG_DRINKSORDERED, TAG_SERVICES
                     }, new int[] { R.id.seatnumber, R.id.orders
            //R.id.drinkstv, R.id.servicestv,


     });

    setListAdapter(adapter);
    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public boolean onItemClick(AdapterView<?> arg0, View arg1,
                final int arg2, long arg3) {
            // TODO Auto-generated method stub

            AlertDialog.Builder alt = new AlertDialog.Builder(
                    YourActivityName.this,
                    android.R.style.Theme_DeviceDefault_Dialog);
            alt.setMessage("Are you sure want to delete this file??");
            alt.setPositiveButton("Yes",
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog,
                                int which) {
                            // TODO Auto-generated method stub
                            mCommentList.remove(arg2);

                            adapter.notifyDataSetChanged();
                        }
                    });

            alt.setNegativeButton("No",
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog,
                                int which) {
                            // TODO Auto-generated method stub

                            dialog.dismiss();

                        }
                    });
            AlertDialog dialog = alt.create();
            dialog.show();

            return true;
        }
    });
}