Question

When I use this code:

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

I have item's position, but I want to get id. This is very important because they are not equal in my case. Help me to correct this code (marked line) that I can get it (exactly ID).

Prob:

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();
}
}
Was it helpful?

Solution 2

I decided to get the name and it is ok, A:

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

OTHER TIPS

Maybe I'm missing something, but isn't the last parameter providing you with the ID you want?

Edit: Alternatives mentioned in the comments:

  • If you want the row ID, just use the last parameter.
  • If you want the view ID, use v.getId(), but be careful to make sure it's an actual ID by testing against View.NO_ID.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top