当我使用此代码时:
listView.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> a, View v, int position, long id)
{
String itemname = new Integer(position).toString(); <= this
Intent intent = new Intent();
intent.setClass(Spisok.this, Prob.class);
Bundle b = new Bundle();
b.putString("posit", itemname);
intent.putExtras(b);
startActivity(intent);
}
});
我有项目的位置,但我想得到身份证明。 这非常重要,因为在我的情况下它们并不相同。 帮我纠正这个代码(标记的行),我可以得到它(完全是ID)。
习题:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_prob);
Bundle bundle = getIntent().getExtras();
String itemname = "n" + bundle.getString("posit");
Context context = getBaseContext();
String text = readRawTextFile(context, getResources().getIdentifier(itemname, "raw", "ru.ibir.irrveb"));
WebView myWebView = (WebView) findViewById(R.id.webView);
myWebView.setBackgroundColor(0);
String summary = "<!Doctype html><html><head><meta charset=utf-8></head><body>" + text + "</body></html>";
myWebView.loadData(summary, "text/html", "utf-8");
}
public static String readRawTextFile(Context ctx, int resId)
{
InputStream inputStream = ctx.getResources().openRawResource(resId);
InputStreamReader inputreader = new InputStreamReader(inputStream);
BufferedReader buffreader = new BufferedReader(inputreader);
String line;
StringBuilder text = new StringBuilder();
try {
while (( line = buffreader.readLine()) != null) {
text.append(line);
text.append('\n');
}
} catch (IOException e) {
return null;
}
return text.toString();
}
}
答案 0 :(得分:3)
也许我错过了什么,但是不是最后一个为您提供所需ID的参数?
编辑:评论中提到的替代方案:
v.getId()
,但请注意通过针对View.NO_ID进行测试来确保它是实际ID。答案 1 :(得分:0)
我决定拿到名字,没关系, 甲强>
private static final String TAG_NAME = "name";
listView.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
String key = ((TextView) view.findViewById(R.id.text1)).getText().toString();
Intent intent = new Intent(getApplicationContext(), Prob.class);
intent.putExtra(TAG_NAME, key);
startActivity(intent);
}
});
}
<强>乙强>
Intent intent = getIntent();
String Name = intent.getStringExtra(TAG_NAME);