Hiiii 我想当我应该单击行数据时,它似乎复制了行的值并传递给另一个活动以显示为 textview。我正在使用 mysql 从表中按单行值获取数据。一个“帮助的希望”将不胜感激,请!
//MainActivity.java
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.widget.TextView;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.net.ParseException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
/*
* @author Firzan Ghulam
*/
public class MainActivity extends Activity{
JSONArray jArray;
String result = null;
InputStream is = null;
StringBuilder sb = null;
ArrayList<String> al = new ArrayList<String>();
String targetmonth;
int responseCode;
int listItemCount=0;
ListView listview ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listview = (ListView) findViewById(R.id.listView1);
new LoadData().execute();
// listening to single list item on click
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// selected item
String product = ((TextView) view).getText().toString();
// Launching new Activity on selecting single List Item
Intent i = new Intent(getApplicationContext(), SingleListItem.class);
// sending data to new activity
i.putExtra("product", product);
startActivity(i);
}
});
}
private class LoadData extends AsyncTask<Void, Void, Void> {
private ProgressDialog progressDialog;
@Override
// can use UI thread here
protected void onPreExecute() {
this.progressDialog = ProgressDialog.show(MainActivity.this, ""," Can you just see your roof...");
}
@Override
protected void onPostExecute(final Void unused) {
try{
listview.setAdapter(new DataAdapter(MainActivity.this,al.toArray(new String[al.size()])));
this.progressDialog.dismiss();
}
catch(Exception e){
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
}
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
// HTTP post
try {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
HttpClient httpclient = new DefaultHttpClient();
try{
HttpPost httppost = new HttpPost("http://palaknehazz.com/app/ap.php");
StringEntity se = new StringEntity("envelope",HTTP.UTF_8);
httppost.setEntity(se);
HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is established.
int timeoutConnection = 3000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 3000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}
catch(Exception e){
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
}
//buffered reader
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 80);
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line = "0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
System.out.println("Magu data aagaya"+result);
}
catch(Exception e){
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
}
try{
jArray = new JSONArray(result);
JSONObject json_data = null;
for (int i = 0; i < jArray.length(); i++) {
json_data = jArray.getJSONObject(i);
//targetamount=json_data.getString("targetamount");
targetmonth=json_data.getString("targetmonth");
//targetyear = json_data.getString("targetyear");
al.add(targetmonth);
//al1.add(targetyear);
//al2.add(targetamount);
//listItemCount=al2.size();
}
}
catch(JSONException e){
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
}
} catch (ParseException e) {
// Log.e("log_tag", "Error in http connection" + e.toString());
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
}
catch (Exception e) {
// Log.e("log_tag", "Error in http connection" + e.toString());
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
}
return null;
}
}
}
//数据适配器
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class DataAdapter extends BaseAdapter {
Context mContext;
private LayoutInflater mInflater;
String targetmonth;
String[] month;
public DataAdapter(Context c, String[] month) {
this.month = month;
mContext = c;
mInflater = LayoutInflater.from(c);
}
public int getCount() {
return month.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.customgrid, parent, false);
holder = new ViewHolder();
holder.month = (TextView) convertView
.findViewById(R.id.targetmonth);
if (position == 0) {
convertView.setTag(holder);
}
} else {
holder = (ViewHolder) convertView.getTag();
}
try {
holder.month.setText(month[position]);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return convertView;
}
static class ViewHolder {
TextView month;
}
}
//SingleListItem
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class SingleListItem extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.meter);
TextView txtProduct = (TextView) findViewById(R.id.product_label);
Intent i = getIntent();
// getting attached intent data
String product = i.getStringExtra("product");
// displaying selected product name
txtProduct.setText(product);
}
}
//Logcat错误
05-14 22:48:02.644: D/dalvikvm(461): GC_CONCURRENT freed 56K, 2% free 14428K/14663K, paused 8ms+6ms
05-14 22:48:03.051: D/gralloc_goldfish(461): Emulator without GPU emulation detected.
05-14 22:48:05.121: I/System.out(461): Magu data aagaya[{"id":"1","targetmonth":"Karan is a intelligegnt guy if you know what i mean haha frieds plays very important role i TV serial life i believe hahaha i mean who guess for being myself"},{"id":"2","targetmonth":"He's the unpredictable foolish man you are looking forward for seriously"},{"id":"3","targetmonth":"Same here you are looking"}]
05-14 22:48:08.294: D/AndroidRuntime(461): Shutting down VM
05-14 22:48:08.294: W/dalvikvm(461): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
05-14 22:48:08.341: E/AndroidRuntime(461): FATAL EXCEPTION: main
05-14 22:48:08.341: E/AndroidRuntime(461): java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
05-14 22:48:08.341: E/AndroidRuntime(461): at com.demo.php.listview.MainActivity$LoadData$1.onItemClick(MainActivity.java:94)
05-14 22:48:08.341: E/AndroidRuntime(461): at android.widget.AdapterView.performItemClick(AdapterView.java:292)
05-14 22:48:08.341: E/AndroidRuntime(461): at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
05-14 22:48:08.341: E/AndroidRuntime(461): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
05-14 22:48:08.341: E/AndroidRuntime(461): at android.widget.AbsListView$1.run(AbsListView.java:3168)
05-14 22:48:08.341: E/AndroidRuntime(461): at android.os.Handler.handleCallback(Handler.java:605)
05-14 22:48:08.341: E/AndroidRuntime(461): at android.os.Handler.dispatchMessage(Handler.java:92)
05-14 22:48:08.341: E/AndroidRuntime(461): at android.os.Looper.loop(Looper.java:137)
05-14 22:48:08.341: E/AndroidRuntime(461): at android.app.ActivityThread.main(ActivityThread.java:4340)
05-14 22:48:08.341: E/AndroidRuntime(461): at java.lang.reflect.Method.invokeNative(Native Method)
05-14 22:48:08.341: E/AndroidRuntime(461): at java.lang.reflect.Method.invoke(Method.java:511)
05-14 22:48:08.341: E/AndroidRuntime(461): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-14 22:48:08.341: E/AndroidRuntime(461): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-14 22:48:08.341: E/AndroidRuntime(461): at dalvik.system.NativeStart.main(Native Method)