안드로이드 액티비티 전환하기 문의
글쓴이: christ17 / 작성시간: 토, 2016/01/09 - 11:17오후
안녕하세요.
목적:
나라 정보를 클릭하면 액티비티로 전환되어
각 나라의 수도를 알려주려고 합니다.
하지만 버튼이 공통으로 되어있어서
다른 나라 버튼을 클릭해도
같은 정보(같은 액티비티)만 나옵니다
지금 코드에서
나라마다 액티비티를
다르게 줄 수 있는 방법이 있을까요?
아니면, 나라마다 버튼을
각각 만드는 코드로 수정해야 할까요?
조언좀 해주시기 바랍니다.
감사합니다.
아래는 제 코드입니다.
MainActivity.java
package com.example.customlistview03; import java.util.ArrayList; import java.util.Collections; import com.example.customlistview03.SubActivity; import com.example.customlistview03.R; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private ListView mListView = null; private ListViewAdapter mAdapter = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mListView = (ListView) findViewById(R.id.mList); mAdapter = new ListViewAdapter(this); mListView.setAdapter(mAdapter); mAdapter.addItem(getResources().getDrawable(R.drawable.korea), "korea", "인구: 5022만"); mAdapter.addItem(getResources().getDrawable(R.drawable.china), "china", "인구: 13.57억"); mAdapter.addItem(getResources().getDrawable(R.drawable.canada), "canada", "인구: 3516만"); mAdapter.addItem(getResources().getDrawable(R.drawable.australia), "australia", "인구: 2313만"); mAdapter.addItem(getResources().getDrawable(R.drawable.usa), "usa", "인구: 3.189억"); mAdapter.addItem(getResources().getDrawable(R.drawable.italy), "italy", "인구: 5983만"); mListView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View v, int position, long id){ ListData mData = mAdapter.mListData.get(position); Toast.makeText(MainActivity.this, mData.mNation, Toast.LENGTH_SHORT).show(); } }); } private class ViewHolder { public ImageView mIcon; public TextView mNation; public TextView mPopulation; } private class ListViewAdapter extends BaseAdapter { private Context mContext = null; private ArrayList<ListData> mListData = new ArrayList<ListData>(); public ListViewAdapter(Context mContext) { super(); this.mContext = mContext; } @Override public int getCount() { return mListData.size(); } @Override public Object getItem(int position) { return mListData.get(position); } @Override public long getItemId(int position) { return position; } public void addItem(Drawable icon, String mTitle, String mDate){ ListData addInfo = null; addInfo = new ListData(); addInfo.mIcon = icon; addInfo.mNation = mTitle; addInfo.mPopulation = mDate; mListData.add(addInfo); } public void remove(int position){ mListData.remove(position); dataChange(); } public void sort(){ Collections.sort(mListData, ListData.ALPHA_COMPARATOR); dataChange(); } public void dataChange(){ mAdapter.notifyDataSetChanged(); } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { holder = new ViewHolder(); LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.listview_item, null); holder.mIcon = (ImageView) convertView.findViewById(R.id.mImage); holder.mNation = (TextView) convertView.findViewById(R.id.mText); holder.mPopulation = (TextView) convertView.findViewById(R.id.mDate); convertView.setTag(holder); }else{ holder = (ViewHolder) convertView.getTag(); } ListData mData = mListData.get(position); if (mData.mIcon != null) { holder.mIcon.setVisibility(View.VISIBLE); holder.mIcon.setImageDrawable(mData.mIcon); }else{ holder.mIcon.setVisibility(View.GONE); } holder.mNation.setText(mData.mNation); holder.mPopulation.setText(mData.mPopulation); return convertView; } } public void mOnClick(View v) { Intent intent = new Intent(this, SubActivity.class); startActivity(intent); } }
ListData.java
package com.example.customlistview03; import java.text.Collator; import java.util.Comparator; import android.graphics.drawable.Drawable; public class ListData { public Drawable mIcon; public String mNation; public String mPopulation; public static final Comparator<ListData> ALPHA_COMPARATOR = new Comparator<ListData>() { private final Collator sCollator = Collator.getInstance(); @Override public int compare(ListData mListDate_1, ListData mListDate_2) { return sCollator.compare(mListDate_1.mNation, mListDate_2.mNation); } }; }
SubActivity.java
package com.example.customlistview03; import com.example.customlistview03.R; import android.app.Activity; import android.os.Bundle; import android.view.View; public class SubActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.subactivity); // TODO Auto-generated method stub } public void mOnClick(View v) { finish(); } }
Forums:
아래는 layout 코드입니다.
listview_item.xml
subactivity.xml
댓글 달기