|
|
|
Android - Trying to get selected data from list item (Mysql and Dialog help needed) |
|
|
|
|
|
|
|
Hello everyone,
I was wondering what is wrong with my code ?
Program stops when click an item from list...
Code (Android-Java)
package com.example.rehber;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.Bundle;
import android.os.StrictMode;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Typeface;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ScaleXSpan;
import android.text.style.StyleSpan;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Menu;
import android.view.ViewGroup;
import android.webkit.WebSettings.TextSize;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
@SuppressLint("NewApi")
@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);
}
final Button btn1 = (Button) findViewById(R.id.button1);
// Perform action on click
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
SearchData();
}
});
btn1.performClick();
}
public void SearchData()
{
// listView1
final ListView lisView1 = (ListView)findViewById(R.id.listView1);
// editText1
final EditText inputText = (EditText)findViewById(R.id.editText1);
String url = "192.168.1.2/list.php";
// Paste Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("txtKeyword", inputText.getText().toString()));
try {
JSONArray data = new JSONArray(getJSONUrl(url,params));
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("adi", c.getString("adi"));
map.put("sirketi", c.getString("sirketi"));
map.put("gorevi", c.getString("gorevi"));
map.put("cep1", c.getString("cep1"));
map.put("cep2", c.getString("cep2"));
map.put("tel", c.getString("tel"));
map.put("dahili", c.getString("dahili"));
map.put("email", c.getString("email"));
MyArrList.add(map);
}
SimpleAdapter sAdap;
sAdap = new SimpleAdapter(MainActivity.this, MyArrList, R.layout.yeni,
new String[] {"adi", "gorevi"}, new int[] {R.id.ColCustomerID, R.id.ColEmail});
lisView1.setAdapter(sAdap);
final AlertDialog.Builder viewDetail = new AlertDialog.Builder(this);
final LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
lisView1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> myAdapter, View myView,
int position, long mylng) {
final String stradi = MyArrList.get(position).get("adi")
.toString();
final String ssirketi = MyArrList.get(position).get("sirketi")
.toString();
final String strgorevi = MyArrList.get(position).get("gorevi")
.toString();
final String strcep1 = MyArrList.get(position).get("cep1")
.toString();
final String strcep2 = MyArrList.get(position).get("cep2")
.toString();
final String strtel = MyArrList.get(position).get("tel")
.toString();
final String strdahili = MyArrList.get(position).get("dahili")
.toString();
final String strmail = MyArrList.get(position).get("email")
.toString();
TextView adiy = (TextView) findViewById(R.id.textView2);
adiy.setText(stradi);
TextView sirkety = (TextView) findViewById(R.id.textView4);
sirkety.setText(ssirketi);
TextView gorevy = (TextView) findViewById(R.id.textView6);
gorevy.setText(strgorevi);
TextView cep1y = (TextView) findViewById(R.id.textView8);
cep1y.setText(strcep1);
TextView cep2y = (TextView) findViewById(R.id.textView10);
cep2y.setText(strcep2);
TextView tely = (TextView) findViewById(R.id.textView12);
tely.setText(strtel);
TextView dahiliy = (TextView) findViewById(R.id.textView14);
dahiliy.setText(strdahili);
TextView maily = (TextView) findViewById(R.id.textView16);
maily.setText(strmail);
final View Viewlayout = inflater.inflate(R.layout.dialog,
(ViewGroup) findViewById(R.id.id_dialog));
viewDetail.setIcon(R.drawable.user);
viewDetail.setTitle("Kişi Bilgileri");
viewDetail.setView(Viewlayout);
// Button OK
viewDetail.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
viewDetail.create();
viewDetail.show();
}
});
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String getJSONUrl(String url,List<NameValuePair> params) {
StringBuilder str = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
try {
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse response = client.execute(httpPost);
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", "file not downloaded..");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return str.toString();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Tag : Mobile, MySQL, Android, Mobile
|
ประวัติการแก้ไข 2013-08-22 21:09:31
|
|
|
|
|
Date :
2013-08-22 20:37:10 |
By :
muratboy31 |
View :
1211 |
Reply :
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Murat, You have logcat ?
|
|
|
|
|
Date :
2013-08-22 21:46:22 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Hello TC Admin
No, I dont have it...
|
|
|
|
|
Date :
2013-08-22 21:58:46 |
By :
muratboy31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Android ProgressBar/ProgressDialog Search Display result Data from Web Server
|
|
|
|
|
Date :
2013-08-23 05:31:20 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Thanks for reply
this codes give error when i click but if i remove the codes dialog shows up with out error !!!
I want to show them in dialog...
logcat error screen
Code (Android-Java)
TextView adiy = (TextView) findViewById(R.id.textView2);
adiy.setText(stradi);
TextView sirkety = (TextView) findViewById(R.id.textView4);
sirkety.setText(strsirketi);
TextView gorevy = (TextView) findViewById(R.id.textView6);
gorevy.setText(strgorevi);
TextView cep1y = (TextView) findViewById(R.id.textView8);
cep1y.setText(strcep1);
TextView cep2y = (TextView) findViewById(R.id.textView10);
cep2y.setText(strcep2);
TextView tely = (TextView) findViewById(R.id.textView12);
tely.setText(strtel);
TextView dahiliy = (TextView) findViewById(R.id.textView14);
dahiliy.setText(strdahili);
TextView maily = (TextView) findViewById(R.id.textView16);
maily.setText(strmail);
|
ประวัติการแก้ไข 2013-08-23 13:03:56 2013-08-23 14:00:44
|
|
|
|
Date :
2013-08-23 12:59:13 |
By :
muratboy31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 02
|