fragment with custom arrayadapter and layout with multiple elements - android

My layout named main_layout has a RelativeLayout with at least two elements like Textview and Imageview inside it.
public class SigninFragment extends Fragment {
private List<Test> list= null;
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Test tes= new Test ();
tes.setId(1);
tes.setDesc("descabc");
list= new ArrayList<>();
list.add(prof);
tesListAdapter =
new TesListAdapter(
rootView.getContext()
,R.layout.list_row_adapter
,list);
autocompletetextview.setThreshold(3);
autocompletetextview.setAdapter(tesListAdapter );
My Adapter Class :
public class ProfissoesListAdapter extends ArrayAdapter<Test> {
private LayoutInflater inflater;
private int resource;
private Context context;
public TesListAdapter(Context activity, int resource, List<Test> listaProf) {
super(activity, resource, listaProf);
this.resource = resource;
this.context = context;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
convertView = inflater.inflate(R.layout.main_layout, parent, false);
holder = new ViewHolder();
holder.idProfissao = (TextView) convertView.findViewById(R.id.textViewId);
//holder.descProfissao = (TextView) convertView.findViewById(R.id.textDescProf);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Test item = getItem(position);
holder.idProfissao.setText(item.getId_prof()); **<=== BUG HERE**
return convertView;
}
Debugging adapter code I got in the line holder.idProfissao.setText(item.getId_prof()); Resource id cannot be found....

The method setText() of the TextView class it's overloaded to either use a String or an integer representing an id in the form of R.string.some_text. If item.getId_prof() doesn't return an id then you need to make it a String before setting it as text:
holder.idProfissao.setText("" + item.getId_prof());

Related

Unable to show Data into List from ListView Adapter Class in GoogleMap

ListView Adapter Class:
public class ListViewAdapter extends ArrayAdapter {
private Context context;
private int resource;
private ArrayList<HashMap<String,String>> list2 = new ArrayList<>();
public ListViewAdapter(Context context,int resource, List<HashMap<String, String>> list) {
super(context, resource, list);
this.resource = resource;
this.context = context;
this.list2 = (ArrayList<HashMap<String, String>>) list;
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
View row = convertView;
ViewHolder holder;
Object newMap = list2.get(position);
if (row == null){
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(resource, parent, false);
holder = new ViewHolder();
holder.name1 = (TextView) row.findViewById(R.id.name);
holder.add = (TextView) row.findViewById(R.id.address);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
String pname = ((HashMap<String, String>) newMap).get("place_name");
holder.name1.setText(pname);
return row;
}
public class ViewHolder {
TextView name1,add;
}
}
All data coming in ListView Adapter class. But when we try to show data into list view it's got a crash.
The Crash shown here holder.name1.setText(pname) in ListView Adapter class. And error show NullPointerException
add .setText() in if condition error occurred because you initialize Textview name1 in if condition,so check below code
#Override
public View getView(int position, View convertView, ViewGroup parent){
View row = convertView;
ViewHolder holder;
Object newMap = list2.get(position);
if (row == null){
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(resource, parent, false);
holder = new ViewHolder();
holder.name1 = (TextView) row.findViewById(R.id.name);
holder.add = (TextView) row.findViewById(R.id.address);
String pname = ((HashMap<String, String>) newMap).get("place_name");
holder.name1.setText(pname);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
return row;
}
As per my understanding, there is tow possible reason to show null pointer exception one is you may be getting null value in 'place_name' and another one is your referenced R.id.name is not available.
Edit your code like following,
String pname="";
pname = ((HashMap<String, String>) newMap).get("place_name");
if(holder.name1!=null){
holder.name1.setText(pname)
}
Finally resolve this problem, silly mistake I have does not add TextView in R.layout.activity_list_view.
There is no problem in your adapter except one error that is
add below code in your ListViewAdapter class
like this
#Override
public int getCount() {
return list2.size();
}
Hope this will work for you...

Custom ListView updating only last value android

It might be silly question, but I am having bit hard time to make it work.
I have a custom list view adapter which some textview and imageview.
But when I am updating listview using custom adapter it is only updating last value. here is my code
Adapter code
public class InfoListAdapter extends ArrayAdapter<aSProperty> {
Context mContext;
List<aSProperty> values= null;
public InfoListAdapter(Context mContext, List<aSProperty> data) {
super(mContext, R.layout.list_row, data);
this.mContext = mContext;
this.values = data.;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_row, null);
signal = (ImageView) convertView.findViewById(R.id.images);
type = (TextView) convertView.findViewById(R.id.type);
info = (TextView) convertView.findViewById(R.id.info);
image.setBackgroundResource(R.drawable.abc);
Log.d(TAG, "List Size"+values.size());
for(int i = 0; i < values.size(); i++){
Log.d(TAG, "DATA"+values.get(i).type);
type.setText(values.get(i).type);
}
}
ImageView image;
TextView type;
TextView info;
static String TAG= "InfoListAdapter";
return convertView;
}
}
Code to populate adapter
info_panel = new InfoListAdapter(esActivity.this, allData);
list.setAdapter(info_panel);
But I do not know why, it is only showing last values from values list. In addition I want to change image based on condition and data inside values, any good way to do this.
use following code
public class InfoListAdapter extends ArrayAdapter<aSProperty> {
Context mContext;
List<aSProperty> values= null;
public InfoListAdapter(Context mContext, List<aSProperty> data) {
super(mContext, R.layout.list_row, data);
this.mContext = mContext;
this.values = data.;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_row, null);
signal = (ImageView) convertView.findViewById(R.id.images);
type = (TextView) convertView.findViewById(R.id.type);
info = (TextView) convertView.findViewById(R.id.info);
image.setBackgroundResource(R.drawable.abc);
Log.d(TAG, "List Size"+values.size());
type.setText(values.get(position).type);
}
ImageView image;
TextView type;
TextView info;
static String TAG= "InfoListAdapter";
return convertView;
}
}
First of all, you have to override getCount() method of your adapter like this
#Override
public int getCount() {
return values .size();
}
and then in your getView() method you have to do something like this
#Override
public View getView(int arg0, View convertView, ViewGroup arg2) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = layoutInflater.inflate(R.layout.mycartslistcontents,
null);
holder.yourImageView= (ImageView) convertView
.findViewById(R.id.image);
holder.yourTextView= (TextView) convertView
.findViewById(R.id.textview);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
// here set your textView text and image resource
return convertView;
}
Don't forget to take a global variable layoutInflater at the top like this
private LayoutInflater layoutInflater;
and initialise it in your constructor this way
layoutInflater = LayoutInflater.from(yourContext);
and here is your ViewHolder class
static class ViewHolder {
ImageView yourImageView;
TextView yourTextView;
}

