Java 在其他活动中传递数据
我必须将我的价值从一项活动传递到另一项活动。 我用Json从数据库下载数据,它可以正常工作 我的第一项活动:Java 在其他活动中传递数据,java,android,database,android-intent,Java,Android,Database,Android Intent,我必须将我的价值从一项活动传递到另一项活动。 我用Json从数据库下载数据,它可以正常工作 我的第一项活动: public class ListAdapter extends BaseAdapter { Context context; List<cources> valueList; HashMap<String, String> resultp = new HashMap<String, String>(); public ListAdapter(List
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_prodotto));
我有一个错误:
变量是在内部类中访问的。需要被宣布为最终的我想你是在尝试这样做
intent.putExtra("nome_prodotto", valueList.get(position).prezzo_prodotto);
putExtra方法需要一个键(字符串)和一个值(在您传递字符串值的情况下)。建议:如果仍然使用
列表
数据,请使用阵列适配器
而不是基本适配器
如IDE所示,生成最后一个变量
...
else
{
viewItem = (ViewItem) convertView.getTag();
}
// This is the item in the current position
final cources item = (cources) getItem(position);
然后,您可以使用它,而不是重复调用valueList.get(position)
在为实例创建新onTouchListener的方法中使用valueList时,该实例是一个内部类 该类访问的任何变量都必须是final或在类内声明。如果在内部类中创建一个新的int,那么 不必是最后的。但是如果int在外部声明,它必须是最终的
在您的情况下,变量
position
必须标记为final有什么问题吗?如果我输入:intent.putExtra(“nome_prodotto”,vieitem.txtitle.setText(valueList.get(position.nome_prodotto));我有一个错误:变量是在内部类中访问的。需要声明为final@litedo,如错误所示。将变量标记为final。据我所见,必须是viewItem
final@liteliteviewItem
不能设置为最终。它是根据convertView
的null值分配的condition@cricket_007然后他必须重构我的错误:变量“position”是在内部类中访问的。需要声明finalWhere我必须把final cources item=getItem(position);?正如我在回答中所做的那样。就在viewItem.txtPrice.setText的正上方
...
else
{
viewItem = (ViewItem) convertView.getTag();
}
// This is the item in the current position
final cources item = (cources) getItem(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);
}
});