|
|
|
Android - ช่วยด้วยครับเกี่ยวกับ Context Menu ใน Listview ครับ |
|
|
|
|
|
|
|
จากโค้ด ที่ผมกำลัง ทำครับ
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
|
|
|
|
|
|
Date :
2014-11-24 02:37:58 |
By :
skyblueen |
View :
1318 |
Reply :
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ไม่ได้จะเข้ามาตอบนะครับเพราะไม่ชำนาญ
แต่ช่วยเอา tag code ครอบ ด้วยครับ และบอกว่าบันทัดไหน
คนช่วยจะได้ หาปัญหาได้ง่ายๆ น่ะครับ
|
ประวัติการแก้ไข 2014-11-24 09:41:51
|
|
|
|
Date :
2014-11-24 07:22:22 |
By :
Chaidhanan |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ตัวอย่างก็มีครับ
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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|