The get View function in my custom adapter class is not returning any value

The data source which i am passing to constructor of custom adapter class contains correct value. here is the code for calling custom adapter,
CAforSearchResults ca = new CAforSearchResults(this, user, s1);
lv.setAdapter(ca);
"user" contains correct value which i am passing to constructor.
here is my custom adapter class
public class CAforSearchResults extends ArrayAdapter {
Context ctx;
ArrayList<User_table> user;
SharedPreferences sf;
int userId;
public CAforSearchResults(Context ctx, ArrayList<User_table> user,
ArrayList<String> s) {
super(ctx, R.layout.searchresult, R.id.textView1, s);
this.ctx = ctx;
this.user = user;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.searchresult, parent, false);
}
ImageView iv1 = (ImageView) row.findViewById(R.id.imageView1);
TextView tv1 = (TextView) row.findViewById(R.id.textView1);
final int p = position;
if (position < user.size()) {
iv1.setImageBitmap(BitmapFactory.decodeFile(user.get(position).getDp()));
tv1.setText(user.get(position).getName());
tv1.setClickable(true);
tv1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
}
return row;
}
}
Now this list view lv is not showing the results.
You need to give the list of elements to display to the super constructor. Here, your ArrayList<String> s is maybe empty...

Custom ArrayAdapter NullpointerException

