2015-06-22 148 views
-5

我有一個json數組和我的數據庫。我在我的android應用程序上收到這個數組。按列表視圖,我收到標題。當我點擊一個事件時,我想打開一個關於此事件的信息的新活動。 「Datainicio」是我的變量與信息和「Titulo」是我的變量與標題。我怎樣才能做到這一點?列表視圖問題

package com.eu.agendamarinhagrande; 

import android.annotation.TargetApi; 
import android.app.ProgressDialog; 
import android.content.Intent; 
import android.os.AsyncTask; 
import android.os.Build; 
import android.support.v7.app.ActionBar; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
import android.widget.SimpleAdapter; 
import android.widget.TextView; 

import com.eu.agendamarinhagrande.JSONParser; 
import com.eu.agendamarinhagrande.R; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.params.HttpConnectionParams; 
import org.apache.http.params.HttpParams; 
import org.apache.http.protocol.HTTP; 
import org.apache.http.util.EntityUtils; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import java.io.IOException; 
import java.io.UnsupportedEncodingException; 
import java.text.Normalizer; 
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import java.util.regex.Pattern; 


public class MainActivity extends ActionBarActivity implements AdapterView.OnItemClickListener { 

    // Progress Dialog 
    private ProgressDialog pDialog; 

    // Creating JSON Parser object 
    // JSONParser jParser = new JSONParser(); 

    ArrayList<HashMap<String, String>> empresaList; 


    // url to get all products list 
    private static String url_all_empresas = "http://www.grifin.pt/projectoamg/Conexao.php"; 

    // JSON Node names 


    private static final String TAG_TITULO = "Titulo"; 


    // products JSONArray 
    String resultado = null; 

    ListView lista; 

    @TargetApi(Build.VERSION_CODES.HONEYCOMB) 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     // Hashmap para el ListView 
     empresaList = new ArrayList<HashMap<String, String>>(); 
     new Download().execute(); 

     // Cargar los productos en el Background Thread 

     lista = (ListView) findViewById(R.id.listView); 
     lista.setOnItemClickListener(this); 
     // ActionBar actionBar = getSupportActionBar(); 
     // actionBar.setDisplayHomeAsUpEnabled(true); 

    }//fin onCreate 

    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

    } 


    public class Download extends AsyncTask<Void, Void, String> { 

     @Override 
     protected String doInBackground(Void... params) { 
      String out = null; 

      try { 
       DefaultHttpClient httpClient = new DefaultHttpClient(); 

       final HttpParams httpParameters = httpClient.getParams(); 

       HttpConnectionParams.setConnectionTimeout(httpParameters, 15000); 
       HttpConnectionParams.setSoTimeout(httpParameters, 15000); 

       HttpGet httpPost = new HttpGet(url_all_empresas); 

       HttpResponse httpResponse = httpClient.execute(httpPost); 
       HttpEntity httpEntity = httpResponse.getEntity(); 

       out = EntityUtils.toString(httpEntity, HTTP.UTF_8); 

      } catch (UnsupportedEncodingException e) { 
       e.printStackTrace(); 
      } catch (ClientProtocolException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

      return out; 
     } 


     @TargetApi(Build.VERSION_CODES.GINGERBREAD) 
     @Override 
     protected void onPostExecute(String result) { 
      super.onPostExecute(result); 

      ArrayList<String> list = new ArrayList<>(); 

      try { 
       JSONArray jsonArray = new JSONArray(result); 
       for (int i = 0; i < jsonArray.length(); i++) { 
        JSONObject jsa = jsonArray.getJSONObject(i); 
        String str = jsa.getString("Titulo"); 
        String data = jsa.getString("Datainicio"); 
        Log.e("TAG", str); 
        Log.e("TAG", data); 


        String s1 = Normalizer.normalize(str, Normalizer.Form.NFKD); 
        String regex = Pattern.quote("[\\p{InCombiningDiacriticalMarks}\\p{IsLm}\\p{IsSk}]+"); 

        str = new String(s1.replaceAll(regex, "").getBytes("ascii"), "ascii"); 

        list.add(str+"\n"+data); 

       } 

       ArrayAdapter adapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, list); 

       // updating listview 
       //setListAdapter(adapter); 
       lista.setAdapter(adapter); 

      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
} 
+0

你應該張貼問題之前,谷歌入堆棧 – Madhu

+0

大聲笑的傢伙,如果我在棧上問,那是因爲我無法搜索或理解這對谷歌 – Rakav

+0

你在這段代碼中的問題是什麼? – Madhu

回答

0

使用ItemOnclick監聽像下面的列表視圖

yourlistview.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 
      //Your intent goes here 

      String yourvalue = yourarry.get(position).getyourvalue; 

     } 
    }); 
+0

我的問題是想象我有url_all_empresas是我的數組json,我有標題信息圖像等。 我需要做些什麼才能獲取有關僅點擊事件的信息?一項新活動?你能用代碼告訴我嗎?我是新的一個機器人對不起 – Rakav

+0

你可以得到listview與你的物品對嗎?你想在單擊列表視圖時獲得任何值嗎? – Madhu

+0

是的,想象一下,當我點擊value1時,我的列表視圖具有value1和value2用關於value1的信息打開新活動,並且我點擊value2關於value2的信息,問題是我在數組上有這個信息url_all_empressas我不知道如何使用這個信息自動 – Rakav