Can somebody show me why ListView isn't showing? - java

The following project was copied verbatim from an online lesson. It is supposed to display a ListView but nothing shows up. I am brand new to Android Studio and this is only my second project in it. There are 5 files and I will display the contents of each of them below. I just need another pair of eyes or two to find the problem, because I'm "bug blind". Any help will be greatly appreciated.
activity_main.xml
<androidx.constraintlayout.widget.ConstraintLayout
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=".MainActivity">
<ListView
android:id="#+id/listView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.tadhg.uiuxadapterpractical;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
ListView lv;
Context context;
TypedArray images;
String [] titleList;
String [] descList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
titleList = getResources().getStringArray(R.array.Languages);
descList =
getResources().getStringArray(R.array.LanguageDesc);
images =
getResources().obtainTypedArray(R.array.LanguageImg);
lv = (ListView) findViewById(R.id.listView);
}
}
OurAdapter.java
package com.example.tadhg.uiuxadapterpractical;
import android.content.Context; import android.content.res.TypedArray;
import android.view.LayoutInflater; import android.view.View; import
android.view.ViewGroup; import android.widget.BaseAdapter; import
android.widget.ImageView; import android.widget.TextView;
public class OurAdapter extends BaseAdapter {
private String [] titles;
private String [] desc;
private TypedArray imageID;
private Context context;
private static LayoutInflater layoutInflater;
//constructor
OurAdapter(Context adapterContext, String[] titleList, String[] descList, TypedArray images){
titles = titleList;
desc = descList;
imageID = images;
context = adapterContext;
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return titles.length;
}
#Override
public Object getItem(int i) {
return i;
}
#Override
public long getItemId(int i) {
return i;
}
private class ViewHolder {
TextView tv1, tv2;
ImageView img;
}
#Override
public View getView(int position, View view, ViewGroup viewGroup) {
ViewHolder viewHolder = new ViewHolder();
if (view == null) {
view = layoutInflater.inflate(R.layout.list_layout, viewGroup, false);
viewHolder.tv1 = (TextView)view.findViewById(R.id.textView7);
viewHolder.tv2 = (TextView)view.findViewById(R.id.textView8);
viewHolder.img = (ImageView) view.findViewById(R.id.imageView2);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.tv1.setText(titles[position]);
viewHolder.tv2.setText(desc[position]);
viewHolder.img.setImageResource(imageID.getResourceId(position, 0));
return view;
} }
List_layout.xml
<androidx.constraintlayout.widget.ConstraintLayout
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">
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/cplus" />
<TextView
android:id="#+id/textView7"
android:layout_width="0dp"
android:layout_height="32dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="TextView"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/imageView2"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView8"
android:layout_width="0dp"
android:layout_height="24dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="TextView"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/imageView2"
app:layout_constraintTop_toBottomOf="#+id/textView7"
tools:text="TextView" />
</androidx.constraintlayout.widget.ConstraintLayout>
strings.xml
UIUXAdapterPractical
<string-array name="Languages">
<item>Java</item>
<item>Swift</item>
<item>C#</item>
<item>SQL</item>
<item>Javascript</item>
<item>Jquery</item>
<item>C++</item>
</string-array>
<string-array name="LanguageDesc">
<item>Desc. of Java</item>
<item>Desc. of Swift</item>
<item>Desc. of C#</item>
<item>Desc. of SQL</item>
<item>Desc. of Javascript</item>
<item>Desc. of Jquery</item>
<item>Desc. of C++</item>
</string-array>
<array name="LanguageImg">
<item>#drawable/java</item>
<item>#drawable/swift</item>
<item>#drawable/csharp</item>
<item>#drawable/sql</item>
<item>#drawable/js</item>
<item>#drawable/jquery</item>
<item>#drawable/cplus</item>
</array> </resources>

you have missed attaching your adapter to listview...you must set your adapter to your listview then only you will see your listview
change your code with following code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
titleList = getResources().getStringArray(R.array.Languages);
descList =
getResources().getStringArray(R.array.LanguageDesc);
images =
getResources().obtainTypedArray(R.array.LanguageImg);
final OurAdapter adapter = new OurAdapter(getApplicationContext(),titleList,descList,images);
lv = (ListView) findViewById(R.id.listView);
lv.setAdapter(adapter);
}
hope it will solve your problem :)

In your listview you have:
android:layout_width="0dp"
android:layout_height="0dp"
instead put
android:layout_width="match_parent"
android:layout_height="match_parent"
You're forcing the size to be 0 so you can't see it. Tell me if it works

