Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > Mobile > Mobile Forum > Android - ช่วยด้วยครับเกี่ยวกับ Context Menu ใน Listview ครับ



 

Android - ช่วยด้วยครับเกี่ยวกับ Context Menu ใน Listview ครับ

 



Topic : 112787



โพสกระทู้ ( 2 )
บทความ ( 0 )



สถานะออฟไลน์




จากโค้ด ที่ผมกำลัง ทำครับ

Code (Android-Java)
public class Casdfasdfsdaf extends Activity {

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

        // Permission StrictMode
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }

        // listView1
        final ListView lisView1 = (ListView) findViewById(R.id.listView1);

        /** JSON return
         *  [{"MemberID":"1","Name":"Weerachai","Tel":"0819876107"},
         * {"MemberID":"2","Name":"Win","Tel":"021978032"},
         * {"MemberID":"3","Name":"Eak","Tel":"0876543210"}]
         */

        String url = "http://projectaaact.hol.es/Activity.php";

        try {

            JSONArray data = new JSONArray(getHttpPost(url, null));




            final ArrayList<HashMap<String, String>> MyArrList = new ArrayList<HashMap<String, String>>();
            HashMap<String, String> map;

            for (int i = 0; i < data.length(); i++) {
                JSONObject c = data.getJSONObject(i);

                map = new HashMap<String, String>();
                map.put("placeid", c.getString("placeid"));
                map.put("nameact", c.getString("nameact"));
                MyArrList.add(map);
                registerForContextMenu(lisView1);

            }


            SimpleAdapter sAdap;
            sAdap = new SimpleAdapter(Casdfasdfsdaf.this, MyArrList, R.layout.activity_column,
                    new String[]{"placeid", "nameact"}, new int[]{R.id.ColMemberID, R.id.ColName});
            lisView1.setAdapter(sAdap);


            // OnClick Item
            lisView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                public void onItemClick(AdapterView<?> myAdapter, View myView,
                                        int position, long mylng) {

                    String placeid = MyArrList.get(position).get("placeid")
                            .toString();
                    String nameact = MyArrList.get(position).get("nameact")
                            .toString();

                    Intent newActivity = new Intent(getApplicationContext(), Barcode.class);
                    newActivity.putExtra("placeid", placeid);
                    newActivity.putExtra("nameact", nameact);
                    startActivity(newActivity);

                }

            });




        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }





        //////////////////////////////////////////การสร้างกิจกรรม//////////////////////////////////////////////////////////
        final AlertDialog.Builder ad = new AlertDialog.Builder(this);
        final AlertDialog.Builder popDialog = new AlertDialog.Builder(this);
        final LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
        // btnShow
        Button newact = (Button) findViewById(R.id.newact);
        newact.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                final View Viewlayout = inflater.inflate(R.layout.newactivity,  //การดึงxmlอีกหน้านึงมาผสมกัน
                        (ViewGroup) findViewById(R.id.layout_dialog));

                popDialog.setIcon(android.R.drawable.btn_star_big_on);
                popDialog.setTitle("สร้างกิจกรรม");
                popDialog.setView(Viewlayout);

                // Button OK
                popDialog.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        // txtUsername and Password (Dialog)

                        EditText inputact = (EditText) Viewlayout.findViewById(R.id.txtinputact);
                        EditText inputidact = (EditText) Viewlayout.findViewById(R.id.txtidact);
                        String nameact = inputact.getText().toString();
                        String placeid = inputidact.getText().toString();
                        HttpClient httpclient = new DefaultHttpClient();
                        String url = ("http://projecfftact.hol.es/Newactivity.php");
                        HttpPost httppost = new HttpPost(url);

                        try {
                            // Add your data
                            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                            nameValuePairs.add(new BasicNameValuePair("placeid", placeid));
                            nameValuePairs.add(new BasicNameValuePair("nameact", nameact));
                            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                            // Execute HTTP Post Request
                            HttpResponse response = httpclient.execute(httppost);
                            String resultServer = getHttpPost(url, nameValuePairs);
                            String strStatusID = "0";
                            String strError = "No Connection";
                            JSONObject c;
                            try {
                                c = new JSONObject(resultServer);   //return
                                strStatusID = c.getString("StatusID");
                                strError = c.getString("Error");
                            } catch (JSONException e) {
// TODO Auto-generated catch block
                                e.printStackTrace();
                            }
// Prepare Login
                            if (strStatusID.equals("0")) {
// Dialog
                                ad.setTitle("Error! ");
                                ad.setIcon(android.R.drawable.btn_star_big_on);
                                ad.setPositiveButton("Close", null);
                                ad.setMessage(strError);                      //มาจาก strError = No connection
                                ad.show();
                                Log.e("Log", "New activity Error");
                            }
//Dialog POPUP
                            else {
                                Log.e("Log", "New activity success");
                                Toast.makeText(Casdfasdfsdaf.this, "Create new activity Success", Toast.LENGTH_SHORT).show();
                                Intent newActivity = new Intent(Casdfasdfsdaf.this, Choice.class);
                                startActivity(newActivity);
                            }

                        } catch (ClientProtocolException e) {
                            // TODO Auto-generated catch block
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                        }////////////////////////////////////////

                    }
                })
                        // Button Cancel
                        .setNegativeButton("Cancel",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int id) {
                                        dialog.cancel();
                                    }
                                });

                popDialog.create();
                popDialog.show();


            }
        });