I got a problem with my Custom Array Adapter:
I have a NullpointerException in my getView() Method.
I already found out that the ArrayList seems not to be the problem because it works fine without the Adapter.
My Adapter
public class ArrayAdapterCusPostlist extends ArrayAdapter<Post> {
Context mContext;
ArrayList<Post> postListe;
public ArrayAdapterCusPostlist(Context mContext, int layoutResourceId, ArrayList<Post>pList) {
super(mContext, layoutResourceId, pList);
postListe = pList;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
convertView = inflater.inflate(R.layout.rowpost, parent, false);
}
Post curpost=postListe.get(position);
if(curpost!=null){
TextView textViewItem = (TextView) convertView.findViewById(R.id.nickname2);
textViewItem.setText(curpost.getName());
textViewItem.setTag(curpost.getName());
}
return convertView;
}
}
And: If the ArrayList is Empty because I did not called the add() Method there is no NullPointerException. Here is my Array List:
ArrayList<Post> PostList=new ArrayList<Post>();
PostList.add(new Post("Name", "Zeit", "Post", getResources().getDrawable(R.drawable.winter), getResources().getDrawable(R.drawable.winter)));
mPostList = (ListView) findViewById(R.id.postliste);
final ArrayAdapterCusPostlist adapter2 = new ArrayAdapterCusPostlist(this, R.layout.rowpost, PostList);
mPostList.setAdapter(adapter2);
Thanks!
Context mContext; is not initialized
Context mContext;
LayoutInflater inflater;
public ArrayAdapterCusPostlist(Context mContext, int layoutResourceId, ArrayList<Post>pList) {
super(mContext, layoutResourceId, pList);
this.mContext =mContext;
inflater = LayoutInflater.from(context); // initialize here. no need for context initialization if you have this here.
}
Also it is better you use a ViewHolder Pattern
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
convertView = inflater.inflate(R.layout.rowpost, parent, false);
holder = new ViewHolder();
holder.textViewItem = (TextView) convertView.findViewById(R.id.nickname2);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
Post curpost=postListe.get(position);
holder.textViewItem.setText(curpost.getName());
holder.textViewItem.setTag(curpost.getName());
}
return convertView;
static class ViewHolder
{
TextView textViewItem;
}
}

populating listview using arrayadapter

I want to display two strings one below other as if it is a list.
I tried using simple_list_item_1 to populate listview but nothing is visible on the screen.
((ListView)view).setAdapter(new ArrayAdapter<String>
(RealScreen.getAndroidBaseContext(),android.R.layout.simple_list_item_1, label));
where label is an array which contains two items to be displayed. All I can see is a white screen. Also I want this list to be unclickable.
I want the output to be like this:
String 1
String 2
I am not working with the Activity, instead with the View.
Here is the method which creates view
public void createView(JSONObject definition) {
// TODO Auto-generated method stub
super.createView(definition);
final JSONArray rowArray = definition.optJSONArray(KeyConstants.KEY_ROWS);
final JSONArray columnArray = definition.optJSONArray(KeyConstants.KEY_COLUMNS);
if (rowArray != null) {
label = new String[rowArray.length()];
for (int i = 0; i < rowArray.length(); i++) {
final JSONObject candidate = rowArray.optJSONObject(i);
if(candidate!=null){
label[i]=candidate.optString(KeyConstants.KEY_LABEL_TEXT, null);
}
}
}
((ListView)view).setAdapter(new ArrayAdapter<String>(RealScreen.getAndroidBaseContext(),android.R.layout.simple_list_item_1, label));
}
Thanks.
First create a custom Adapter Class as below
class MyAdapter extends ArrayAdapter
{
private Context context;
private ArrayList<String> list;
public MyAdapter(Context context,
ArrayList<String> list)
{
super(context, R.layout.layout_list);
this.context = context;
this.list = list;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.layout_list,
null);
convertView.setTag(holder);
holder.string1 = (TextView) convertView
.findViewById(R.id.stringgID1);
holder.string2 = (TextView) convertView
.findViewById(R.id.stringID2);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.string1 .setText(list.get(postion));
holder.string2 .setText(list.get(position));
return convertView;
}
#Override
public int getCount() {
return list.size();
}
class ViewHolder {
TextView string1 = null;
TextView string2 = null;
}
}
And then let your activity extend Activity and not ListActivity
listView.setAdapter(new MyAdapter(youtactivityame.this,label);
Doing this the label with any number of list items can be displayed.

Categories

Resources