gpt4 book ai didi

java - 将数据从一个 Activity 传递到 ArrayList 项目单击上的另一个 Activity

转载 作者:太空宇宙 更新时间:2023-11-04 12:47:07 26 4
gpt4 key购买 nike

我正在开发一个应用程序(字典)。
使用 Volley 从 JSON 格式的链接获取单词、发音、定义、图像名称。
然后解析 JSON 并在屏幕顶​​部显示单词列表和 EditText 以搜索单词。一切都好。但我不知道如何处理点击事件,如何将所选项目的数据传递给另一个 Activity 并在那里接收。
因此,当选择列表项时,DetailsActivity 应显示单词、发音、定义并设置图像名称(如果有)。

我从网络服务接收的JSON

{
"words": [

{
"id": "1",
"the_word": "first word",
"pronunciation": "pronun",
"definition": "def",
"image_name": "img_01"
},
{
"id": "2",
"the_word": "second word",
"pronunciation": "pronun2",
"definition": "def2",
"image_name": "img_02"
},
{
"id": "3",
"the_word": "third word",
"pronunciation": "pronun3",
"definition": "def3",
"image_name": "img_03"
}

]
}

Word.java 类作为模型

public class Word {

private String the_word;
private String pronunciation;
private String definition;
private String image_name;


public String getThe_word() {
return the_word;
}

public void setThe_word(String the_word) {
this.the_word = the_word;
}

public String getPronunciation() {
return pronunciation;
}

public void setPronunciation(String pronunciation) {
this.pronunciation = pronunciation;
}

public String getDefinition() {
return definition;
}

public void setDefinition(String definition) {
this.definition = definition;
}

public String getImage_name() {
return image_name;
}

public void setImage_name(String image_name) {
this.image_name = image_name;
}


}

WordAdapter.java 类作为适配器(我稍后会处理图像)

public class WordAdapter extends BaseAdapter {

Context context;
LayoutInflater inflater;
List<Word> data;

public WordAdapter(Context context, List<Word> data) {
this.context = context;
this.data = data;
}




@Override
public int getCount() {
return data.size();
}

@Override
public Object getItem(int position) {
return null;
}

@Override
public long getItemId(int position) {
return 0;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

TextView word;

inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View itemView = inflater.inflate(R.layout.word_item, parent, false);

word = (TextView) itemView.findViewById(R.id.t_word);
word.setText(data.get(position).getThe_word());

return itemView;
}

}

这是列表 Activity 类

public class WordsList extends AppCompatActivity {


private static final String url = "http://mywebaddress.com/dictionary/showWords.php";

ListView listView;
WordAdapter adapter;
ProgressDialog PD;
List<Word> arrayListOfWords;
EditText editText;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_words_list);

listView = (ListView) findViewById(R.id.list);

editText = (EditText) findViewById(R.id.search);

PD = new ProgressDialog(this);
PD.setMessage("Loading ...");
PD.show();

getListView().setOnItemClickListener(new ListItemClickListener());

ReadDataFromDB();

editText.addTextChangedListener(filterTextWatcher);

}



private void ReadDataFromDB() {
PD.show();

JsonObjectRequest jsonObject = new JsonObjectRequest(Method.POST, url,new Response.Listener<JSONObject>() {

@Override
public void onResponse(JSONObject response) {

arrayListOfWords = new ArrayList<>();

try {
JSONArray ja = response.getJSONArray("words");
for (int i = 0; i < ja.length(); i++) {
JSONObject jsonWordsList = ja.getJSONObject(i);
Word word = new Word();
word.setThe_word(jsonWordsList.optString("the_word"));
word.setPronunciation(jsonWordsList.optString("pronunciation"));
word.setDefinition(jsonWordsList.optString("definition"));
word.setImage_name(jsonWordsList.optString("image_name"));

arrayListOfWords.add(word);

PD.dismiss();

}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "inside JSONException", Toast.LENGTH_LONG).show();
}

adapter = new WordAdapter(WordsList.this, arrayListOfWords);
editText.addTextChangedListener(filterTextWatcher);
listView.setAdapter(adapter);

adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
PD.dismiss();
}
});