///////////////////////////////////////////////การสร้างกิจกรรม/////////////////////////////////////////////////////


    }  //ปีกกาปิดOncreate







//////////////////////////////////////////////Long click///////////////////////////////////////

    public void onCreateContextMenu(ContextMenu menu, View v,
                                    ContextMenu.ContextMenuInfo menuInfo) {
        //if (v.getId()==R.id.list) {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
        menu.setHeaderTitle("Command").toString();
        String[] menuItems = getResources().getStringArray(R.array.CmdMenu);
        for (int i = 0; i < menuItems.length; i++) {
            menu.add(Menu.NONE, i, i, menuItems[i]);
        }

        //}
    }


    public boolean onContextItemSelected(MenuItem item) {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        int menuItemIndex = item.getItemId();
        String[] menuItems = getResources().getStringArray(R.array.CmdMenu);
        String CmdName = menuItems[menuItemIndex];

        //String MemName = MebmerList.get(info.position).get("Name").toString();

        // Check Event Command
        if ("Edit".equals(CmdName)) {

            // Show on new activity
            //Intent newActivity = new Intent(ListDeleteActivity.this,UpdateActivity.class);
            //newActivity.putExtra("MemID", MebmerList.get(info.position).get("ID").toString());
            //startActivity(newActivity);

            // for Delete Command
        } else if ("Delete".equals(CmdName)) {

            final AlertDialog.Builder adb = new AlertDialog.Builder(Casdfasdfsdaf.this);
            final AlertDialog.Builder adb2 = new AlertDialog.Builder(Casdfasdfsdaf.this);

            adb.setTitle("Delete?");
            adb.setMessage("Are you sure delete ");
            adb.setNegativeButton("Cancel", null);
            adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                    // Request to Delete data.
                    String url = "http://projeeectact.hol.es/deletedata.php";
                    List<NameValuePair> params = new ArrayList<NameValuePair>();
                    params.add(new BasicNameValuePair("placeid", MyArrList.get(position).get("placeid")));  //////////////////

                    String resultServer  = getHttpPost(url,params);
                    String strStatusID = "0";
                    String strError = "Unknow Status";

                    try {
                        JSONObject c = new JSONObject(resultServer);
                        strStatusID = c.getString("StatusID");
                        strError = c.getString("Error");
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    // Prepare Delete
                    if(strStatusID.equals("0"))
                    {
                        // Dialog
                        adb2.setTitle("Error! ");
                        adb2.setIcon(android.R.drawable.btn_star_big_on);
                        adb2.setPositiveButton("Close", null);
                        adb2.setMessage(strError);
                        adb2.show();
                    }
                    else
                    {
                        Toast.makeText(Casdfasdfsdaf.this, "Delete data successfully.", Toast.LENGTH_SHORT).show();
                        onCreate(null);
                    }

                }
            });
            adb.show();
        }

        return true;
    }

//////////////////////////////////////////////long click/////////////////////////////////////





    public String getHttpPost(String url,List<NameValuePair> params) {
        StringBuilder str = new StringBuilder();
        HttpClient client = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        try {
            HttpResponse response = client.execute(httpGet);
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            if (statusCode == 200) { // Download OK
                HttpEntity entity = response.getEntity();
                InputStream content = entity.getContent();
                BufferedReader reader = new BufferedReader(new InputStreamReader(content));
                String line;
                while ((line = reader.readLine()) != null) {
                    str.append(line);
                }
            } else {
                Log.e("Log", "Failed to download result..");
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return str.toString();
    }





    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_my, menu);
        return true;
    }
}





ปัญหาคือ ส่วนโค้ดนี้ params.add(new BasicNameValuePair("placeid", MyArrList.get(position).get("placeid")));
ตัว MyArrList เป็นสีแดงครับผมจะนำเอาไปใช้ในส่วนของการเลือกเมนูแล้วกด Delete แต่มันติดโค้ดนี้ที่ผมต้องเอาไปเข้ากับฐานข้อมูลครับผมตันมากเลยครับ ขั้นตอนของโค้ดผลลัพธ์จะเป็นดังนี้
1. เป็นรายการListviewดึงมาจากฐานข้อมูล
2. เมื่อกดแช่ประมาณ 1 วิ จะขึ้น เมนู Edit กับ Delete ขึ้นมา
3. กด Delete ก็จะลบรายการนั้น (ติดขั้นตอนช่วงนี้แหละครับ งง มาก)



Tag : Mobile, Android, JAVA







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2014-11-24 02:37:58 By : skyblueen View : 1262 Reply : 2
 

 

No. 1



โพสกระทู้ ( 9,553 )
บทความ ( 2 )



สถานะออฟไลน์


ไม่ได้จะเข้ามาตอบนะครับเพราะไม่ชำนาญ
แต่ช่วยเอา tag code ครอบ ด้วยครับ และบอกว่าบันทัดไหน
คนช่วยจะได้ หาปัญหาได้ง่ายๆ น่ะครับ

tag_code








ประวัติการแก้ไข
2014-11-24 09:41:51
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2014-11-24 07:22:22 By : Chaidhanan
 


 

No. 2



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

ตัวอย่างก็มีครับ




Android Delete data in Web Server Database

Android Edit/Update Data to Web Server Database (Web Server)

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2014-11-24 10:16:43 By : mr.win
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : Android - ช่วยด้วยครับเกี่ยวกับ Context Menu ใน Listview ครับ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 03
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่