2013-07-13 1 views
0

여기 내 코드입니다, 내가 항목을 클릭하면 대화 상자를 표시하고 싶습니다. 당신이 도와 줄 수 있습니까? 여기 내 코드입니다. 항목을 클릭하면 대화 상자를 표시하고 싶습니다. 이 문제를 해결할 수 있습니까?어떻게 안드로이드에있는 홀더를 클릭 할 수

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); 

홈페이지 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; 
    } 
} 


    } 
+0

? ListView, GridView? 나는 당신이 "어떻게 홀더를 클릭 할 수 있는가?"라는 것이 무슨 뜻인지 잘 모르겠습니다. 당신은 Lists/Grids 등의 자식 뷰에서 클릭 이벤트를들을 수 있습니다. –

+0

listview ... 안드로이드의 새로운 기능입니다. 이벤트를 선언 할 수 없습니다. 왜 –

답변

0

, 당신이 때 하나의 대화 상자를 표시하려면이 부분에서 생각 소유자의 항목이 클릭됩니다. 이처럼

holder.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(); 
    } 
}); 
+0

이 코드를 넣어야합니까? 그냥 달을 클릭 할 때 –

+0

강제로 닫습니다. –

+0

logcat에서 오류를 알려주시겠습니까? 대부분 'getApplicationContext()'때문에 크래시가 발생했을 수 있습니다. 액티비티의 컨텍스트를 BaseAdapter로 전달하거나 액티비티에서 데이터 목록을 보여주는 BaseAdapter를 정의하십시오. –

0

사용 :이 방법을 시도 할 수 있습니다

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 
    } 
    }); 
    } 
    } 
} 

MainActivity : 당신의 어댑터입니다 어댑터 뷰 AdapterView 어떤 종류의

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; 
} 

} 

    } 
+0

이 코드를 어디에 넣어야합니까? –

+0

수정 된 내 답변을 확인하십시오. – Nirmal

+0

클릭하면 그냥 닫습니다. –

관련 문제