AppController.getInstance().addToRequestQueue(jsonObject);

}

private TextWatcher filterTextWatcher = new TextWatcher() {

public void afterTextChanged(Editable s) {
}

public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}

public void onTextChanged(CharSequence s, int start, int before, int count) {
List<Word> foundItems = new ArrayList<>();
if (arrayListOfWords != null) {
getWordsList(s, foundItems);
listView.setAdapter(new WordAdapter(getApplicationContext(), foundItems));
adapter.notifyDataSetChanged();
}

}

};

public void getWordsList(CharSequence s, List<Word> list) {
for (Word word : arrayListOfWords) {
if (word.getThe_word().contains(s)) {
list.add(word);
}
}
}

public ListView getListView() {
return listView;
}



class ListItemClickListener implements ListView.OnItemClickListener {

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

Intent i = new Intent(WordsList.this,DetailsActivity.class);
i.putExtra("selected_word", (Parcelable) arrayListOfWords.get(position));
startActivity(i);

}

}
}

正如我所说,一切正常,但在列表项上单击!
实际上这一段代码就是问题所在:

class ListItemClickListener implements ListView.OnItemClickListener {

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

Intent i = new Intent(WordsList.this,DetailsActivity.class);
i.putExtra("selected_word", (Parcelable) arrayListOfWords.get(position));
startActivity(i);

}

}

如何将列表中所选项目的单词、发音、定义和图像名称传递到 DetailsActivity.java 以及如何将它们传递到那里?

谢谢!

<小时/>

编辑
所以我找到了一种非常简单的方法来传递数据,我一直使用它!!!
但是我不确定这是否是最佳实践,但我确信有更好的方法。考虑到您将要传递更多数据的时间,那么我的解决方案将会令人沮丧。
这就是我正在做的事情:
使用 putExtra 发送...

class ListItemClickListener implements ListView.OnItemClickListener {

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

String passingWord = String.valueOf(arrayListOfWords.get(position).getThe_word());
String passingPr = String.valueOf(arrayListOfWords.get(position).getPronunciation());
String passingDf = String.valueOf(arrayListOfWords.get(position).getDefinition());
String passingIm = String.valueOf(arrayListOfWords.get(position).getImage_name());

Intent i = new Intent(WordsList.this,DetailsActivity.class);
i.putExtra("selected_word", passingWord);
i.putExtra("selected_pr", passingPr);
i.putExtra("selected_df", passingDf);
i.putExtra("selected_im", passingIm);
startActivity(i);

}

}

并在 DetailsActivity.java 中获取 StringExtra :

Intent intent = getIntent();
String the_word = intent.getStringExtra("selected_word");
String pronunciation = intent.getStringExtra("selected_pr");
String definition = intent.getStringExtra("selected_df");
String image_name = intent.getStringExtra("selected_im");
<小时/>


编辑
上面的代码是可以的,直到您搜索一个单词并单击列表项,DetailsActivity 显示所选单词详细信息的不同内容。
实际上,当您搜索一个单词时,列表中可能会通过过滤字母显示两到三个项目。如果您选择第一个,在DetailsActivity中您将看到整个词典列表中第一个单词的详细信息。说明选择的位置错误。
所以我更改了代码并再次重写,这次它按我的预期工作。我将分享它作为这个问题的答案,对于谁来说可能会遇到这样的麻烦。

最佳答案

我认为实现在 Activity 之间发送复合对象的最佳方法是 EventBus 。您可以简单地创建一个要传递的新对象(它可以是任何类型,例如您的自定义类)并使用:

MyClass myObject = new MyClass; // initialize the object how you like
//and post it
eventBus.post(myObject);

然后在“接收者” Activity 中,创建一个 @Subscribe 方法,该方法将监听正在接收的此类对象:

@Subscribe
public void onMyClassEvent(MyClass myObject){
//your logic here
}

EventBus 工作应该不难,他们有很好的指南,但如果您遇到任何问题,请写下来。

关于java - 将数据从一个 Activity 传递到 ArrayList 项目单击上的另一个 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36203854/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com