Related

How do I fix my FirebaseRecyclerAdapter on a fragment?

I am working on this application and I'm trying to show my "expenses" database data in my personal fragment. I have created a recycler view file to replace the default layout. I am struggling to make this work. Can anyone help with the FirebaseRecycler Adapter and the holder class? I am not able to make this work, my fragment display the default layout with no information. I have tried and follow some codes but it is not working.
here is my personal fragment where the recycler view should appear
package uk.brighton.ama75.project;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.TextView;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import java.time.chrono.JapaneseDate;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
public class personalFragment extends Fragment {
private FirebaseAuth mAuth;
private String currentUserID;
private DatabaseReference databaseReference;
private RecyclerView recyclerView;
private Adapter adapter;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View myView = inflater.inflate(R.layout.fragment_personal, container, false);
mAuth = FirebaseAuth.getInstance();
currentUserID = mAuth.getCurrentUser().getUid();
databaseReference = FirebaseDatabase.getInstance().getReference().child("Expenses").child(currentUserID);
recyclerView = myView.findViewById(R.id.recycler_expense);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setStackFromEnd(true);
layoutManager.setReverseLayout(true);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(layoutManager);
return myView;
}
#Override
public void onStart() {
super.onStart();
Query query = FirebaseDatabase.getInstance()
.getReference()
.child("expenses")
.limitToLast(5);
FirebaseRecyclerOptions<PersonalExpenses> options =
new FirebaseRecyclerOptions.Builder<PersonalExpenses>()
.setQuery(query, PersonalExpenses.class)
.build();
FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<PersonalExpenses, MyViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull MyViewHolder holder, int position, #NonNull PersonalExpenses model) {
holder.setDate(model.getDate());
holder.setDescription(model.getDescription());
holder.setAmount(model.getAmount());
holder.setType(model.getType());
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.expense_recycler, parent, false);
return new MyViewHolder(view);
}
};
recyclerView.setAdapter(adapter);
}
#Override
public void onStop() {
super.onStop();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
private final View mView;
public MyViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
private void setDate(String date) {
TextView mDate = mView.findViewById(R.id.date_income);
mDate.setText(date);
}
private void setType(String type) {
TextView mType = mView.findViewById(R.id.type_txt);
mType.setText(type);
}
private void setDescription(String description) {
TextView mNote = mView.findViewById(R.id.note_txt);
mNote.setText(description);
}
private void setAmount(String amount) {
TextView mAmount = mView.findViewById(R.id.amount_income);
String stramount = String.valueOf(amount);
mAmount.setText(stramount);
}
}
}
This is the expenses recycler view file
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/expense_recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="5dp"
app:cardElevation="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<TextView
android:id="#+id/date_income"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="6 June 2019"
android:textAppearance="?android:textAppearanceSmall"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2">
<TextView
android:id="#+id/type_txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="1"
android:text="Type"
android:textAlignment="center"
android:textAppearance="?android:textAppearanceSmall"
android:textStyle="bold" />
<TextView
android:id="#+id/note_txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/type_txt"
android:text="#string/description"
android:textAlignment="center"
android:textAppearance="?android:textAppearanceSmall"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3
">
<TextView
android:id="#+id/amount_income"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="0000"
android:textAlignment="center"
android:textAppearance="?android:textAppearanceSmall"
android:textStyle="bold" />
</RelativeLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
This is the personal layout
<?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"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:background="#FFF1F3FC"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
app:cardElevation="5dp"
android:elevation="10dp"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:layout_margin="10dp"
android:layout_height="wrap_content"
tools:ignore="Suspicious0dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#android:color/black"
android:id="#+id/expenses"
android:text="expense"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_weight="3"
android:gravity="center"
android:layout_margin="10dp"
android:layout_height="wrap_content"
tools:ignore="Suspicious0dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/expense_txt"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#android:color/black"
android:text="000.00"/>
</RelativeLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:id="#+id/recycler_expense"
android:layout_height="match_parent"/>
</LinearLayout>
I believe you are missing these two main code.
Set adapter.startListening() inside onStart()
Set adapter.stopListening() inside onStop()
Make sure to put the code before the scope end of onStart() as you are performing a lot of operations and setting your adapter above.

Setting Up Horizontal RecyclerView

