2012-10-31 2 views
1

ListActivity에서 을 사용하여 데이터를 다른 ListActivity으로 보내려고합니다. OnItemClickListenerSharedPreferences으로 데이터를 추가했습니다. ArrayList을 사용하여 ListActivity 다른 데이터를 채우는 방법을 모르겠습니다. 아래는 코드입니다.공유 환경 설정 데이터의 목록 항목 채우기

convertView.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      SharedPreferences pref = getContext().getSharedPreferences(
        Constants.SHARED_PRED, 0); 
      Editor edit = pref.edit(); 

      edit.putString("TITLE", getItem(position).title); 
      edit.putString("CHAR", getItem(position).character); 
      edit.putString("TOTAL", getItem(position).totalpages+""); 
      edit.putString("THUMB", getItem(position).thumbnail); 
      edit.putInt("CODE", getItem(position).code); 
      edit.commit(); 

      System.out.println("Title >>>>>>" + getItem(position).title); 
      System.out.println("Character >>>>>>" + getItem(position).character); 
      System.out.println("Total >>>>>>" + getItem(position).totalpages); 
      System.out.println("thumb url >>>>>>" + getItem(position).thumbnail); 
      System.out.println("Code >>>>>>" + getItem(position).code); 

      Intent i = new Intent(getContext(), DetailPager.class); 
      i.putExtra("NPOS", position); 
      getContext().startActivity(i); 
     } 
    }); 

제 시나리오는 응용 프로그램에서 두 가지 활동이 있습니다. 첫 번째 활동은 SharedPreferences을 사용하여 ListActivity을 실행합니다. 두 번째 활동은 코드로 작성된 DetailPager.class을로드합니다. 내 문제는 ListActivity을 채우는 방법을 알지 못하기 때문에 OnClick 이벤트에 실행되지 않습니다.

내 ListActivity 클래스

private ListViewAdapter m_adapter; 
public ArrayList<String> cat = new ArrayList<String>(); 
private SharedPreferences preferences; 
private SharedPreferences.Editor editor; 
String title, character, thumbnail, totalPages; 
int Code; 

TextView titleView, charView, totalView; 
ImageView thumbView, removeView; 
Uri thumbUri; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setTheme(android.R.style.Theme_Black_NoTitleBar); 
    getWindow().requestFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.myputet); 
    init_UI(); 
} 

public void init_UI() { 
    preferences = getApplicationContext().getSharedPreferences(
      Constants.SHARED_PRED, 0); 
    editor = preferences.edit(); 

    title = preferences.getString("TITLE", ""); 
    character = preferences.getString("CHAR", ""); 
    thumbnail = preferences.getString("THUMB", ""); 
    thumbUri = Uri.parse(thumbnail); 
    totalPages = preferences.getString("TOTAL", ""); 
    Code = preferences.getInt("CODE", 0); 

    System.out.println("Getting Title " + title); 
    System.out.println("Getting character " + character); 
    System.out.println("Getting thumbnail " + thumbnail); 
    System.out.println("Getting thumbUri " + thumbUri); 
    System.out.println("Getting totalPages " + totalPages); 

    ListView lv = (ListView) findViewById(R.id.listview); 
    m_adapter = new ListViewAdapter(this, R.layout.myputet_item, cat); 
    lv.setAdapter(m_adapter); 
} 

private class ListViewAdapter extends ArrayAdapter<String> { 
    private ArrayList<String> items; 

    public ListViewAdapter(Context context, int textViewResourceId, 
      ArrayList<String> items) { 
     super(context, textViewResourceId, items); 
     this.items = items; 

    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View v = convertView; 
     if (v == null) { 
      LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      v = vi.inflate(R.layout.myputet_item, null); 
     } 
     String info = items.get(position); 

     if (info != null) { 
      titleView = (TextView) v.findViewById(R.id.titleTxtView); 
      charView = (TextView) v.findViewById(R.id.charTxtView); 
      totalView = (TextView) v.findViewById(R.id.totalTxtView); 
      thumbView = (ImageView) v.findViewById(R.id.charImageView); 
      removeView = (ImageView) v.findViewById(R.id.removeListItem); 

      titleView.setText(title); 
      charView.setText(character); 
      totalView.setText(totalPages); 
      thumbView.setImageURI(thumbUri); 
     } 
     return v; 
    } 
} 

@Override 
public void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 

} 
+1

나는 당신의 마지막 문장을 이해하지 않습니다. getItem (position)이 반환하는 것을 직렬화하고 i.putExtra()를 수행하는 것이 어떻습니까? – Shellum

+0

@ChuckNorris 나는 두 가지 다른 활동을 시작했기 때문에. 나는 다음 Intent에서 그 데이터를 사용하고 싶지 않다. 나는 다른 활동에서 전화해야 해. –

답변

2

음, 음 확실히 그런 식으로하지 않습니다. SharedPreferences에 모두 던지기보다는 Intent을 통해 해당 조각을 전달합니다. 대신 예를 들어

edit.putString("TITLE", getItem(position).title); 

당신은 당신의 세부 활동의 onCreate(), 사용 그리고

i.putExtra("TITLE", getItem(position).title); 

을 사용할 수 있습니다

Intent passedIntent = getIntent(); 
String title = passedIntent.getStringExtra("TITLE"); 
+0

그 데이터를 다음 인 텐트에 전달하고 싶지 않습니다. 다른 활동 클래스의 데이터를 사용해야합니다. –

+1

그게 당신의 의도 * *입니다. 'startActivity (i)'를 할 때, 그'Intent'를 다음'Activity'에 넘깁니다. 그 다음'Activity'의'onCreate()'에서,'Intent'와 전달 된 데이터를 검색 할 수 있습니다.'Intent' 문서를 읽어야합니다. – kcoppock

+0

두 개의 서로 다른 Intent의 데이터를 사용해야합니다. 그게 내가 혼란스러워하는거야. –