2017-05-24 1 views
0

私はあるアクティビティから別のアクティビティに自分の値を渡す必要があります。 データベースからJsonを使用してデータをダウンロードします。それはpropellyで動作します。他のアクティビティでデータを渡す

私の最初の活動:私は、コードを持っている

viewItem.txtPrice.setText(valueList.get(position).prezzo_prodotto); 

マイセカンドアクティビティー:

public class ListAdapter extends BaseAdapter 
{ 
Context context; 
List<cources> valueList; 
HashMap<String, String> resultp = new HashMap<String, String>(); 
public ListAdapter(List<cources> listValue, Context context) 
{ 
    this.context = context; 
    this.valueList = listValue; 
} 

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

@Override 
public Object getItem(int position) 
{ 
    return this.valueList.get(position); 
} 

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

@Override 
public View getView(int position, View convertView, ViewGroup parent) 
{ 
    ViewItem viewItem = null; 
    if(convertView == null) 
    { 
     viewItem = new ViewItem(); 
     LayoutInflater layoutInfiater = (LayoutInflater)this.context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 
     //LayoutInflater layoutInfiater = LayoutInflater.from(context); 
     convertView = layoutInfiater.inflate(R.layout.list_adapter_view_test, null); 

     viewItem.txtTitle = (TextView)convertView.findViewById(R.id.nome_prodotto); 
     viewItem.txtDescription = (TextView)convertView.findViewById(R.id.descrizione_prodotto); 
     viewItem.txtPrice = (TextView)convertView.findViewById(R.id.prezzo); 
     convertView.setTag(viewItem); 
    } 
    else 
    { 
     viewItem = (ViewItem) convertView.getTag(); 
    } 
    viewItem.txtPrice.setText(valueList.get(position).prezzo_prodotto); 
    viewItem.txtTitle.setText(valueList.get(position).nome_prodotto); 
    viewItem.txtDescription.setText(valueList.get(position).descrizione_prodotto); 


    convertView.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 

      // Get the position 
      Intent intent = new Intent(context, Dettaglio_Prodotto.class); 
      // Pass all data country 
      intent.putExtra("nome_prodotto", "test"); 

      // Start SingleItemView Class 
      context.startActivity(intent); 

     } 
    }); 


    return convertView; 
} 

} 

class ViewItem 
{ 
TextView txtTitle; 
TextView txtDescription; 
TextView txtPrice; 
} 

私はの値を渡す必要が

Intent i = getIntent(); 
    nome_prodotto = i.getStringExtra("nome_prodotto"); 

私はそれを行うことができますどのように? は私が入れた場合:

intent.putExtra("nome_prodotto",viewItem.txtTitle.setText(valueList.get(position).nome_prodo‌​tto)); 

私はエラーを持っている: 変数を内部クラス内でアクセスされます。私はあなたがこの

intent.putExtra("nome_prodotto", valueList.get(position).prezzo_prodotto); 

putExtraはキー(文字列)と値を期待する方法のような何かをしようとしていると思います

+0

どうしたのですか? – litelite

+0

私が置く場合: intent.putExtra( "nome_prodotto"、viewItem.txtTitle.setText(valueList.get(position).nome_prodotto)); 私はエラーがあります: 変数は内部クラス内でアクセスされます。最終的に宣言する必要があります。 @liteite –

+0

エラーが表示されます。変数をfinalとしてマークします。最終的なものでなければならない 'viewItem'が表示されます – litelite

答えて

-1

最終的に宣言する必要があります(あなたのケースでは、文字列値を渡しています)。

+1

私はエラーがあります:変数 "位置"は、内部クラス内でアクセスされます。 –

1

提案:とにかくListデータを使用している場合はBaseAdapterではなくArrayAdapterを使用してください。

IDEが示すとおり、最終的な変数を作成します。あなたは、インスタンスのための新しいonTouchListenerを作成する方法でVALUELISTを使用する場合

... 

else 
{ 
    viewItem = (ViewItem) convertView.getTag(); 
} 

// This is the item in the current position 
final cources item = (cources) getItem(position); 

次に、あなたが繰り返しvalueList.get(position)

viewItem.txtPrice.setText(item.prezzo_prodotto); 
viewItem.txtTitle.setText(item.nome_prodotto); 
viewItem.txtDescription.setText(item.descrizione_prodotto); 


convertView.setOnClickListener(new View.OnClickListener() { 

    @Override 
    public void onClick(View arg0) { 

     // Get the position 
     Intent intent = new Intent(context, Dettaglio_Prodotto.class); 
     // Pass all data country 
     intent.putExtra("nome_prodo‌​tto", item.nome_prodo‌​tto); 

     // Start SingleItemView Class 
     context.startActivity(intent); 

    } 
}); 
+0

最終的なソースを置く必要がある項目item = getItem(position); –

+0

ちょうど私が私の答えでしたように。 'viewItem.txtPrice.setText'の直上 –

0

を呼び出すのではなく、それを使用することができ、それは内部クラスです。

そのクラスによってアクセスされるすべての変数は、finalであるか、クラス内で宣言されている必要があります。あなたが内側のクラスの中に新しいintを作成した場合、それは でなければなりません。しかしintが外側に宣言されている場合は、finalでなければなりません。

あなたのケースでは、変数positionを最終としてマークする必要があります

関連する問題