嘿伙计们!
公平警告:我对编程总体上非常新,更不用说java和android了,所以说实话,我可能甚至不知道如何以正确的方式提出正确的问题。因此,我感谢任何愿意花时间看过去的人,如果可以的话,给予帮助!此外,如果你能找到一种方法来愚弄你所拥有的任何解决方案,并像我三岁那样解释它,我也非常感激。我已经查看了developer.android.com,所有技术术语都让我的困惑变得更糟。
所以我到目前为止所做的是
我有一个以下代码(包括java和xml),一旦打开它就会显示三个类别'我的应用程序(即activities.java文件)在activity_main.xml文件中。由于现在这三个都是相同的,我已经在下面发布了MidlandsActivity.java的代码。
OnClick会打开您点击的任何活动类别,并在list_items.xml文件中显示arraylist (如MidlandsActivity.java文件中所示)。
所以现在......我希望能做的是:
我希望您能够点击列表中的任何项目(您目前可以使用但目前无法执行任何操作)然后显示您在新活动中点击的任何列表项目(我猜对了吗? ListDetails.java是为了进行对话,但我还没有制作,因为我迷失了。或者它应该被编码到MidlandsActivity.java和每个额外的类别活动中,例如?)。然后将该活动显示到list_details.xml文件,以便完全查看所选类别arraylist中的数据。
目前我确实为此设置了xml文件(list_details.xml),但我不知道接下来需要做什么或者放在哪里。
任何帮助或建议?
package com.example.android.hauntedsc;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import java.util.ArrayList;
public class MidlandsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.word_list);
//Create an array of words
final ArrayList<Word> words = new ArrayList<Word>();
words.add(new Word(
"title",
"address\n" +
"citystatezip\n\n",
"The Facts:\n\n" +
"factstext\n\n" +
"The Legend:\n\n" +
"legendtext",
R.drawable.haunted));
words.add(new Word(
"title",
"address\n" +
"citystatezip\n\n",
"The Facts:\n\n" +
"factstext\n\n" +
"The Legend:\n\n" +
"legendtext",
R.drawable.haunted));
words.add(new Word(
"title",
"address\n" +
"citystatezip\n\n",
"The Facts:\n\n" +
"factstext\n\n" +
"The Legend:\n\n" +
"legendtext",
R.drawable.haunted));
words.add(new Word(
"title",
"address\n" +
"citystatezip\n\n",
"The Facts:\n\n" +
"factstext\n\n" +
"The Legend:\n\n" +
"legendtext",
R.drawable.haunted));
words.add(new Word(
"title",
"address\n" +
"citystatezip\n\n",
"The Facts:\n\n" +
"factstext\n\n" +
"The Legend:\n\n" +
"legendtext",
R.drawable.haunted));
words.add(new Word(
"title",
"address\n" +
"citystatezip\n\n",
"The Facts:\n\n" +
"factstext\n\n" +
"The Legend:\n\n" +
"legendtext",
R.drawable.haunted));
words.add(new Word(
"title",
"address\n" +
"citystatezip\n\n",
"The Facts:\n\n" +
"factstext\n\n" +
"The Legend:\n\n" +
"legendtext",
R.drawable.haunted));
words.add(new Word(
"title",
"address\n" +
"citystatezip\n\n",
"The Facts:\n\n" +
"factstext\n\n" +
"The Legend:\n\n" +
"legendtext",
R.drawable.haunted));
// Create an {@link ArrayAdapter}, whose data source is a list of Strings. The
// adapter knows how to create layouts for each item in the list, using the
// simple_list_item_1.xml layout resource defined in the Android framework.
// This list item layout contains a single {@link TextView}, which the adapter will set to
// display a single word.
WordAdapter adapter =
new WordAdapter(this, words, R.color.category_midlands);
// Find the {@link ListView} object in the view hierarchy of the {@link Activity}.
// There should be a {@link ListView} with the view ID called list, which is declared in the
// word_list.xml file.
ListView listView = (ListView) findViewById(R.id.list);
// Make the {@link ListView} use the {@link ArrayAdapter} we created above, so that the
// {@link ListView} will display list items for each word in the list of words.
// Do this by calling the setAdapter method on the {@link ListView} object and pass in
// 1 argument, which is the {@link ArrayAdapter} with the variable name itemsAdapter.
listView.setAdapter(adapter);
// Set a click listener to play the audio when the list item is clicked on
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
// Get the {@link Word} object at the given position the user clicked on
Word word = words.get(position);
}
});
}
}
package com.example.android.hauntedsc;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the content of the activity to use the activity_main.xml layout file
setContentView(R.layout.activity_main);
//Find the View that shows the lowcountry catagory
TextView lowcountry = (TextView)findViewById(R.id.lowcountry);
//set a clicklistener on that View
lowcountry.setOnClickListener(new View.OnClickListener() {
// The code in this method will be executed when the numbers category is clicked on.
@Override
public void onClick(View view) {
//Create a new intent to open the (@Link LowcountryActivity)
Intent lowcountryIntent = new Intent(MainActivity.this,LowcountryActivity.class);
//Start New Activity
startActivity(lowcountryIntent);
}
});
//Find the View that shows the upstate catagory
TextView upstate = (TextView)findViewById(R.id.upstate);
//set a clicklistener on that View
upstate.setOnClickListener(new View.OnClickListener() {
// The code in this method will be executed when the numbers category is clicked on.
@Override
public void onClick(View view) {
//Create a new intent to open the (@Link UpstateActivity)
Intent upstateIntent = new Intent(MainActivity.this,UpstateActivity.class);
//Start New Activity
startActivity(upstateIntent);
}
});
//Find the View that shows the midlands catagory
TextView midlands = (TextView)findViewById(R.id.midlands);
//set a clicklistener on that View
midlands.setOnClickListener(new View.OnClickListener() {
// The code in this method will be executed when the numbers category is clicked on.
@Override
public void onClick(View view) {
//Create a new intent to open the (@Link midlandsActivity)
Intent midlandsIntent = new Intent(MainActivity.this,MidlandsActivity.class);
//Start New Activity
startActivity(midlandsIntent);
}
});
}
}
package com.example.android.hauntedsc;
public class Word {
//
private String mDescription;
//
private String mAddress;
//
private String mTitle;
/** Image resource ID for the word */
private int mImageResourceId = NO_IMAGE_PROVIDED;
/** Constant value that represents no image was provided for this word */
private static final int NO_IMAGE_PROVIDED = -1;
public Word(String description, String address, String title) {
mDescription = description;
mAddress = address;
mTitle = title;
}
public Word(String description, String address, String title, int imageResourceId) {
mDescription = description;
mAddress = address;
mTitle = title;
mImageResourceId = imageResourceId;
}
//Get the description of the list item.
public String getdescription() { return mDescription;}
//Get the address of the list item.
public String getaddress() { return mAddress; }
//Get the title of the list item.
public String gettitle() { return mTitle; }
//Return the image resource ID of the list item.
public int getImageResourceId() { return mImageResourceId; }
//Returns whether or not there is an image for this list item.
public boolean hasImage() { return mImageResourceId != NO_IMAGE_PROVIDED; }
}
package com.example.android.hauntedsc;
import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
public class WordAdapter extends ArrayAdapter<Word> {
//Resource ID for background color of this list of words
private int mColorResourceId;
// Here, we initialize the ArrayAdapter's internal storage for the context and the list.
// the second argument is used when the ArrayAdapter is populating a single TextView.
// Because this is a custom adapter for two TextViews and an ImageView, the adapter is not
// going to use this second argument, so it can be any value. Here, we used 0.
public WordAdapter(Context context, ArrayList<Word> words, int colorResourceId) {
super(context, 0, words);
mColorResourceId = colorResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Check if the existing view is being reused, otherwise inflate the view
View listItemView = convertView;
if(listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
// Get the {@link AndroidFlavor} object located at this position in the list
Word currentWord = getItem(position);
// Find the TextView in the list_item.xml layout with the ID version_name
TextView titleTextView = (TextView) listItemView.findViewById(R.id.description_text_view);
// Get the version name from the current AndroidFlavor object and
// set this text on the name TextView
titleTextView.setText(currentWord.gettitle());
// Find the TextView in the list_item.xml layout with the ID version_number
TextView addressTextView = (TextView) listItemView.findViewById(R.id.address_text_view);
// Get the version number from the current AndroidFlavor object and
// set this text on the number TextView
addressTextView.setText(currentWord.getaddress());
// Find the TextView in the list_item.xml layout with the ID version_number
TextView descriptionTextView = (TextView) listItemView.findViewById(R.id.title_text_view);
// Get the version number from the current AndroidFlavor object and
// set this text on the number TextView
descriptionTextView.setText(currentWord.getdescription());
// Find the ImageView in the list_item.xml layout with the ID image.
ImageView imageView = (ImageView) listItemView.findViewById(R.id.image_view);
// Check if an image is provided for this word or not
if (currentWord.hasImage()) {
// If an image is available, display the provided image based on the resource ID
imageView.setImageResource(currentWord.getImageResourceId());
// Make sure the view is visible
imageView.setVisibility(View.VISIBLE);
} else {
// Otherwise hide the ImageView (set visibility to GONE)
imageView.setVisibility(View.GONE);
}
//Set the theme color for the list item
View textContainer = listItemView.findViewById(R.id.text_container);
//find the color that the resource ID maps too
int color = ContextCompat.getColor(getContext(), mColorResourceId);
//Set the background color of the text container View
textContainer.setBackgroundColor(color);
//Set the theme color for the list item
View imageContainer = listItemView.findViewById(R.id.list_item_layout);
//find the color that the resource ID maps too
int icolor = ContextCompat.getColor(getContext(), mColorResourceId);
//Set the background color of the text container View
imageContainer.setBackgroundColor(icolor);
// Return the whole list item layout (containing 2 TextViews and an ImageView)
// so that it can be shown in the ListView
return listItemView;
}
}
<?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="match_parent"
android:layout_height="match_parent"
android:background="@color/background"
android:orientation="vertical"
tools:context="com.example.android.hauntedsc.MainActivity">
<!-- Lowcountry category ((formerly numbers))-->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/category_lowcountry">
<TextView
android:id="@+id/lowcountry"
style="@style/CategoryStyle"
android:background="?android:attr/selectableItemBackground"
android:text="@string/category_lowcountry" />
</FrameLayout>
<!-- Midlands category ((formerly family))-->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/category_midlands">
<TextView
android:id="@+id/midlands"
style="@style/CategoryStyle"
android:background="?android:attr/selectableItemBackground"
android:text="@string/category_midlands" />
</FrameLayout>
<!-- Upstate category ((formerly colors))-->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/category_upstate">
<TextView
android:id="@+id/upstate"
style="@style/CategoryStyle"
android:background="?android:attr/selectableItemBackground"
android:text="@string/category_upstate" />
</FrameLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/list_item_layout"
android:layout_width="match_parent"
android:layout_height="@dimen/list_item_height"
android:background="@color/background"
android:orientation="horizontal">
<ImageView
android:id="@+id/image_view"
android:layout_width="@dimen/list_item_height"
android:layout_height="@dimen/list_item_height"/>
<LinearLayout
android:id="@+id/text_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="@id/image_view"
android:orientation="vertical"
android:paddingLeft="16dp">
<TextView
android:id="@+id/title_text_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:gravity="center_vertical"
android:layout_weight="1"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="@android:color/white"
android:textStyle="bold"
android:paddingTop="4dp"
android:paddingRight="8dp"
android:paddingBottom="4dp"
tools:text=""/>
<TextView
android:id="@+id/address_text_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:gravity="center_vertical"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="@android:color/white"
android:paddingTop="4dp"
android:paddingRight="8dp"
android:paddingBottom="4dp"
tools:text="" />
<TextView
android:id="@+id/description_text_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0"
android:gravity="top"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="@android:color/white"
android:paddingTop="4dp"
android:paddingRight="8dp"
android:paddingBottom="4dp"
tools:text="" />
</LinearLayout>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/detail_title_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="@android:color/white"
android:textStyle="bold"
android:paddingTop="4dp"
android:paddingRight="8dp"
android:paddingBottom="4dp"/>
<TextView
android:id="@+id/detail_address_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="@android:color/white"
android:paddingTop="4dp"
android:paddingRight="8dp"
android:paddingBottom="4dp"/>
<TextView
android:id="@+id/detail_description_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="@android:color/white"
android:paddingTop="4dp"
android:paddingRight="8dp"
android:paddingBottom="4dp"/>
</LinearLayout>
</RelativeLayout>
</ScrollView>
答案 0 :(得分:0)
创建一个新活动并将其命名为ListDetails.java,然后将以下内容添加到现有的Midlands.java
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
// Get the {@link Word} object at the given position the user clicked on
//Word word = words.get(position);
Intent intent = new Intent(Midlands.this, ListDetails.class);
startActivity(intent);
}
答案 1 :(得分:0)
所以,您只是希望自己的ListView
项目带您进入新的Activity
?
在您设置MidlandsActivity
ListView
的{{1}}中执行此操作:
onClick
这样做是启动一个新的listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Word word = words.get(position);
Intent intent = new Intent(MidlandsActivity.this, ListDetails.class);
intent.putExtra("myWord", word);
startActivity(intent);
}
});
并传递Activity
对象的值。 但是,您的Word
类将实现Word
接口,以便通过意图传递。你可以找到一个很好的答案here。
准备就绪之后,您的对象将被传递到您的Parcelable
活动,您可以使用
ListDetails
中检索它
onCreate(..)
答案 2 :(得分:0)
除了@Suhayl SH编写的内容之外 - 您已经说过,您希望所点击的项目在您开始的活动中可见。在给定的代码中,此活动将是ListDetails。为此,请使用Suhayl提供的代码并添加以下内容:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
// Get the {@link Word} object at the given position the user clicked on
//Word word = words.get(position);
Intent intent = new Intent(Midlands.this, ListDetails.class);
Word word = word.get(position);
intent.putExtra("wordToBeDisplayed",word);
startActivity(intent);
}
然后在ListDetails活动中,您可以使用以下方式显示单词:
Bundle b = getIntent().getExtras();
Word word = (Word) b.get("wordToBeDisplayed");
// The rest depends on your preferences about what to display