如何在Android中单击支架
这是我的代码,我想在单击项目时显示一个对话框。。 你能帮我吗? 这是我的代码,我想在单击项目时显示一个对话框。。 你能帮我吗如何在Android中单击支架,android,listview,click,Android,Listview,Click,这是我的代码,我想在单击项目时显示一个对话框。。 你能帮我吗? 这是我的代码,我想在单击项目时显示一个对话框。。 你能帮我吗 import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; impor
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
public class DataAdapter extends BaseAdapter {
Context mContext;
private LayoutInflater mInflater;
String targetmonth;
String targetyear;
String targetamount;
String[] month;
String[] year;
String[] amount;
public DataAdapter(Context c, String[] month, String[] year, String[] amount) {
this.month = month;
this.year = year;
this.amount = amount;
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;
holder = new ViewHolder();
if (convertView == null) {
convertView = mInflater.inflate(R.layout.customgrid, parent, false);
holder = new ViewHolder();
holder.month = (TextView) convertView
.findViewById(R.id.targetmonth);
holder.year = (TextView) convertView.findViewById(R.id.targetyear);
holder.amount = (TextView) convertView
.findViewById(R.id.targetamount);
if (position == 0) {
convertView.setTag(holder);
}
} else {
holder = (ViewHolder) convertView.getTag();
}
try {
holder.month.setText(month[position]);
holder.year.setText(year[position]);
holder.amount.setText(amount[position]);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return convertView;
}
static class ViewHolder {
TextView month;
TextView year, amount;
}
}
我认为在这一部分
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
holder = new ViewHolder();
if (convertView == null) {
convertView = mInflater.inflate(R.layout.customgrid, parent, false);
holder = new ViewHolder();
holder.month = (TextView) convertView
.findViewById(R.id.targetmonth);
holder.year = (TextView) convertView.findViewById(R.id.targetyear);
holder.amount = (TextView) convertView
.findViewById(R.id.targetamount);
Main Activity.java
package com.demo.php.listview;
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.app.ProgressDialog;
import android.graphics.Color;
import android.net.ParseException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Window;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
JSONArray jArray;
String result = null;
InputStream is = null;
StringBuilder sb = null;
ArrayList<String> al = new ArrayList<String>();
ArrayList<String> al1 = new ArrayList<String>();
ArrayList<String> al2 = new ArrayList<String>();
String targetmonth;
String targetyear;
String targetamount;
// int responseCode;
//int listItemCount=0;
ListView listview ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(Color.WHITE);
setTitleColor(Color.rgb(0x74, 0, 0x37));
setTitle("DEMO PHP");
requestWindowFeature(Window.FEATURE_RIGHT_ICON);
setContentView(R.layout.main);
listview = (ListView) findViewById(R.id.listView1);
new LoadData().execute();
}
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, ""," Loading...");
}
@Override
protected void onPostExecute(final Void unused) {
try{
listview.setAdapter(new DataAdapter(MainActivity.this,al.toArray(new String[al.size()]),al1.toArray(new String[al1.size()]),al2.toArray(new String[al2.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://192.168.0.2/android/test.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();
}
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;
}
}
}
package com.demo.php.listview;
导入java.io.BufferedReader;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.util.ArrayList;
导入org.apache.http.HttpEntity;
导入org.apache.http.HttpResponse;
导入org.apache.http.NameValuePair;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.entity.UrlEncodedFormEntity;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.entity.StringEntity;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.apache.http.params.BasicHttpParams;
导入org.apache.http.params.HttpConnectionParams;
导入org.apache.http.params.HttpParams;
导入org.apache.http.protocol.http;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入android.app.Activity;
导入android.app.ProgressDialog;
导入android.graphics.Color;
导入android.net.ParseException;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.view.Window;
导入android.widget.ListView;
导入android.widget.Toast;
公共类MainActivity扩展了活动{
杰索纳雷·贾雷;
字符串结果=null;
InputStream=null;
StringBuilder sb=null;
ArrayList al=新的ArrayList();
ArrayList al1=新的ArrayList();
ArrayList al2=新的ArrayList();
字符串targetmonth;
字符串目标耳朵;
字符串目标安装;
//国际响应码;
//int listItemCount=0;
列表视图列表视图;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setTheme(颜色:白色);
setTitleColor(Color.rgb(0x74,0,0x37));
setTitle(“演示PHP”);
requestWindowFeature(窗口。功能\右\图标);
setContentView(R.layout.main);
listview=(listview)findViewById(R.id.listView1);
新建LoadData().execute();
}
私有类LoadData扩展异步任务{
私有进程对话;
@凌驾
//可以在这里使用UI线程
受保护的void onPreExecute(){
this.progressDialog=progressDialog.show(MainActivity.this,“,”正在加载…);
}
@凌驾
受保护的void onPostExecute(最终未使用的void){
试一试{
setAdapter(新DataAdapter(MainActivity.this,al.toArray(新字符串[al.size()])、al1.toArray(新字符串[al1.size()])、al2.toArray(新字符串[al2.size()]);
此.progressDialog.discouse()文件;
}
捕获(例外e){
Toast.makeText(getApplicationContext(),e.toString(),Toast.LENGTH_LONG).show();
}
}
@凌驾
受保护的Void doInBackground(Void…参数){
//TODO自动生成的方法存根
//HTTP post
试试{
ArrayList nameValuePairs=新的ArrayList();
HttpClient HttpClient=新的DefaultHttpClient();
试一试{
HttpPost HttpPost=新的HttpPost(“http://192.168.0.2/android/test.php");
StringEntity se=新的StringEntity(“信封”,HTTP.UTF_8);
httppost.setEntity(se);
HttpParams httpParameters=新的BasicHttpParams();
//以毫秒为单位设置超时,直到建立连接。
int timeoutConnection=3000;
HttpConnectionParams.setConnectionTimeout(httpParameters,timeoutConnection);
//设置默认套接字超时(SO\U超时)
//以毫秒为单位,这是等待数据的超时。
int timeoutSocket=3000;
HttpConnectionParams.setSoTimeout(httpParameters,timeoutSocket);
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
is=entity.getContent();
}
捕获(例外e){
Toast.makeText(getApplicationContext(),e.toString(),Toast.LENGTH_LONG).show();
}
//缓冲读取器
试一试{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(
is,“iso-8859-1”),80);
sb=新的StringBuilder();
sb.append(reader.readLine()+“\n”);
字符串行=“0”;
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
is.close();
结果=sb.toString();
}
捕获(例外e){
Toast.makeText(getApplicationContext(),e.toString(),Toast.LENGTH_LONG).show();
}
试一试{
jArray=新的JSONArray(结果);
JSONObject json_data=null;
for(int i=0;iholder.month.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
final AlertDialog alertDialog = new AlertDialog.Builder(getApplicationContext()).create();
alertDialog.setIcon(/*Provide any drawable*/);
alertDialog.setTitle("Alert Dialog");
alertDialog.setMessage("Alert Dialog in OnClick");
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE,
"Ok",
new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
alertDialog.dismiss();
}
});
alertDialog.show();
}
});
public class MainActivity extends Activity {
class DataAdapter extends BaseAdapter {
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
holder = new ViewHolder();
//Onclick for your tagetyear TextView
holder.year = (TextView) convertView.findViewById(R.id.targetyear);
holder.year.setOnClickListener(new OnClickListener() {
@SuppressWarnings("unused")
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
}
}
public class MainActivity extends Activity {
JSONArray jArray;
String result = null;
InputStream is = null;
StringBuilder sb = null;
ArrayList<String> al = new ArrayList<String>();
ArrayList<String> al1 = new ArrayList<String>();
ArrayList<String> al2 = new ArrayList<String>();
String targetmonth;
String targetyear;
String targetamount;
// int responseCode;
//int listItemCount=0;
ListView listview ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(Color.WHITE);
setTitleColor(Color.rgb(0x74, 0, 0x37));
setTitle("DEMO PHP");
requestWindowFeature(Window.FEATURE_RIGHT_ICON);
setContentView(R.layout.main);
listview = (ListView) findViewById(R.id.listView1);
new LoadData().execute();
}
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, ""," Loading...");
}
@Override
protected void onPostExecute(final Void unused) {
try{
listview.setAdapter(new DataAdapter(MainActivity.this,al.toArray(new String[al.size()]),al1.toArray(new String[al1.size()]),al2.toArray(new String[al2.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://192.168.0.2/android/test.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();
}
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;
}
}
class DataAdapter extends BaseAdapter {
Context mContext;
private LayoutInflater mInflater;
String targetmonth;
String targetyear;
String targetamount;
String[] month;
String[] year;
String[] amount;
public DataAdapter(Context c, String[] month, String[] year, String[] amount) {
this.month = month;
this.year = year;
this.amount = amount;
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;
holder = new ViewHolder();
if (convertView == null) {
convertView = mInflater.inflate(R.layout.customgrid, parent, false);
holder = new ViewHolder();
holder.month = (TextView) convertView
.findViewById(R.id.targetmonth);
holder.year = (TextView) convertView.findViewById(R.id.targetyear);
holder.amount = (TextView) convertView
.findViewById(R.id.targetamount);
//Onclick for your tagetyear TextView
holder.year = (TextView) convertView.findViewById(R.id.targetyear);
holder.year.setOnClickListener(new OnClickListener() {
@SuppressWarnings("unused")
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
if (position == 0) {
convertView.setTag(holder);
}
} else {
holder = (ViewHolder) convertView.getTag();
}
try {
holder.month.setText(month[position]);
holder.year.setText(year[position]);
holder.amount.setText(amount[position]);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return convertView;
}
static class ViewHolder {
TextView month;
TextView year, amount;
}
}
}