简体   繁体   中英

Pass data in other activity

I have to pass my value from one activity to other one. I download data from database with Json and it works propelly.

My First Activity:

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;
 }

I have to pass the value of:

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

My Second Activity i have the code:

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

How i can do it? if i put:

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

i have the error: Variable is accessed within inner class. Needs to be declared final

Suggestion: Use an ArrayAdapter rather than BaseAdapter if you are using List data anyway.

As the IDE indicates, make a final variable.

...

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

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

Then, you can use that instead of repeatedly calling 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);

    }
});

When you use valueList in a method that creates a new onTouchListener for an instance, that is an inner class.

Any variables accessed by that class has to be final or declared inside the class. Meanign if you create a new int inside the inner class, that doesn t have to be final. But if the int is declared outside, it has to be final.

In your case, the variable position has to be marked as final

I guess you're trying to do something like this

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

The method putExtra expects a key (String) and a value (in your case you're passing a string value).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM