在其他活动中传递数据
我必须将我的价值从一项活动传递给另一项活动。 我用Json从数据库下载数据,并且它可以工作。在其他活动中传递数据
我的第一个活动:
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;
}
我必须要通过的值:
viewItem.txtPrice.setText(valueList.get(position).prezzo_prodotto);
我的第二个活动我的代码:
Intent i = getIntent();
nome_prodotto = i.getStringExtra("nome_prodotto");
我该怎么办呢? 如果我把:
intent.putExtra("nome_prodotto",viewItem.txtTitle.setText(valueList.get(position).nome_prodotto));
我有错误: 变量被内部类访问。需要声明的最终
我猜你试图做这样的事情
intent.putExtra("nome_prodotto", valueList.get(position).prezzo_prodotto);
的方法putExtra需要一个密钥(字符串)和值(在你的情况你传递一个字符串值)。
我有错误:在内部类中访问变量“位置”。需要被宣布为最终 –
建议:如果您正在使用List
数据,则使用ArrayAdapter
而不是BaseAdapter
。
如IDE所示,做出最终变量。
...
else
{
viewItem = (ViewItem) convertView.getTag();
}
// This is the item in the current position
final cources item = (cources) getItem(position);
然后,当你在为一个实例创建一个新的onTouchListener的方法使用值列表,你可以使用,而不是重复调用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_prodotto", item.nome_prodotto);
// Start SingleItemView Class
context.startActivity(intent);
}
});
我必须把最终的资源item = getItem(position);? –
就像我在回答中所做的一样。直接在'viewItem.txtPrice.setText'之上 –
,这是一个内部类。
该类访问的任何变量必须是最终的或者在类中声明的。如果你在内部类中创建了一个新的int,那么这个结果是平均的,那么它不得不是最终的。但是如果int在外部声明,它必须是final。
在你的情况下,变量position
必须标记为最终
问题是什么? – litelite
如果我把: intent.putExtra(“nome_prodotto”,viewItem.txtTitle.setText(valueList.get(position).nome_prodotto)); 我有错误: 在内部类中访问变量。需要被宣布为最终 @litelite –
做错误说。将该变量标记为final。从我所能看到的'viewItem'必须是最终的 – litelite