2012-04-05 2 views
0

이 코드가 작동하는 데 문제가 있습니다.Java에서 안드로이드 ListActivities간에 데이터 전달

기본적으로 하나의 목록 활동에서 다른 활동으로 이동하여 목록 항목의 텍스트를 새 목록보기로 전달한 다음 새 목록 활동에서 해당 텍스트를 검색 한 다음 그 목록 항목의 값.

로그인 고양이

04-05 17:47:32.370: E/AndroidRuntime(30135): FATAL EXCEPTION: main 
04-05 17:47:32.370: E/AndroidRuntime(30135): java.lang.ClassCastException:android.widget.LinearLayout 
04-05 17:47:32.370: E/AndroidRuntime(30135): at com.thickcrustdesigns.ufood.CatogPage$1.onItemClick(CatogPage.java:66) 
04-05 17:47:32.370: E/AndroidRuntime(30135): at android.widget.AdapterView.performItemClick(AdapterView.java:284) 
04-05 17:47:32.370: E/AndroidRuntime(30135): at android.widget.ListView.performItemClick(ListView.java:3731) 
04-05 17:47:32.370: E/AndroidRuntime(30135): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1959) 
04-05 17:47:32.370: E/AndroidRuntime(30135): at android.os.Handler.handleCallback(Handler.java:587) 
04-05 17:47:32.370: E/AndroidRuntime(30135): at android.os.Handler.dispatchMessage(Handler.java:92) 
04-05 17:47:32.370: E/AndroidRuntime(30135): at android.os.Looper.loop(Looper.java:130) 
04-05 17:47:32.370: E/AndroidRuntime(30135): at android.app.ActivityThread.main(ActivityThread.java:3691) 
04-05 17:47:32.370: E/AndroidRuntime(30135): at java.lang.reflect.Method.invokeNative(Native Method) 
04-05 17:47:32.370: E/AndroidRuntime(30135): at java.lang.reflect.Method.invoke(Method.java:507) 
04-05 17:47:32.370: E/AndroidRuntime(30135): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907) 
04-05 17:47:32.370: E/AndroidRuntime(30135): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665) 
04-05 17:47:32.370: E/AndroidRuntime(30135): at dalvik.system.NativeStart.main(Native Method) 

ListActivity 1

package com.thickcrustdesigns.ufood; 

import java.util.ArrayList; 
import org.apache.http.NameValuePair; 
import org.apache.http.message.BasicNameValuePair; 
import org.json.JSONException; 
import org.json.JSONObject; 
import android.app.ListActivity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.Button; 
import android.widget.ListView; 
import android.widget.TextView; 

public class CatogPage extends ListActivity { 

    ListView listView1; 
    Button btn_bk; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.definition_main); 

     btn_bk = (Button) findViewById(R.id.btn_bk); 

     listView1 = (ListView) findViewById(android.R.id.list); 

     ArrayList<NameValuePair> nvp = new ArrayList<NameValuePair>(); 

     nvp.add(new BasicNameValuePair("request", "categories")); 

     ArrayList<JSONObject> jsondefs = Request.fetchData(this, nvp); 

     String[] defs = new String[jsondefs.size()]; 

     for (int i = 0; i < jsondefs.size(); i++) { 
      try { 
       defs[i] = jsondefs.get(i).getString("Name"); 
      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 

     uFoodAdapter adapter = new uFoodAdapter(this, R.layout.definition_list, 
       defs); 

     listView1.setAdapter(adapter); 
     ListView lv = getListView(); 
     lv.setOnItemClickListener(new OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, 
        int position, long id) { 
       TextView tv = (TextView) view; 
       String p = tv.getText().toString(); 
       Intent i = new Intent(getApplicationContext(), Results.class); 
       i.putExtra("category", p); 
       startActivity(i); 
      } 
     }); 

     btn_bk.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View arg0) { 
       Intent i = new Intent(getApplicationContext(), 
         UFoodAppActivity.class); 
       startActivity(i); 
      } 
     }); 

    } 

} 


**ListActivity 2** 

