【问题标题】:Need to get id instead of position需要获取 id 而不是 position
【发布时间】:2014-04-23 09:26:20
【问题描述】:

当我使用此代码时:

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。 这非常重要,因为在我的情况下它们并不相等。 帮我更正这段代码(标记线),我可以得到它(确切的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();
}
}

【问题讨论】:

    标签: android list listview android-listview onitemclicklistener


    【解决方案1】:

    也许我遗漏了一些东西,但最后一个参数不是为您提供了您想要的 ID 吗?

    编辑:cmets 中提到的替代方案:

    • 如果需要行 ID,只需使用最后一个参数即可。
    • 如果您需要视图 ID,请使用 v.getId(),但请注意通过针对 View.NO_ID 进行测试以确保它是实际 ID。

    【讨论】:

    • 这是位置,但我想获得 id。如果我将“位置”替换为“ID”,它将不起作用。
    • 这将为您提供行的 ID。如果您想要视图的 ID,可以使用 v.getId(),但您需要检查返回的值是否不是 View.NO_ID。
    • 我已经编辑了答案,提出了 cmets 中提到的两个备选方案。 OP,这能回答你原来的问题吗?
    • 感谢您的回答!我使用了较低答案的例子,但他错了,给出了不正确的结果。如果我知道如何更正代码,请写出来。
    【解决方案2】:

    我决定取名字,没关系, 答:

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

    B

    Intent intent = getIntent();
    String Name = intent.getStringExtra(TAG_NAME);
    

    【讨论】:

      猜你喜欢
      • 2021-09-23
      • 1970-01-01
      • 2016-06-18
      • 1970-01-01
      • 2012-08-06
      • 2021-05-09
      • 2017-01-23
      • 1970-01-01
      • 2021-09-10
      相关资源
      最近更新 更多