在项目中,单击字符串的数组



我制作了一个Android应用程序,用户在其中单击"喜欢的菜单"项按钮,并且页名保存在fely.class活动中。但是,当我单击"喜欢的列表"中的任何项目时,它仅打开一个特定类,而不是我想要的其他类。这是我的代码,请查看代码,并告诉我该怎么做才能首先以ListView表格制作,然后单击从中打开正确活动的项目。

favorite.xml布局:

<?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活动:

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

最后,用户单击以获取添加到喜欢的活动的活动:

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

好吧,我想您要做的就是列表项目中的每个人setOnitemClicklistener ...一个例子是

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

这将显示每个ListView项目的敬酒消息,或者启动新意图。

最新更新