I want to add 10dp padding for my first tile in recycler View.
Just Like in the Book My show app the first App has 10dp padding (assume) then the padding between two tiles (suppose 5dp).
Similarly Last tile also have 10dp (assume) padding in the end
I'm adding my code of the recycler view If anyone wants to add something in it
RecyclerMetroAdapter.java
package com.example.android.indianmetro;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
public class RecyclerViewAdapter extends
RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private static final String TAG = "RecyclerViewAdapter";
private ArrayList<String> mPlaceNames = new ArrayList<>();
private ArrayList<String> mPlaceImageUrl = new ArrayList<>();
private ArrayList<String> mPlaceMetroDistance = new ArrayList<>();
private ArrayList<String> mClosestMetro = new ArrayList<>();
private Context mContext;
public RecyclerViewAdapter(Context context, ArrayList<String> placeNames, ArrayList<String> placeImageUrl,
ArrayList<String> placeMetroDistance, ArrayList<String> closestMetro ) {
mPlaceNames = placeNames;
mPlaceImageUrl = placeImageUrl;
mPlaceMetroDistance = placeMetroDistance;
mClosestMetro = closestMetro;
mContext = context;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.horizontal_item, viewGroup, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder viewHolder, int i) {
Log.d(TAG, "onCreateViewHolder: called");
Glide.with(mContext)
.asBitmap()
.load(mPlaceImageUrl.get(i))
.into(viewHolder.placeImage);
viewHolder.placeName.setText(mPlaceNames.get(i));
viewHolder.placeMetroDistance.setText(mPlaceMetroDistance.get(i));
viewHolder.closestMetro.setText(mClosestMetro.get(i));
}
#Override
public int getItemCount() {
if (mPlaceNames.size() < 8){
return mPlaceNames.size();
} else return 8;
}
public class ViewHolder extends RecyclerView.ViewHolder{
ImageView placeImage;
TextView placeName;
TextView placeMetroDistance;
TextView closestMetro;
public ViewHolder(#NonNull View itemView) {
super(itemView);
placeImage = (ImageView) itemView.findViewById(R.id.image);
placeName = itemView.findViewById(R.id.textView);
placeMetroDistance = itemView.findViewById(R.id.textView2);
closestMetro = itemView.findViewById(R.id.placeMetroName);
}
}
}
Code of tiles I'm using inside my RecyclerView
horizontal_item.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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="wrap_content"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:id="#+id/cardView2"
android:layout_width="120dp"
android:layout_height="140dp"
android:layout_marginStart="10dp"
android:layout_marginTop="8dp"
app:cardCornerRadius="10dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/delhi1" />
</android.support.v7.widget.CardView>
<TextView
android:id="#+id/textView"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="2dp"
android:ellipsize="end"
android:maxLines="1"
android:text="India Gate"
android:textColor="#color/darkHeading"
android:textSize="15sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/cardView2" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="36dp"
android:text="2.4 Km Away"
android:textColor="#color/blueAccent"
android:textSize="10sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/cardView2" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="24dp"
android:src="#drawable/ic_metro"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/cardView2" />
<TextView
android:id="#+id/placeMetroName"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:layout_marginStart="25dp"
android:layout_marginTop="20dp"
android:ellipsize="end"
android:maxLines="1"
android:text="Central Secretariat"
android:textColor="#color/subHeading"
android:textSize="12sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/cardView2" />
</android.support.constraint.ConstraintLayout>
Also if some one can suggest that how to make a tile fully visible from left and not allow any tile partially hidden from the left side
I guess you can try to set the padding in onBindViewHolder(),
for the first:
if (i == 0)
viewHolder.placeImage.setPadding(10, 5, 5, 5);
and for the last:
if (i == mPlaceImageUrl.size() - 1)
viewHolder.placeImage.setPadding(5, 5, 10, 5);
Change the numbers to what you like they are left->top->right->bottom padding.

How can I pass the data from one activity to another listview activity?