package com.thickcrustdesigns.ufood; 

    import java.util.ArrayList; 
    import org.apache.http.NameValuePair; 
    import org.apache.http.message.BasicNameValuePair; 
    import org.json.JSONException; 
    import org.json.JSONObject; 
    import android.app.ListActivity; 
    import android.os.Bundle; 
    import android.widget.ListView; 

    public class Results extends ListActivity { 

    ListView listView1; 

    enum Category { 
     Chicken, Beef, Chinese, Cocktails, Curry, Deserts, Fish, ForOne { 
      public String toString() { 
       return "For One"; 
      } 
     }, 
     Lamb, LightBites { 
      public String toString() { 
       return "Light Bites"; 
      } 
     }, 
     Pasta, Pork, Vegetarian 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     this.setContentView(R.layout.definition_main); 

     listView1 = (ListView) findViewById(android.R.id.list); 

     Bundle data = getIntent().getExtras(); 
     String category = data.getString("category"); 

     Category cat = Category.valueOf(category); 

     String value = null; 

     switch (cat) { 
     case Chicken: 
      value = "Chicken"; 
      break; 
     case Beef: 
      value = "Beef"; 
      break; 
     case Chinese: 
      value = "Chinese"; 
      break; 
     case Cocktails: 
      value = "Cocktails"; 
      break; 
     case Curry: 
      value = "Curry"; 
      break; 
     case Deserts: 
      value = "Deserts"; 
      break; 
     case Fish: 
      value = "Fish"; 
      break; 
     case ForOne: 
      value = "ForOne"; 
      break; 
     case Lamb: 
      value = "Lamb"; 
      break; 
     case LightBites: 
      value = "LightBites"; 
      break; 
     case Pasta: 
      value = "Pasta"; 
      break; 
     case Pork: 
      value = "Pork"; 
      break; 
     case Vegetarian: 
      value = "Vegetarian"; 
     } 

     ArrayList<NameValuePair> nvp = new ArrayList<NameValuePair>(); 

     nvp.add(new BasicNameValuePair("request", "category")); 

     nvp.add(new BasicNameValuePair("cat", value)); 

     ArrayList<JSONObject> jsondefs = Request.fetchData(this, nvp); 

     String[] defs = new String[jsondefs.size()]; 

     for (int i = 0; i < jsondefs.size(); i++) { 
      try { 
       defs[i] = jsondefs.get(i).getString("Name"); 
      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 

     uFoodAdapter adapter = new uFoodAdapter(this, R.layout.definition_list, 
       defs); 
     listView1.setAdapter(adapter); 

      } 

     } 

요청

package com.thickcrustdesigns.ufood; 

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.impl.client.DefaultHttpClient; 
import org.json.JSONArray; 
import org.json.JSONObject; 
import android.content.Context; 
import android.util.Log; 
import android.widget.Toast; 

public class Request { 

    @SuppressWarnings("null") 
    public static ArrayList<JSONObject> fetchData(Context context, 
      ArrayList<NameValuePair> nvp) { 
     ArrayList<JSONObject> listItems = new ArrayList<JSONObject>(); 
     InputStream is = null; 
     try { 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost(
        "http://co350-11d.projects02.glos.ac.uk/php/database.php"); 
      httppost.setEntity(new UrlEncodedFormEntity(nvp)); 
      HttpResponse response = httpclient.execute(httppost); 
      HttpEntity entity = response.getEntity(); 
      is = entity.getContent(); 
     } catch (Exception e) { 
      Log.e("log_tag", "Error in http connection" + e.toString()); 
     } 

     // convert response to string 

     String result = ""; 
     try { 
      BufferedReader reader = new BufferedReader(new InputStreamReader(
        is, "iso-8859-1"), 8); 
      InputStream stream = null; 
      StringBuilder sb = null; 

      while ((result = reader.readLine()) != null) { 
       sb.append(result + "\n"); 
      } 

      stream.close(); 
      result = sb.toString(); 

     } catch (Exception e) { 
      Log.e("log_tag", "Error converting result " + e.toString()); 
     } 

     try { 
      JSONArray jArray = new JSONArray(result); 

      for (int i = 0; i < jArray.length(); i++) { 

       JSONObject jo = jArray.getJSONObject(i); 

       listItems.add(jo); 

      } 

     } catch (Exception e) { 
      Toast.makeText(context.getApplicationContext(), "None Found!", 
        Toast.LENGTH_LONG).show(); 

     } 
     return listItems; 

    } 

} 

package com.thickcrustdesigns.ufood; 

import java.util.ArrayList; 

import org.apache.http.NameValuePair; 
import org.apache.http.message.BasicNameValuePair; 
import org.json.JSONException; 
import org.json.JSONObject; 
import android.app.ListActivity; 
import android.os.Bundle; 
import android.widget.ListView; 

public class Results extends ListActivity { 

    ListView listView1; 

    enum Category { 
     Chicken, Beef, Chinese, Cocktails, Curry, Deserts, Fish, ForOne { 
      public String toString() { 
       return "For One"; 
      } 
     }, 
     Lamb, LightBites { 
      public String toString() { 
       return "Light Bites"; 
      } 
     }, 
     Pasta, Pork, Vegetarian 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     this.setContentView(R.layout.definition_main); 

     listView1 = (ListView) findViewById(android.R.id.list); 

     Bundle data = getIntent().getExtras(); 
     String category = data.getString("category"); 

     Category cat = Category.valueOf(category); 

     String value = null; 

     switch (cat) { 
     case Chicken: 
      value = "Chicken"; 
      break; 
     case Beef: 
      value = "Beef"; 
      break; 
     case Chinese: 
      value = "Chinese"; 
      break; 
     case Cocktails: 
      value = "Cocktails"; 
      break; 
     case Curry: 
      value = "Curry"; 
      break; 
     case Deserts: 
      value = "Deserts"; 
      break; 
     case Fish: 
      value = "Fish"; 
      break; 
     case ForOne: 
      value = "ForOne"; 
      break; 
     case Lamb: 
      value = "Lamb"; 
      break; 
     case LightBites: 
      value = "LightBites"; 
      break; 
     case Pasta: 
      value = "Pasta"; 
      break; 
     case Pork: 
      value = "Pork"; 
      break; 
     case Vegetarian: 
      value = "Vegetarian"; 
     } 

     ArrayList<NameValuePair> nvp = new ArrayList<NameValuePair>(); 

     nvp.add(new BasicNameValuePair("request", "category")); 

     nvp.add(new BasicNameValuePair("cat", value)); 

     ArrayList<JSONObject> jsondefs = Request.fetchData(this, nvp); 

     String[] defs = new String[jsondefs.size()]; 

     for (int i = 0; i < jsondefs.size(); i++) { 
      try { 
       defs[i] = jsondefs.get(i).getString("Name"); 
      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 

     uFoodAdapter adapter = new uFoodAdapter(this, R.layout.definition_list, 
       defs); 
     listView1.setAdapter(adapter); 

    } 

} 

답변

0

LinearLayout 캐스팅된다.

0

당신의 문제는 아마 여기 TextView tv = (TextView) view;에서 당신은 기본적으로 나는 그것이 목록보기를 채우는 데 사용되는 배열에서 항목을 얻을 수있는 onitemclick의 위치 매개 변수를 사용하여 전송할 데이터를 검색 한 방식을 변경 TextView

+0

'((TextView) view) .getText(). toString();'이라고 말하는 피곤한 예제이지만 작동하지 않습니다. –

+0

빠른 답변을위한 환호! 나는이 프로젝트를 위해 매우 짧은 시간 규모를 가지고있다! 애플 리케이션을 개발하라는 Uni 프로젝트. 나는 멀티미디어 학생이므로 이미 깊은 물에 던져져있는 앱을 만드는 작업을 할 수 있습니다! 이 문제가 해결되면 거기에서 명확한 실행이 이루어져야합니다! 다행히 ...... –

+0

B가 A를 확장하면 클래스 A의 객체를 다른 클래스 B로 캐스팅하는 경우에 명시 적 형변환이 유용하다는 것을 알아야합니다. – jeet