2016-06-15 5 views
0

基本的に私がやりたいことは次のとおりです。自分のアプリケーションにカスタムリストアダプターのリストビューがあります。しかし、私はTextLabelを呼び出し元のアクティビティに基づいて変更したい。商品リスト、お気に入り、履歴のような3つのリストがあります。だから私たちが呼び出すことができる方法はありますか(parentActivity)?どのアクティビティがカスタムリストアダプタを呼び出しているかを知る方法は?

簡単な解決策は、すべてのリストのレイアウトを設計することですが、そのような冗長なコードは必要ありません。

customListAdapter:

public class CustomListAdapter extends BaseAdapter { 

private final Context context; 
private ArrayList<Product> products=new ArrayList<>() ; 

public CustomListAdapter(Context context, ArrayList<Product> product) { 


    this.context=context; 
    this.products=product; 

} 

@Override 
public int getCount() { 
    return products.size(); 
} 

@Override 
public Object getItem(int position) { 
    return null; 
} 

@Override 
public long getItemId(int position) { 
    return 0; 
} 

public View getView(int position, View view, ViewGroup parent) { 
    LayoutInflater inflater=(LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View rowView=inflater.inflate(R.layout.product_list_item, null, true); 

    TextView title = (TextView) rowView.findViewById(R.id.product_title); 
    //TextView url = (TextView) rowView.findViewById(R.id.product_url); 
    TextView createDate = (TextView) rowView.findViewById(R.id.product_create_date); 
    ImageView icon=(ImageView)rowView.findViewById(R.id.imageArrow); 


    ........... 
    title.setText(productInfo.getTitle()); 

    createDate.setText("Created Date: "+productInfo.getCreatedDate()); 
    icon.setImageResource(R.drawable.go_right); 
    return rowView; 

}; 
} 

だから私は、履歴リストのための修正日付で作成された日付を変更したいです。

+0

アダプタのコードを表示してください。 – Rohit5k2

答えて

2

アダプタを呼び出すと、アクティビティコンテキストが渡されます。これはあなたのアダプターを呼び出すする方法である:あなたはこの

ソリューション1

String currentActivity = this.context.getClass().getName(); 
if(!TextUtils.isEmpty(currentActivity)) { 
    if(currentActivity.contains("com.example.activity1")){ 
     // do this 
    } 
    else if(currentActivity.contains("com.example.activity2")){ 
     // do that 
    } 
} 

ソリューション2

if(this.context instanceof Activity1){ 
    // do this 
} 
else if(this.context instanceof Activity2){ 
    // do that 
} 

ノートのように呼び出し元のアクティビティを識別するためにそれを使用することができます

CustomListAdapter myCustomListAdapter = new CustomListAdapter(this, myProducts); // In activity 

または

CustomListAdapter myCustomListAdapter = new CustomListAdapter(getActivity(), myProducts); // In Fragment 
+0

が私のために働いた。ありがとう –

関連する問題