I need to pass the data from one activity to a ListView inside another activity. Currently I have set a single TextView to see all the details in second activity. But instead I need to view those items in ListView with adapter for each item.
MainActivity.java:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
public class MainActivity extends Activity implements AdapterView.OnItemSelectedListener {
String[] place={"Karur","Salem","Namakkal","Trichy","Madurai"};
EditText Name;
EditText Email;
EditText Number;
Button submit;
RadioGroup radioGroup;
RadioButton maleradioButton,femaleradioButton;
CheckBox BEcheckbox,MEcheckbox;
Spinner spin;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spin = (Spinner)findViewById(R.id.spinner);
spin.setOnItemSelectedListener(this);
ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_spinner_item, place);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(aa);
Name = (EditText)findViewById(R.id.Name);
Email = (EditText)findViewById(R.id.Email);
Number = (EditText)findViewById(R.id.Number);
submit = (Button)findViewById(R.id.submit);
radioGroup = (RadioGroup)findViewById(R.id.radioGroup);
//// maleradioButton = (RadioButton)findViewById(R.id.maleradioButton);
femaleradioButton = (RadioButton)findViewById(R.id.femaleradioButton);
BEcheckbox = (CheckBox)findViewById(R.id.BEcheckBox);
MEcheckbox = (CheckBox)findViewById(R.id.MEcheckbox);
final RadioGroup rgroup = (RadioGroup)findViewById(R.id.radioGroup);
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String spinText = spin.getSelectedItem().toString();
String name = Name.getText().toString();
String email = Email.getText().toString();
String number = Number.getText().toString();
String val="";
String radio;
if (BEcheckbox.isChecked()) {
val="BE";
} if(MEcheckbox.isChecked()) {
val="ME";
}
int select=rgroup.getCheckedRadioButtonId();
maleradioButton=(RadioButton)findViewById(select);
radio=maleradioButton.getText().toString();
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("Name",name);
intent.putExtra("Email",email);
intent.putExtra("Number",number);
intent.putExtra("Degree",val);
intent.putExtra("Gender",radio);
intent.putExtra("Place",spinText);
startActivity(intent);
}
});
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
SecondActivity.java:
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class SecondActivity extends Activity {
String Name;
String Email;
String Number;
String Degree;
String Gender;
String Place;
TextView result;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
result = (TextView)findViewById(R.id.result);
Name = getIntent().getExtras().getString("Name");
Email = getIntent().getExtras().getString("Email");
Number = getIntent().getExtras().getString("Number");
Degree = getIntent().getExtras().getString("Degree");
Gender = getIntent().getExtras().getString("Gender");
Place = getIntent().getExtras().getString("Place");
result.setText("Name:"+""+Name +'\n'+"Email"+" "+Email+'\n'+"Number:"+" "+Number+'\n'+"Degree:"+" "+Degree+'\n'+"Gender:"+" "+Gender+'\n'+"Place:"+" "+Place);
}
}
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="arun.com.project.MainActivity">
<EditText
android:id="#+id/Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignStart="#+id/Email"
android:ems="10"
android:hint="Enter your Name"
android:inputType="textPersonName" />
<EditText
android:id="#+id/Email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/Name"
android:layout_centerHorizontal="true"
android:ems="10"
android:hint="Enter your Email"
android:inputType="textEmailAddress" />
<EditText
android:id="#+id/Number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/Email"
android:layout_below="#+id/Email"
android:ems="10"
android:hint="Enter your Mobile Number"
android:inputType="phone" />
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/Number"
android:id="#+id/radioGroup">
<CheckBox
android:id="#+id/BEcheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignTop="#+id/radioGroup"
android:layout_marginTop="74dp"
android:layout_weight="1"
android:text="BE" />
<CheckBox
android:id="#+id/MEcheckbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="ME" />
<RadioButton
android:id="#+id/maleradioButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Male" />
<RadioButton
android:id="#+id/femaleradioButton"
android:layout_width="442dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Female" />
</RadioGroup>
<Button
android:id="#+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="11dp"
android:text="SUBMIT"
android:textColor="#ffffff"
android:background="#color/colorPrimaryDark"/>
<Spinner
android:id="#+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/submit"
android:layout_alignParentStart="true"
android:layout_marginBottom="27dp" />
</RelativeLayout>
activity_second.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="arun.com.project.SecondActivity">
<TextView
android:id="#+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="55dp"
android:layout_marginTop="43dp"
android:text="TextView"
android:textColor="#000000"
android:textSize="16sp"/>
</RelativeLayout>
I've just used single TextView to see the details whatever I have entered in MainActivity.java. But now I need to show the details in ListView with each details (name, email, number, etc...) in a separate adapter.
If you want to display many information inside a row of ListView. Create a custom Adapter for your ListView and custom POJO class to store multiple field of data in single object.
Create Info.java:
class Info {
String name;
String email;
public Info(String name, String email) {
this.name = name;
this.email = email;
}
public String getName() { return name; }
public String getEmail() { return email; }
}
Create row.xml to make custom layout for a single row of your ListView:
<?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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/name"
android:text="Name"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email"
android:id="#+id/email"/>
</LinearLayout>
Define an adapter to create rows and populate data on your rows. Create InfoAdapter.java:
class InfoAdapter extends BaseAdapter {
Context context;
List<Info> data;
private static LayoutInflater inflater = null;
public InfoAdapter(Context context, List<Info> data) {
// TODO Auto-generated constructor stub
this.context = context;
this.data = data;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return data.length;
}
#Override
public Object getItem(int position) {
return data.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (vi == null)
vi = inflater.inflate(R.layout.row, null);
TextView name = (TextView) vi.findViewById(R.id.name);
TextView email = (TextView) vi.findViewById(R.id.email);
name.setText(data.get(position).getName());
email.setText(data.get(position).getEmail());
return vi;
}
}
Put ListView inside your activity_second.xml and config the layout whatever you want.
<ListView
android:id="#+id/list"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</ListView>
Inside SecondActivity.java create a ListView instance and array of data:
listView = (ListView) findViewById(R.id.list);
//Getting extras (Name, Email,..)
listInfo.add(new Info(Name, Email,...); //Create Info object base on extras information
Init adapter and set:
InfoAdapter adapter = new InfoAdapter(this, listInfo);
listView.setAdapter(adapter);

Unable to click on custom listview, blocksDescendants not working

I'm fairly new to Android programming and tried everything I could I found on SO, but it still doesn't work.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ScrollView01"
android:layout_width="fill_parent"
android:fillViewport="true"
android:layout_height="fill_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.northcityproductions.androidiosapptransfertest1.MainActivity">
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="18dp"
android:headerDividersEnabled="false"
android:footerDividersEnabled="false" />
<ImageView
android:layout_width="48dp"
android:layout_height="48dp"
android:id="#+id/imageView1"
android:layout_gravity="center_vertical"
android:clickable="false"
android:padding="5dp" />
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:paddingStart="50dip"
android:textSize="20dip"
android:layout_gravity="center_vertical"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:clickable="false"
android:layout_alignParentStart="true">
</TextView>
</RelativeLayout>
AndroidListAdapter.java
package com.northcityproductions.androidiosapptransfertest1;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
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;
import java.util.List;
public class AndroidListAdapter extends ArrayAdapter {
List<String> androidListViewStrings = new ArrayList<String>();
List<Drawable> imagesId = new ArrayList<Drawable>();
Context context;
public AndroidListAdapter(Activity context, List<Drawable> imagesId, List<String> textListView) {
super(context, R.layout.activity_main, textListView);
this.androidListViewStrings = textListView;
this.imagesId = imagesId;
this.context = context;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View viewRow = layoutInflater.inflate(R.layout.activity_main, null,
true);
TextView mtextView = (TextView) viewRow.findViewById(R.id.textView1);
ImageView mimageView = (ImageView) viewRow.findViewById(R.id.imageView1);
mtextView.setText(androidListViewStrings.get(i));
mimageView.setImageDrawable(imagesId.get(i));
return viewRow;
}
}
Finally, MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//get list of apps
int flags = PackageManager.GET_META_DATA |
PackageManager.GET_SHARED_LIBRARY_FILES |
PackageManager.GET_UNINSTALLED_PACKAGES;
PackageManager pm = getPackageManager();
List<ApplicationInfo> applications = pm.getInstalledApplications(flags);
List<String> applicationsInstalled = new ArrayList<String>();
List<Drawable> applicationIcons = new ArrayList<Drawable>();
//Create map and sort alphabetically
Map applicationsList = new HashMap();
for (ApplicationInfo appInfo : applications) {
if ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 1) {
// System application
} else {
// Installed by user
String appName = (String) pm.getApplicationLabel((appInfo));
appName = appName.substring(0, 1).toUpperCase() + appName.substring(1);
applicationsList.put(appName, pm.getApplicationIcon(appInfo));
}
}
Map<String, Drawable> treeApps = new TreeMap<String, Drawable>(applicationsList);
for (Map.Entry<String, Drawable> appMap : treeApps.entrySet()) {
applicationsInstalled.add(appMap.getKey());
applicationIcons.add(appMap.getValue());
}
//Arrange them in listview
final AndroidListAdapter androidListAdapter = new AndroidListAdapter(this, applicationIcons, applicationsInstalled);
ListView lv1 = (ListView) findViewById(R.id.listView1);
lv1.setAdapter(androidListAdapter);
lv1.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
AlertDialog.Builder adb = new AlertDialog.Builder(MainActivity.this);
adb.setTitle("List");
adb.setMessage(" selected Item is="+parent.getItemAtPosition(position));
adb.setPositiveButton("Ok", null);
adb.show();
}
});
}
}
I have tried adding the blocksDescendants code in my activity_main.xml under Listview, and Relativelayout, but that doesn't seem to fix it. I also tried setting the focusable property to false for the imageview and textview but that didn't fix it either.

