gpt4 book ai didi

android - 从数组中获取字符串

转载 作者:行者123 更新时间:2023-11-29 22:05:49 25 4
gpt4 key购买 nike

我想从 ListView 返回选定的值,如果我使用默认 ListView 我可以做到,但现在我自定义 ListView ,得到 ImageView 、ratingbarm 和几个 TextView 。用户选择后如何在 toast 处返回 title 的值?下面是我的代码:

    @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.appslist);

list=(ListView)findViewById(R.id.lvApps);


SoapObject Request = new SoapObject (NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

soapEnvelope.dotNet = true;
soapEnvelope.setOutputSoapObject(Request);

AndroidHttpTransport aht = new AndroidHttpTransport(URL);

try
{
aht.call(SOAP_ACTION, soapEnvelope);
SoapObject resultString = (SoapObject) soapEnvelope.getResponse();

String[] strTitle = new String[resultString.getPropertyCount()];
String[] strDeveloper = new String[resultString.getPropertyCount()];
String[] strRating = new String[resultString.getPropertyCount()];
String[] strLogo = new String[resultString.getPropertyCount()];

for(int i =0; i<resultString.getPropertyCount(); i++)
{
SoapObject array = (SoapObject) resultString .getProperty(i);
strTitle[i] = array.getProperty(1).toString(); //get title
strDeveloper[i] = array.getProperty(3).toString(); //get developer
strRating[i] = array.getProperty(4).toString(); //get rating
strLogo[i] = array.getProperty(5).toString(); //get photo
}
adapter=new AppsListAdapter(this, strTitle, strDeveloper, strRating, strLogo);
list.setAdapter(adapter);

list.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Object GetLabel = list.getItemAtPosition(arg2);
//Toast here
Toast.makeText(getApplicationContext(), GetLabel.toString(), Toast.LENGTH_SHORT).show();

});
}

catch(Exception e)
{

}
}
}

最佳答案

您应该在一项中创建所有数据的模型(bean)意味着将数据包装在对象中并创建该模型的数组并将该数组发送到适配器…………

但现在有几种方法可以激活您的任务:

1- 您拥有所选项目的位置,可以将其用作 strTitle 中的索引以获取特定标题。

字符串标题 = strTitle[位置];///if think can Put the null and length check if to make it safe

2-您可以使用 findViewbyid 从该 View 的回调 public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) 中查看列表,您可以获得具有标题的 TextView

String s =(String) ((TextView) view.findViewById(R.id.title)).getText();// View 是选中的行 View

TextView txt =(TextView)arg0.getChildAt(position-lv.firstVisiblePosition()).findViewById(R.id.mylistviewtextview);//arg0 是父 View 字符串关键字 = txt.getText().toString();

3-

关于android - 从数组中获取字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10784589/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com