So basically I want this list view to be shuffled so it is always random. But each one needs to open up its own activity and be constant no matter where it is in the list view (so I'm pretty sure I can't use position.) So I tried using the (TextView) view).getText() and it isn't reading it as it does in the Toast. Any ideas?
public class Bands extends ListActivity{
int numBands;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final ArrayList<String> bands = new ArrayList<String>();
bands.add(new String("Band0"));
bands.add(new String("Band1"));
bands.add(new String("Band2"));
Collections.shuffle(bands);
setListAdapter(new ArrayAdapter<String>(this, R.layout.item_list, bands));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
if( ((TextView) view).getText() == "Band0"){
Intent i = new Intent(Bands.this, Audio.class);
i.putExtra("Band", 0);
startActivity(i);
}
}
});
}
}
Anyone know a way to read the number order the shuffle creates? Cause that could be a way if someone knows how. Thanks and any help is appreciated.
How about making this change:
String text = ((TextView) view).getText();
if (text.equals("Band0") {
...
If i got your question right, you need everytime the listview to be randomly populated. In your getView() where you set your strings from the list add this
Outside of getView
int remember = 0;
int[] rememberArray = new int[arraylist.size()-1]; //to store indexes that already has been drawn
Inside the getView():
Random gen= new Random()
int randomPosition = gen.nextInt(arraylist.size()-1);
rememberArray[remember]=randomPosition;
remember++;
for(int i =0; i <= rememberArray.length;i++){
if(!remember==rememberArray[i])
{
arraylist.get(randomPosition);
} else { //generate again, probably you can create method for this, and apply recursion
}
} //proceed with the rest of the getView()
This is not tested, I'm just giving you the idea.
Related
I'm trying to display a listview conatining clothes items ..the listview will appear after the button "enter" under edittext will be clicked ..thelistitems will be different depending on what is the string(colors) on edittext (PS: the string in Listview are name and refrence of clothes!)..
for exemple:
if user choses in edittext: "Color1, Color2, "
a list will appear with a name and refrence of clothes that contain those colors: "Item1, Item2"
Because Item1 and Item2 are the only ones to contain the color1 and color2!
The list of possible Items(clothes) are 100, and Possible input in edit text are 6 colors (Color1, Color2, Color3, Color4, Color5, Color6) ..every time the user choses a set of color, the list will be displayed with the possible Items than contain those colors! ( as explained here: https://i.imgur.com/XwHTmGY.png)
PS: i've already created a custom edittext and use the adapter with the different strings(Color1..Color6) and i know how to get the string from the edittext ..but how to create update the listview with different items w depending on the string(chosen colors) of the edittext is the priblem! Thanks!
How to that? i've searched on internet on similar exemples with no luck..
This is my code so far:
MainActivity.java
public class DecisionTree extends AppCompatActivity {
private static final String TAG = DecisionTree.class.getSimpleName();
TextView txt,txt2;
com.mycardboarddreams.autocompletebubbletext.MultiSelectEditText editText;
String data;
private ListView listView;
private ItemAdapter itemListAdapter;
private List<Item> itemList = new ArrayList<Item>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_decision_tree);
/////////////////////////////////////////////////
listView = (ListView) findViewById(R.id.diseases_list_container);
itemListAdapter = new ItemAdapter(itemList, this);
listView.setAdapter(itemListAdapter);
listView.setFastScrollEnabled(true);
////////////////////////////////////////////////
// add some items
itemList.add(new Item("Jeans Refnum 2520"));
itemList.add(new Item("T shirt Refnum 1220"));
.
.
.
.
.
A long list of 100 items
//add new items and changes to adapter
itemListAdapter.notifyDataSetChanged();
////////////////////////////////////////////////
//Text
txt = (TextView) findViewById(R.id.textView);
txt2 = (TextView) findViewById(R.id.textView2);
txt.setText("Given a set of clinical features, this tool should provide you with a reasonable and relevant differential diagnsosis (not the definitive diagnosis!)");
txt2.setText("Remember, this tool adds to your diagnostic skills and serves as an educational aid. It is not meant to replace your clinical judgement.");
//the edittext
editText = (com.mycardboarddreams.autocompletebubbletext.MultiSelectEditText) findViewById(R.id.auto_text_complete);
//Add some sample items
List<SampleItem> sampleItems = Arrays.asList(
new SampleItem("Blue"),
new SampleItem("Red"),
new SampleItem("Orange"),
new SampleItem("Yellow"),
new SampleItem("vert"),
new SampleItem("rouge")
);
editText.addAllItems(sampleItems);
//Get the ListView associated with the MultiSelectEditText
ListView list = editText.getListView();
//Add it to a FrameLayout somewhere in the activity
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
list.setLayoutParams(params);
final FrameLayout frame = (FrameLayout) findViewById(R.id.auto_list_container);
frame.addView(list);
// The output
//frameLayout = (FrameLayout) findViewById(R.id.auto_list_container2);
//Set a listener on bubble clicks
//editText.setBubbleClickListener(new com.mycardboarddreams.autocompletebubbletext.MultiSelectEditText.BubbleClickListener<SampleItem>() {
/* #Override
public void onClick(SampleItem item) {
//Log.d(TAG, "Item: " + item.getReadableName());
Toast.makeText(DecisionTree.this, item.getReadableName(),
Toast.LENGTH_LONG).show();
//displayListPatho();
}
});*/
//-----------------------------------------END ONCREATE
}
public void blahblah (View view) {
data= editText.getText().toString();
listView.setVisibility(View.VISIBLE);
Toast.makeText(DecisionTree.this, data,
Toast.LENGTH_LONG).show();
}
I acheived what i want with making this code:
1/ created a list with adapter
2/ on each click the items on adapter are removed( clearAdapter();) and new items are added (itemList.add(new Item("Jenny"));) depending on string from edit text( if (data.equals("Epistaxis, ")). and after the adapter is notified with itemListAdapter.notifyDataSetChanged();
Ps: blahblah (=Button OnClick) ..
public void blahblah (View view) {
data= editText.getText().toString();
if (data.equals("Epistaxis, ")){
clearAdapter();
itemList.add(new Item("Jenny"));
itemList.add(new Item("Bladna"));
itemList.add(new Item("Drafat"));
itemListAdapter.notifyDataSetChanged();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
if (position == 0) {
Intent i=new Intent(DecisionTree.this, Figures.class);
startActivity(i);
} else if (position == 1) {
//code specific to 2nd list item
Toast.makeText(getApplicationContext(), "Place Your Second Option Code", Toast.LENGTH_SHORT).show();
} else if (position == 2) {
Toast.makeText(getApplicationContext(), "Place Your Third Option Code", Toast.LENGTH_SHORT).show();
}
}
}); }
else if (data.equals("Nasal Discharge, ")) {
clearAdapter();
itemList.add(new Item("Jenny2"));
itemList.add(new Item("Bladna2"));
itemList.add(new Item("Drafat2"));
itemListAdapter.notifyDataSetChanged();
}
Very long question, but it's not clear exactly what do you want?
If I understand right, you should do somthing like that:
Get your String string from EditText.
String[] items = string.split(" ");
ListView listView = (ListView)findViewById(R.id.listView);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, items);
listView.setAdapter(adapter);
What a problem with that?
I'm trying to get a Toast to display a message based on a string in an array. I've got two arrays and a listview. When an item in the listview gets selected (which is populated by strings from my first array), I want a toast to come up displaying a message from the second array. The string in position 0 in the first array should correspond to the string in position 0 in the second array.
public class listView_Quiz extends AppCompatActivity {
ListView listView;
ArrayAdapter<String> adapterList;
String [] quizQuestions;
String [] quizAnswers;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview_quiz);
listView = (ListView) findViewById(R.id.quizList);
quizQuestions = getResources().getStringArray(R.array.quizQuestions);
quizAnswers = getResources().getStringArray(R.array.quizToast);
adapterList = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, quizQuestions) {
listView.setAdapter(adapterList);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {}
I can get it to half work when I use toString.contains and then write in an answer myself. But I'm looking for a solution where the array position would be checked in each array, and if they match, print the second array message (the answer message)
if (parent.getItemAtPosition(position).toString().contains("Q1")) {
Toast.makeText(getApplicationContext(), "Answer1", Toast.LENGTH_LONG).show();
}
else if (parent.getItemAtPosition(position).toString().contains("Q2")) {
Toast.makeText(getApplicationContext(), "Answer2", Toast.LENGTH_LONG).show();
}
As a test, I also also tried some iterations of the below. However, the toast never gets displayed
String i = quizAnswers[0];
String j = quizQuestions[0];
if (parent.getItemAtPosition(position).toString().equals(j)) {
Toast.makeText(getApplicationContext(), i, Toast.LENGTH_LONG).show();
}
Pretty much, what I'm trying to do is
if (selectedListViewitem(position) == Array2(position)) {Toast message}
Is this possible to do? Or will I have to use toString.contains(String)? Thanks
try
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), quizAnswers[position], Toast.LENGTH_LONG).show();
}
onitemclicks already gives you the position
hope it helps.
onItemClick method already contains position parameter. You can just take a String from the 2nd array by position and show it with a Toast:
Toast.makeText(listView_Quiz.this, quizAnswers[position], Toast.LENGTH_LONG).show();
My app is passing text taken from TextView in list item. I know that text in this text views is right cause I can see that it's ok. Text taken from TextView is pid of photo in MySql database.
My onItemClick code :
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (((ImageView) findViewById(R.id.reviewImageView)).getBackground() != null) {
String pid = ((TextView) findViewById(R.id.reviewListPid)).getText().toString();
Intent i = new Intent(getApplicationContext(), photoActivity.class);
i.putExtra("pid", pid);
i.putExtra("database", database_name);
startActivity(i);
}
}
});
I'm sure that text in every list item is right and I can see that text passed to next activity is wrong, cause I can see variable value in log and it's wrong.
Two things:
This won't fix your issue but instead of using getApplicationContext() in your Intent intialization use <YourActivityName>.this instead.
The way you are getting your String is a little unusual, and unnecessary. Most likely, you are loading your adapter for your listview with some sort of array that contains the "pid", correct? So then just get the value from that array in your onItemClickListener callback.
It would look like this:
String[] items = {"..."};
ArrayAdapter<String> itemsAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
listView.setAdapter(itemsAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (((ImageView) findViewById(R.id.reviewImageView)).getBackground() != null) {
String pid = items[position];
Intent i = new Intent(MyActivity.this, photoActivity.class);
i.putExtra("pid", pid);
i.putExtra("database", database_name);
startActivity(i);
}
}
});
So, this might be a simple question, or I may be doing things totally off here, but here's what I have:
public class SetPrefsActivity extends ListActivity{
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.radiolist);
ArrayList<Hall> listItems = new ArrayList<Hall>();
ArrayAdapter<Hall> ar = new ArrayAdapter<Hall>(this, android.R.layout.simple_list_item_single_choice, listItems);
setListAdapter(ar);
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener()
{
boolean somethingChecked = false;
int lastChecked;
public void onItemClick(AdapterView arg0, View arg1, int arg2,
long arg3) {
if(somethingChecked){
ListView lv = (ListView) arg0;
TextView tv = (TextView) lv.getChildAt(lastChecked);
CheckedTextView cv = (CheckedTextView) tv;
cv.setChecked(false);
}
ListView lv = (ListView) arg0;
TextView tv = (TextView) lv.getChildAt(arg2);
CheckedTextView cv = (CheckedTextView) tv;
if(!cv.isChecked())
cv.setChecked(true);
lastChecked = arg2;
somethingChecked=true;
}
});
new LoadListTask().execute(); //This loads Items into the List
Button b = (Button) findViewById(R.id.savePrefBtn);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//IF SOMETHING IS SELECTED,
//THEN UPDATE SHARED PREFERENCES
Intent i = new Intent(SetPrefsActivity.this, SomeOtherActivity.class);
startActivity(i);
}
}
});
}
//other stuff to fill the arrayAdapter
}
What I want to do, is:
When someone clicks the button, it gets information from the listview and updates a shared preference according to the radio option that's selected.
What I'm having trouble with is getting the index of the currently selected item. What is the best way to retrieve that information? Am I implementing the single choice list completely wrong?
Thank You!
arg2 is the position in your list.
It looks like you are doing some extra (unnecessary) processing. arg1 is your row view, and since the android.R.layout.simple_list_item_single_choice layout contains only a CheckedTextView, you can use that directly without having to look for it.
CheckedTextView cv = (CheckedTextView) arg1;
I have created a list. And I need to get the text on the list item, when it is clicked. Then that text need to be set in a TextView. Following is my code and i get a force stop when I run it. Please give some ideas.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtTask = (EditText)findViewById(R.id.txtTask);
btnAdd = (Button)findViewById(R.id.btnAddTask);
selectedTask = (TextView)findViewById(R.id.textViewTask);
list = getListView();
list.setTextFilterEnabled(true);
btnAdd.setOnClickListener(this);
list.setOnKeyListener(this);
toDoItems = new ArrayList<String>();
oo = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, toDoItems);
list.setAdapter(oo);
list.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id3) {
int tmp = list.getSelectedItemPosition();
String v= toDoItems.get(tmp).toString();
selectedTask.setTag(v);
flippy.showNext();
}
});
}
Replace below 3 lines of your code in onItemClick method with my suggested code.
int tmp = list.getSelectedItemPosition();
String v= toDoItems.get(tmp).toString();
selectedTask.setTag(v);
Suggested Code
String v= toDoItems[position]; // or
String v = list.getItemAtPosition(position).toString();
selectedTask.setText(v);
After you have got the string v, you need to put the following line :
selectedTask.setText(v);
Also there is no need to put list.setOnKeyListener(this); since you need to listen for the item being clicked.