2012-02-23 20 views
0

私はreomteデータベースでクエリを作成するアプリケーションを開発しています.JsonとPHPをMySQLで使用すると、後でもっとオープンになります。SimpleAdapterのコンストラクタは定義されていません

私のSimpleAdapterは、クイックフィックスなしでEclipseによって定義されていないと考えられています。奇妙なことは、私がimputサンプルで試していたときにボタンを追加する前に、したがって、私はこのバグを修正する方法のアイデアはありません。

コードは次のとおりです。

import java.util.ArrayList; 
import java.util.HashMap; 

import org.apache.http.NameValuePair; 
import org.apache.http.message.BasicNameValuePair; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.app.ListActivity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
import android.widget.SimpleAdapter; 
import android.widget.TextView; 

public class DataconectActivity extends ListActivity { 
    EditText searcher; 
    Button btmsearch; 
    // adding strings that are going to be the tags of the nodes for the list view in the ListAdapter 
    private static String TAG_PRODID = "codigo"; 
    private static final String TAG_NOMBRE = "nombre"; 
    private static final String TAG_DESCRIPCION = "descripcion"; 
    private static final String TAG_COLOR = "color"; 
    private static final String TAG_PESO = "peso"; 
    private static final String TAG_PRECIO = "precio"; 
    private static final String TAG_DISPONIBILIDAD = "disponibilidad"; 
    private static final String TAG_INFO = "resultados"; 
    // remember to change the url to try it with the web server http://trialsols.webege.com/mobil.php after this is important to add at least 10.000 items to the db to ensure the query efficiency 
    private static final String url ="http://192.168.1.50/mobilv1.php"; 
    ArrayList<HashMap<String, String>> jsonResultList = new ArrayList<HashMap<String, String>>(); 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 



     searcher = (EditText) findViewById(R.id.searchtxt); 
     btmsearch = (Button) findViewById(R.id.searchbtm); 

     btmsearch.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 


       // Hashmap for ListView 

       String data = searcher.getText().toString() ; 

       ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
       nameValuePairs.add(new BasicNameValuePair("precio",data)); 

       // Creating JSON Parser instance 
       JsonParser jParser = new JsonParser(); 

       // getting JSON string from URL 
       JSONArray jArray = jParser.getJSONFromUrl(nameValuePairs, url); 


       try { 

        // looping through All JsonData 
        for(int i = 0; i < jArray.length() ; i++){ 
         JSONObject json_data = jArray.getJSONObject(i); 
         String nombre = json_data.getString("nombre"); 
         String prodid = json_data.getString("prodid"); 
         String descripcion = json_data.getString("descripcion"); 
         String color = json_data.getString("color"); 
         String peso = json_data.getString("peso"); 
         String precio = json_data.getString("precio"); 
         String disponibilidad = json_data.getString("disponibilidad"); 
         // creating new HashMap 
         HashMap<String, String> map = new HashMap<String, String>(); 

         // adding each child node to HashMap key => value 
         map.put(TAG_PRODID, prodid); 
         map.put(TAG_NOMBRE, nombre); 
         map.put(TAG_DESCRIPCION, descripcion); 
         map.put(TAG_COLOR, color); 
         map.put(TAG_PESO, peso); 
         map.put(TAG_PRECIO, precio); 
         map.put(TAG_DISPONIBILIDAD, disponibilidad); 
         // adding HashList to ArrayList 
         jsonResultList.add(map); 

        } 
       } 

       catch(JSONException e){ 
        Log.e("log_tag", "Error parsing data "+e.toString()); 
       }     

       //adding the results of the parsed JSON data to a listView using a ListAdapter 
       ListAdapter adapter = new SimpleAdapter(this, jsonResultList, 
         R.layout.datalistitems, 
         new String[] { TAG_NOMBRE, TAG_PRECIO, TAG_DISPONIBILIDAD }, new int[] { 
         R.id.dbnombretxt, R.id.dbpreciotxt, R.id.dbdisponibilidadtxt }); 

       // add in a ListActivity We need to make 
       setListAdapter(adapter);  
       // initialize a ListView to display the single item information 
       ListView lv = getListView(); 
       lv.setTextFilterEnabled(true); 
       // generate the action when an item is clicked 
       lv.setOnItemClickListener(new OnItemClickListener() { 
        public void onItemClick(AdapterView<?> parent, View view, 
          int position, long id) { 
         // When clicked, create a view with the values 
         // getting values from selected ListItem 
         String name = ((TextView) view.findViewById(R.id.dbnombretxt)).getText().toString(); 
         String cost = ((TextView) view.findViewById(R.id.dbpreciotxt)).getText().toString(); 
         String stock = ((TextView) view.findViewById(R.id.dbdisponibilidadtxt)).getText().toString(); 

         // Starting new intent 
         Intent in = new Intent(getApplicationContext(), SearchResults.class); 
         in.putExtra(TAG_NOMBRE, name); 
         in.putExtra(TAG_PRECIO, cost); 
         in.putExtra(TAG_DISPONIBILIDAD, stock); 
         startActivity(in); 
        } 
       }); 

      } 

     }); 

    } 

} 

エラーが正確である:あなたのView.OnClickListenerの内側にあなたのSimpleAdapterインスタンスを構築し、そのOnClickListenerthisポイントをご参照、これはされている

adapter = new SimpleAdapter(this, jsonResultList, 
         R.layout.datalistitems, 
         new String[] { TAG_NOMBRE, TAG_PRECIO, TAG_DISPONIBILIDAD }, new int[] { 
         R.id.dbnombretxt, R.id.dbpreciotxt, R.id.dbdisponibilidadtxt }); 
+0

保存してクリーンアップしましたか? – kosa

答えて

8

有効なContextではありません。コードの行を次のように変更したい場合があります。

adapter = new SimpleAdapter(DataconectActivity.this, jsonResultList, 
        R.layout.datalistitems, 
        new String[] { TAG_NOMBRE, TAG_PRECIO, TAG_DISPONIBILIDAD }, new int[] { 
        R.id.dbnombretxt, R.id.dbpreciotxt, R.id.dbdisponibilidadtxt }); 

これは、コンストラクタへの適切な参照を提供します。

HTH

+0

お返事ありがとうございます..... –

関連する問題