Salman Salman - 2 months ago 14
Android Question

on item click in array of string

I made an android app in which the user click the favorite menu item button and the page name saves in the favorite.class activity. But when I click on any item in the favorite list it opens only one specific class and not others which I want. Here is my code please look at the code and tell me what should I do to make it first in

ListView
form and then to click the item which opens the correct activity from it.

favorite.xml
layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_marginLeft="17dp">

<TextView
android:id="@+id/empty_textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/no_bookmark"
android:textSize="16sp"
android:textColor="#000000"
android:
android:textStyle="bold"/>
<TextView
android:id="@+id/bookmark_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:textSize="16sp"
android:textColor="#000000"
android:textStyle="bold"/>
<!-- ScrollView is needed when adding views, or only the last view will show up -->

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
android:id="@+id/bookmark_insert_point"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>

</LinearLayout>


Favorite.Class
activity:

public class Favorite extends Activity {

private TextView mEmptyText;
private LinearLayout mBookmarkLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.favorite);

// this code is used for the action bar color change//
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#6B8E23")));
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);

mEmptyText = (TextView) findViewById(R.id.empty_textview);
mBookmarkLayout = (LinearLayout) findViewById(R.id.bookmark_insert_point);

getAllKeys();
}
private void getAllKeys()
{
SharedPreferences sp = this.getSharedPreferences("bookmarks", MODE_PRIVATE);
Map<String,?> keys = sp.getAll();

int count = 0;
for(Map.Entry<String,?> entry : keys.entrySet())
{
String value = entry.getValue().toString();
System.out.println("!!!!!!!!!!!!!!!!!value = "+value);
String delimiter = ",";
String[] values_array = value.split(delimiter);
addBookmark(values_array);
count++; //keep track of the number of bookmarks
}

//if there are no bookmarks, display a text view saying so. Otherwise, make the text view go away
if (count == 0)
{
mEmptyText.setVisibility(View.VISIBLE);
mEmptyText.setText(getString(R.string.no_bookmark));
}
else
mEmptyText.setVisibility(View.GONE);

}

@SuppressWarnings("deprecation")
private void addBookmark(final String[] values_array)
{
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.favorite, null);

final TextView text = (TextView) v.findViewById(R.id.bookmark_text);

text.setText(values_array[1]);

// insert into main view
mBookmarkLayout.addView(v, 0, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
System.out.println("!!!!!!!!!!!!!!!!!!!!Just added a view");
text.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

if(text.equals("Atherosclerosis"));
Intent i=new Intent(Favorite.this,Atherosclerosis.class);
startActivity(i);
}
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.favorite, menu);
return true;
}

}


and finally the activity from where user click for add to favorite:

public boolean onOptionsItemSelected(MenuItem item) {
// Take appropriate action for each action item click


switch (item.getItemId()) {
case R.id.id_search:
Intent newActivity0 = new Intent(this,Search.class);
startActivity(newActivity0);
return true;
case R.id.id_favorit:
SharedPreferences sp = this.getSharedPreferences("bookmarks", MODE_PRIVATE);
Editor editor = sp.edit();
editor.putString("atherosclerosis", "com.kmcpesh.shortreviewofcardiology.Favorite,Atherosclerosis");
editor.commit();
Toast.makeText(getApplicationContext(), "Item Added to Favorite List!", Toast.LENGTH_SHORT).show();

return true;

default:
return super.onOptionsItemSelected(item);
}

}

Answer

well i guess what you have to do is setOnItemClickListener in everyone of the list items... an example is

private void registerCallClickBack() {
    // TODO Auto-generated method stub
    ListView list = (ListView)findViewById(R.id.listView1);
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View viewClicked, int position,
                long id) {
            // TODO Auto-generated method stub
            //String message = "You have chosen the " + id + "item";
            //Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
            Intent intent = new Intent("package.Activity name");
            startActivity(intent);
        }
    });
}

this will either display a toast message of every listview item clicked or start a new intent..

Comments