listview does not update with notifydatasetchanged() call

My Android listview does not update with notifydatasetchanged() call.
The Main Code Activity:
package com.example.jokesbook;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.jokesbook.MESSAGE";
CustomAdapter Adapter;
ListView lv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
JokeDB.jokesList = new ArrayList<Joke>();
JokeDB.jokesList.add(new Joke("DDD"));
lv = (ListView)findViewById(android.R.id.list);
Adapter= new CustomAdapter(MainActivity.this, R.layout.joke_list_item, JokeDB.jokesList);
lv.setAdapter(Adapter);
}//onCreate
#Override
public void onResume() {
super.onResume(); // Always call the superclass method first
Log.d("jokesbook", "onResume ");
Adapter.notifyDataSetChanged();
}
class CustomAdapter extends ArrayAdapter<Joke>{
Context context;
int layoutResourceId;
ArrayList<Joke> data = null;
private LayoutInflater mInflater;
public CustomAdapter(Context customAdapter, int layoutResourceId, ArrayList<Joke> data) {
super(customAdapter, layoutResourceId, data);
Log.d("jokesbook", "CustomAdapter ");
this.layoutResourceId = layoutResourceId;
this.context = customAdapter;
this.data = data;
this.mInflater = LayoutInflater.from(customAdapter);
}
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
//item_list
convertView = mInflater.inflate(R.layout.joke_list_item, null);
holder = new ViewHolder();
//fill the views
holder.joke = (TextView) convertView.findViewById(R.id.ListTextView1);
convertView.setTag(holder);
}
else {
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
holder = (ViewHolder) convertView.getTag();//
}
holder.joke.setText(data.get(position).jokeStr);
return convertView;
}
class ViewHolder {
TextView joke;
}
}
/** Called when the user clicks the Add a new joke button */
public void goNewJoke(View view) {
Intent intent = new Intent(this, New_joke.class);
startActivity(intent);
}
}
Its xml:
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:id="#+id/ButtonAboveList"
android:layout_width="500dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:onClick="goNewJoke"
android:text="#string/Add_a_new_joke"/>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
When adding content to JokeDB.jokesList in another activity that its code is:
package com.example.jokesbook;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class New_joke extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_joke);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.new_joke, menu);
return true;
}
public void addJoke(View view){
EditText editTextJoke= (EditText)findViewById(R.id.edit_text_jokeToAdd);
JokeDB.jokesList.add(new Joke(editTextJoke.getText().toString()));
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
}
and its XML is:
<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/green"
android:orientation="vertical"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:paddingTop="3dp"
tools:context=".New_joke" >
<EditText
android:id="#+id/edit_text_jokeToAdd"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_marginBottom="40dp"
android:background="#color/white"
android:hint="#string/edit_message" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="horizontal"
tools:context=".New_joke" >
<TextView
android:id="#+id/AuthorText"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="left"
android:text="#string/Author"
android:textColor="#android:color/white"
android:textSize="25sp" />
<EditText
android:id="#+id/edit_text_Author"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="#color/white"
android:hint="#string/Only_letters_allowed"
android:inputType="textPersonName" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="60dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="horizontal"
tools:context=".New_joke" >
<TextView
android:id="#+id/DateText"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="left"
android:text="#string/Date"
android:textColor="#android:color/white"
android:textSize="25sp" />
<EditText
android:id="#+id/edit_text_Date"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="#color/white"
android:hint="#string/Only_digits_allowed"
android:inputType="number" />
</LinearLayout>
<Button
android:id="#+id/ButtonAboveList"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="addJoke"
android:text="#string/Add" />
</LinearLayout>
I cannot see the updated list in the main activity eveb though in onResume I used notifydatasetchanged() function.
Try This....
#Override
public void onResume() {
super.onResume(); // Always call the superclass method first
Log.d("jokesbook", "onResume ");
notifyDataSetChanged();
}

Categories

Resources