I'm experiencing the problem "Unfortunately, your listview has stopped working" when i was using the android emulator to run my codes.
Please take a look at my codes thanks!
For activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.it3783.listview.MainActivity">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Choose a location..."/>
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"
android:id="#+id/spLocations"></ListView>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#+id/superHerosTV"
/>
</LinearLayout>
For MainActivity.java:
public class MainActivity extends AppCompatActivity {
ArrayAdapter<CharSequence>adapter;
TextView tvSelection;
ListView lvLocation;
String []strArray;
String []superHeros = {"1. Batman\n 2. SpiderMan", "1. Peter Pan\n2. Superman",
"1. IronMan\n2. Peter Pan", "1. SnowWhite \n2. Gingerbreadman"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_list_item_1, strArray);
tvSelection = (TextView) findViewById(R.id.superHerosTV);
lvLocation = (ListView) findViewById(R.id.spLocations);
Resources myRes = this.getResources();
strArray = myRes.getStringArray(R.array.locationArea);
lvLocation.setAdapter(adapter);
lvLocation.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String text = strArray[position];
tvSelection.setText(text);
}
});
adapter = ArrayAdapter.createFromResource(this, R.array.locationArea, android.R.layout.simple_list_item_1);
}
}
For strings.xml:
<resources>
<string name="app_name">ListView</string>
<string-array name="locationArea">
<item>North</item>
<item>South</item>
<item>East</item>
<item>West</item>
</string-array>
</resources>
adapter = new ArrayAdapter<CharSequence>(this,android.R.layout.simple_list_item_1, strArray);
This line in your onCreate methods takes the strArray, which is not initialized so its value is null.
Now when you set the adapter with null data to the listview, it crashes.
Related
I'm making simple listview of therapies and the list is not showing on the emulator. I've upgraded and downgraded my api from 22 to 30 and change relative layout to linear layout but still the list is not showing.
activty_stress.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="9"
android:background="#83BCD4"
tools:context=".stress">
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Find your relaxation"
android:textColor="#color/white"
android:textSize="18pt" />
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="70dp"/>
</RelativeLayout>
stress.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stress);
ListView listView = findViewById(R.id.listview);
List<String> list = new ArrayList<>();
list.add("Therapy1");
list.add("Therapy2");
list.add("Therapy3");
list.add("Therapy4");
list.add("Therapy5");
ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,list);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position==0){
//clicked therapy1
startActivity(new Intent(stress.this,Therapy1.class));
} else if (position==1){
//clicked therapy2
}else{
}
}
});
}
Heyy try replacing your RelativeLayout with LinearLayout, or remove weightSum because weightSum cannot be used with RelativeLayout
Replace with this code.
Note: Whenever you give textSize, give it in sp not in Pixel.
activty_stress.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#83BCD4"
android:orientation="vertical">
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Find your relaxation"
android:textColor="#color/white"
android:textSize="30sp" />
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="70dp" />
</RelativeLayout>
stress.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = findViewById(R.id.listview);
List<String> list = new ArrayList<>();
list.add("Therapy1");
list.add("Therapy2");
list.add("Therapy3");
list.add("Therapy4");
list.add("Therapy5");
ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position == 0) {
//clicked therapy1
// startActivity(new Intent(stress.this,Therapy1.class));
} else if (position == 1) {
//clicked therapy2
} else {
}
}
});
}
Output:
I am getting an error when trying to set my view to display the ListView for the file I want to display(text file). I am pretty sure it has something to do with the XML. I just want to display the information from this.file = fileop.ReadFileAsList("Installed_packages.txt");. My code:
public class Main extends Activity {
private TextView tv;
private FileOperations fileop;
private String[] file;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.fileop = new FileOperations();
this.file = fileop.ReadFileAsList("Installed_packages.txt");
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.TextView01);
ListView lv = new ListView(this);
lv.setTextFilterEnabled(true);
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, this.file));
lv.setOnItemClickListener(new AdapterView.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();
}
});
setContentView(lv);
}
}
list_item.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp"
android:textSize="16sp"
android:textColor="#000">
</LinearLayout>
main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<ScrollView
android:id="#+id/SCROLLER_ID"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:fillViewport="true">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5sp"
android:id="#+id/TextView01"
android:text="#string/hello"/>
</ScrollView>
</LinearLayout>
The ArrayAdapter requires the resource ID to be a TextView XML exception means you don't supply what the ArrayAdapter expects. When you use this constructor:
new ArrayAdapter<String>(this, R.layout.a_layout_file, this.file)
R.Layout.a_layout_file must be the id of a xml layout file containing only a TextView(the TextView can't be wrapped by another layout, like a LinearLayout, RelativeLayout etc!), something like this:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
// other attributes of the TextView
/>
If you want your list row layout to be something a little different then a simple TextView widget use this constructor:
new ArrayAdapter<String>(this, R.layout.a_layout_file,
R.id.the_id_of_a_textview_from_the_layout, this.file)
where you supply the id of a layout that can contain various views, but also must contain a TextView with and id(the third parameter) that you pass to your ArrayAdapter so it can know where to put the Strings in the row layout.
Soution is here
listitem.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textview"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</TextView>
</LinearLayout>
Java code :
String[] countryArray = {"India", "Pakistan", "USA", "UK"};
ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.listitem,R.id.textview, countryArray);
ListView listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(adapter);
If you are getting that message when you are extending an ArrayAdapter, you are getting that error because you have not provided the correct resource id to display the item. Call the super class in the constructor and pass in the resource id of the TextView:
//Pass in the resource id: R.id.text_view
SpinnerAdapter spinnerAddToListAdapter = new SpinnerAdapter(MyActivity.this,
R.id.text_view,
new ArrayList<>());
Adapter:
public class SpinnerAdapter extends ArrayAdapter<MyEntity> {
private Context context;
private List<MyEntity> values;
public SpinnerAdapter(Context context, int textViewResourceId,
List<MyEntity> values) {
//Pass in the resource id: R.id.text_view
super(context, textViewResourceId, values);
this.context = context;
this.values = values;
}
The problem is in the line:
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, this.file));
Because when i change it to:
lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, this.file));
It works!
I have a ListView of values and I want to make each value editable by pressing on it and opening a context menu. How can I add an EditText field to the context menu that appears when clicking on an item in a ListView? If you have a better idea for said issue, feel free to suggest it. Thanks in advance to anyone who answers!
You don't have to use a context menu for that. You can use AlertDialog.
Check out this answer here.
https://stackoverflow.com/a/45352961/8200290
Just create a different layout with an edit view.
Here is you solution.
You are going to have to edit things to make it fit into your application!
MainActivity class
public class MainActivity extends AppCompatActivity {
List<String> list = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list.add("Android");
list.add("iPhone");
list.add("Windows");
list.add("Blackberry");
list.add("Mac");
list.add("Laptop");
list.add("LCD");
list.add("Dell");
ArrayAdapter adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.list_view_item, list);
ListView listView = (ListView) findViewById(R.id.mobile_list);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "Clicked: " + list.get(position), Toast.LENGTH_SHORT).show();
AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
View alertView = getLayoutInflater().inflate(R.layout.custom_alert, null);
//Set the view
alert.setView(alertView);
//Show alert
final AlertDialog alertDialog = alert.show();
//Can not close the alert by touching outside.
alertDialog.setCancelable(false);
alertDialog.setCanceledOnTouchOutside(false);
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//Set the edit text for the item clicked
EditText editText = (EditText) alertView.findViewById(R.id.editText);
editText.setText(list.get(position));
ImageView closeButton = (ImageView) alertView.findViewById(R.id.closeButton);
closeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alertDialog.dismiss();
}
});
}
});
listView.setAdapter(adapter);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.vzw.www.listviewalert.MainActivity">
<ListView
android:id="#+id/mobile_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
list_view_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold" >
</TextView>
custom_alert.xml
-> This will show your EDIT text. The box at the bottom closes the alert
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/alertContainer"
android:background="#drawable/custom_alert_bg">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/rowOne">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editText"/>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>
<ImageView
android:id="#+id/closeButton"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_weight="0.33"
android:background="#cdcdcd" />
</RelativeLayout>
#drawable/custom_alert_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="#ffffff"/>
<corners
android:radius="5dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
You get the following:
I am taking data from my database using and want to set that as my list item. Upon clicking and holding an item, I want a dialog to open prompting the user to select one of the given options. My code is as follows:
My MainActivity.java class
public class MainActivity extends AppCompatActivity {
ListView todoList;
DbHelper dbHelper;
ArrayAdapter<String> myAdapter;
Button rename, delete;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHelper=new DbHelper(this);
todoList=(ListView)findViewById(R.id.to_do);
loadTaskList();
todoList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int i, long l) {
Dialog dialog=new Dialog(MainActivity.this);
dialog.setContentView(R.layout.item_dialog);
dialog.setTitle("Select");
rename = dialog.findViewById(R.id.rename_task);
delete = dialog.findViewById(R.id.delete_task);
dialog.show();
return false;
}
});
}
private void loadTaskList() {
ArrayList<String> taskList=dbHelper.getTask();
if(myAdapter==null){
myAdapter=new ArrayAdapter<String>(this,R.layout.todo_item,R.id.task_title,taskList);
todoList.setAdapter(myAdapter);
}else {
myAdapter.clear();
myAdapter.addAll(taskList);
myAdapter.notifyDataSetChanged();
}
}
My activity_main.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="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mylist.MainActivity">
<ListView
android:id="#+id/to_do"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"/>
</LinearLayout>
My Layout for the list item:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true">
<TextView
android:id="#+id/task_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Task"
android:textSize="20sp"
android:layout_centerVertical="true"
android:padding="5dp"/>
<ImageButton
android:id="#+id/task_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="#drawable/ic_delete_black_24dp" />
<ImageButton
android:id="#+id/task_done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_done_black_24dp"
android:layout_toLeftOf="#id/task_delete"/>
</RelativeLayout>
Try like this;
todoList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Dialog dialog=new Dialog(MainActivity.this);
dialog.setContentView(R.layout.item_dialog);
dialog.setTitle("Select");
rename = dialog.findViewById(R.id.rename_task);
delete = dialog.findViewById(R.id.delete_task);
dialog.show();
}
});
To detect that an item is clicked, you custom adapter must know which one so you should add this in your adapter class.
public ItemClicked getItem(int position){
return items.get(position);
}
I think returning true from your OnItemLongClick() method rather than false should work.
You are saying that you are trying to do onItemClickListener.
But you have written onItemLongClickListner.
Currently when running my application, going to this activity does not display my fragment. Am I adding the fragment wrong and so nothing in the fragment is displayed? Or is there something wrong the dynamic behavior in the fragment onCreateView method?
My Activity Code
public class AskQuestionActivity extends FragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ask_question);
}
}
My Activity XML
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="#string/header_text"/>
<fragment android:name="com.mypackage.AskQuestionFragment"
android:id="#+id/question_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
My Fragment Code
public class AskQuestionFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
String questionText = "What is your favorite color?";
List<String> answers = Arrays.asList("Red", "Blue", "Yellow");
LinearLayout layout = (LinearLayout)inflater.inflate(R.layout.question, container);
Resources res = getResources();
int id = res.getIdentifier("question_text", "id", layout.getContext().getPackageName());
TextView questionTextView = (TextView)layout.findViewById(id);
questionTextView.setText(questionText);
id = res.getIdentifier("question_answers", "id", layout.getContext().getPackageName());
RadioGroup radioGroup = (RadioGroup)layout.findViewById(id);
Collections.reverse(answers);
for (String s : answers) {
RadioButton button = new RadioButton(layout.getContext());
button.setText(s);
radioGroup.addView(button, 0);
}
return layout;
}
}
My Fragment XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/question_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/question_text"/>
<RadioGroup android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/question_answers"></RadioGroup>
<Button
android:id="#+id/submit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/submit_button_text"
/>
</LinearLayout>
try to put framelayout instead of fragment in xml. and in your activity's onCreate create the instance of your fragment you want to display and add it to the fragment container
public class AskQuestionActivity extends FragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ask_question);
AskQuestionFragment fragment = new AskQuestionFragment();
getSupportFragmentManager().beginTransaction()
.add(R.id.id_of_your_container, fragment).commit();
}
}
Your problem might be when doing this:
LinearLayout layout = (LinearLayout)inflater.inflate(R.layout.question, container);
Consider to replace that line with one of the following:
LinearLayout layout = (LinearLayout)inflater.inflate(R.layout.question, null);
or
LinearLayout layout = (LinearLayout)inflater.inflate(R.layout.question, container, false);
change your layout from linear to be framelayout, and attach your fragment in activity .
getSupportFragmentManager().beginTransaction()
.add(R.id.your_fragment_container, fragment).commit();