I need to show call log in ListView on Fragment
setListAdapter(new MyAdapter(this, android.R.layout.simple_list_item_1,
R.id.tvNameMain, conNames));
return rootView;
And All Code
private ListView mListView;
private ArrayList<String> conNames;
private ArrayList<String> conNumbers;
private ArrayList<String> conTime;
private ArrayList<String> conDate;
private ArrayList<String> conType;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_recents, container, false);
mListView = (ListView)rootView.findViewById(R.id.list);
conNames = new ArrayList<String>();
conNumbers = new ArrayList<String>();
conTime = new ArrayList<String>();
conDate = new ArrayList<String>();
conType = new ArrayList<String>();
Cursor curLog = getAllCallLogs(getActivity().getContentResolver());
setCallLogs(curLog);
setListAdapter(new MyAdapter(this, android.R.layout.simple_list_item_1,
R.id.tvNameMain, conNames));
return rootView;
}
Class MyAdapter
private class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(Context context, int resource, int textViewResourceId,
ArrayList<String> conNames) {
super(context, resource, textViewResourceId, conNames);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = setList(position, parent);
return row;
}
private View setList(int position, ViewGroup parent) {
LayoutInflater inf = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inf.inflate(R.layout.liststyle, parent, false);
TextView tvName = (TextView) row.findViewById(R.id.tvNameMain);
TextView tvNumber = (TextView) row.findViewById(R.id.tvNumberMain);
TextView tvTime = (TextView) row.findViewById(R.id.tvTime);
TextView tvDate = (TextView) row.findViewById(R.id.tvDate);
TextView tvType = (TextView) row.findViewById(R.id.tvType);
tvName.setText(conNames.get(position));
tvNumber.setText(conNumbers.get(position));
tvTime.setText("( " + conTime.get(position) + "sec )");
tvDate.setText(conDate.get(position));
tvType.setText("( " + conType.get(position) + " )");
return row;
}
}
Class CallLog
private void setCallLogs(Cursor curLog) {
while (curLog.moveToNext()) {
String callNumber = curLog.getString(curLog
.getColumnIndex(android.provider.CallLog.Calls.NUMBER));
conNumbers.add(callNumber);
String callName = curLog
.getString(curLog
.getColumnIndex(android.provider.CallLog.Calls.CACHED_NAME));
if (callName == null) {
conNames.add("Unknown");
} else
conNames.add(callName);
String callDate = curLog.getString(curLog
.getColumnIndex(android.provider.CallLog.Calls.DATE));
SimpleDateFormat formatter = new SimpleDateFormat(
"dd-MMM-yyyy HH:mm");
String dateString = formatter.format(new Date(Long
.parseLong(callDate)));
conDate.add(dateString);
String callType = curLog.getString(curLog
.getColumnIndex(android.provider.CallLog.Calls.TYPE));
if (callType.equals("1")) {
conType.add("Incoming");
} else
conType.add("Outgoing");
String duration = curLog.getString(curLog
.getColumnIndex(android.provider.CallLog.Calls.DURATION));
conTime.add(duration);
}
}
Class Cursor
public static Cursor getAllCallLogs(ContentResolver cr) {
// reading all data in descending order according to DATE
String strOrder = android.provider.CallLog.Calls.DATE + " DESC";
Uri callUri = Uri.parse("content://call_log/calls");
Cursor curCallLogs = cr.query(callUri, null, null, null, strOrder);
return curCallLogs;
}
If you want to use setListAdapter your Fragment should extend ListFragment. but in your case changing it to mListView.setAdapter([...]) should do the trick
Change
setListAdapter(new MyAdapter(this, android.R.layout.simple_list_item_1, R.id.tvNameMain, conNames));
To
mListView.setAdapter(new MyAdapter(this, android.R.layout.simple_list_item_1, R.id.tvNameMain, conNames));
Related
Im absolutely new in android studio, and Im trying to create custom listview, the problem is that it creating wrong list, as
Name
Name
------
Surname
Surname
-----
... and repeating
what I want to do is:
Name
Surname
-------
My code is:
protected void onResume() {
DBHelper db = new DBHelper(this);
ArrayList<String> names = new ArrayList<String>();
for (int i = 0; i < db.getAllContacts().size(); i++) {
names.add(db.getAllContacts().get(i).getName());
names.add(db.getAllContacts().get(i).getEmail());
}
ArrayAdapter adapter = new custom_row(this, names);
listView_allContacts.setAdapter(adapter);
super.onResume();
}
What is wrong here? Thanks in advance!
My custom_row code:
public class custom_row extends ArrayAdapter {
public custom_row(Context context, ArrayList<String> names) {
super(context, R.layout.custom_cell, names);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater peoInf = LayoutInflater.from(getContext());
View customView = peoInf.inflate(R.layout.custom_cell, parent , false);
String singlePeople = getItem(position);
TextView name_text = (TextView) customView.findViewById(R.id.name_text);
TextView email_text = (TextView) customView.findViewById(R.id.email_text);
name_text.setText(singlePeople);
email_text.setText(singlePeople);
return customView;
}
}
You need two different ArrayList one for the Names and one for the email.
try this
ArrayList<String> list;
public custom_row(Context context, ArrayList<String> names, ArrayList<String> email) {
super(context, R.layout.custom_cell, names);
list = email;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater peoInf = LayoutInflater.from(getContext());
View customView = peoInf.inflate(R.layout.custom_cell, parent , false);
String singlePeople = getItem(position);
String email = list.get(position);
TextView name_text = (TextView) customView.findViewById(R.id.name_text);
TextView email_text = (TextView) customView.findViewById(R.id.email_text);
name_text.setText(singlePeople);
email_text.setText(email);
return customView;
}
I know to get the ListItem from:
string[] = {"data1","data","data3"};
But how do I get it from a JSON response? I moved my data to:
ArrayList<HashMap<String, String>>
My code is below, I have to use JSON instead of getting a String[] because my JSON response contains an id, a title and an image link.
public class MainActivity extends AppCompatActivity {
String[] titles,header;
ListView list1;
int[] img;
String[] title;
int[] img = {R.drawable.img1, R.drawable.img2,R.drawable.img3};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list1 = (ListView) findViewById(R.id.list);
title ={"data1","data2","data3"}
myAdapter adapter = new myAdapter(getApplicationContext(), titles, img);
list1.setAdapter(adapter);
}
class myAdapter extends ArrayAdapter<String> {
Context context;
int[] imgs;
String[] titles;
private ArrayList<String> mData = new ArrayList<>();
private ArrayList<Integer> mImage = new ArrayList<>();
private TreeSet<Integer> sectionHeader = new TreeSet<Integer>();
private LayoutInflater mInflater;
myAdapter(Context context, String[] titles, int imgs[]) {
super(context, R.layout.list_item, R.id.text, titles);
this.context = context;
this.imgs = imgs;
this.titles = titles;
mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
int rowType = getItemViewType(position);
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.list_item, parent, false);
holder.myImage = (ImageView) convertView.findViewById(R.id.image);
holder.myText = (TextView) convertView.findViewById(R.id.texttitle);
Picasso.with(context)
.load(imgs[position])
.fit()
.into(holder.myImage);
holder.myText.setText(titles[position]);
convertView.setTag(holder.myImage);
return convertView;
}
public class ViewHolder {
public TextView myText;
public ImageView myImage;
}
}
}
First change your above code by below so that you can see the list of image and string properly:
public class MainActivity extends AppCompatActivity {
ListView list1;
String[] title = {"data1", "data2", "data3"};
;
int[] img = {R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list1 = (ListView) findViewById(R.id.list);
myAdapter adapter = new myAdapter(getApplicationContext(), title, img);
list1.setAdapter(adapter);
}
}
class myAdapter extends ArrayAdapter<String> {
Context context;
int[] imgs;
String[] titles;
private ArrayList<String> mData = new ArrayList<>();
private ArrayList<Integer> mImage = new ArrayList<>();
private TreeSet<Integer> sectionHeader = new TreeSet<Integer>();
private LayoutInflater mInflater;
myAdapter(Context context, String[] titles, int imgs[]) {
super(context, R.layout.list_item, R.id.text, titles);
this.context = context;
this.imgs = imgs;
this.titles = titles;
mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
int rowType = getItemViewType(position);
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.list_item, parent, false);
holder.myImage = (ImageView) convertView.findViewById(R.id.image);
holder.myText = (TextView) convertView.findViewById(R.id.texttitle);
holder.myImage.setImageResource(imgs[position]);
// Picasso.with(context)
// .load(imgs[position])
// .fit()
// .into(holder.myImage);
holder.myText.setText(titles[position]);
convertView.setTag(holder.myImage);
}
return convertView;
}
public class ViewHolder {
public TextView myText;
public ImageView myImage;
}
}
i have two problems
1. spinner cannot be selected.
2. getting the spinner item if selected
i tried lots of things but dunno really what's with it if someone would help me i'd be really thankful
here's the code:
`
public class MobileArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final List values;
public MobileArrayAdapter(Context context, List values) {
super(context, R.layout.req_row, values);
this.context = context;
this.values = values;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.req_row, parent, false);
TextView req_date = (TextView) rowView.findViewById(R.id.req_date);
TextView request_time = (TextView) rowView.findViewById(R.id.request_time);
TextView task = (TextView) rowView.findViewById(R.id.tsk);
TextView issue = (TextView) rowView.findViewById(R.id.issue);
TextView employee_name = (TextView) rowView.findViewById(R.id.emp_name);
TextView contactno = (TextView) rowView.findViewById(R.id.contact);
TextView department = (TextView) rowView.findViewById(R.id.dep);
TextView comment = (TextView) rowView.findViewById(R.id.comment);
// final Spinner empSpinner = (Spinner) rowView.findViewById(R.id.spinner);
HashMap<String, String> m = new HashMap<String, String>();
m = (HashMap) values.get(position);
req_date.setText(m.get("req_date"));
request_time.setText(m.get("req_time"));
task.setText("TSK/"+m.get("id"));
issue.setText(m.get("Issue"));
employee_name.setText(m.get("Emp_Name"));
contactno.setText(m.get("Contact_No"));
department.setText(m.get("Department"));
comment.setText(m.get("Comment"));
final Spinner empSpinner = (Spinner) rowView.findViewById(R.id.spinner);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(context,
R.layout.support_simple_spinner_dropdown_item, specialistsList);
dataAdapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
empSpinner.setAdapter(dataAdapter);
empSpinner.setSelection(position);
empSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
sp = empSpinner.getSelectedItem().toString();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
Button send = (Button) rowView.findViewById(R.id.snd_tsk);
send.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new Send().execute();
}
});
return rowView;
}
}
`
I have
public class MainActivity extends FragmentActivity
I wrote Loader class which implements AsyncResponse:
public class Loader implements AsyncResponse {
public Activity contextActivity;
Loader(Activity context) {
this.contextActivity = context;
}
Class "Loader" has method "DrawTabInfo" which calling from AsyncResponse callback.
public void drawTabInfo(String jsonBlob, int tabId)
{
JSONObject dataJsonObj;
PullToRefreshListView list = null;
try {
dataJsonObj = new JSONObject(jsonBlob);
JSONArray jsonData = dataJsonObj.getJSONArray("steals");
ArrayList<JSONObject> data = new ArrayList<JSONObject>();
for (int i=0;i<jsonData.length();i++){
data.add(jsonData.getJSONObject(i));
}
Adapter adapter = new Adapter(contextActivity, data);
LayoutInflater inflater = LayoutInflater.from(contextActivity);
if (tabId == 0) {
View view = inflater.inflate(R.layout.listviewlayout, null, false);
list = (PullToRefreshListView) view.findViewById(R.id.listView);
}
if (tabId == 1) {
View view = inflater.inflate(R.layout.listviewlayout2, null, false);
list = (PullToRefreshListView) view.findViewById(R.id.listView2);
}
list.setAdapter(adapter);
adapter.setNotifyOnChange(true);
Log.d("COUNT") shows - "4", what means array is built fine.
public class Adapter extends ArrayAdapter<JSONObject> {
private final Activity context;
private final ArrayList<JSONObject> profiles;
public String header;
public String preText;
public String imageURL;
static class ViewHolder {
public TextView header;
public ImageView image;
public TextView preText;
public LinearLayout linearItemLayout;
}
public Adapter(Activity context, ArrayList<JSONObject> array) {
super(context, R.layout.tab1_list_layout, array);
Log.d("ADAPTER", "SETTING ADAPTER");
this.context = context;
this.profiles = array;
Log.d("COUNT", "" + profiles.size());
}
#Override
public int getCount()
{
return profiles.size();
}
My getView method:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
Log.i("GetView Log", "Adapters GetView hasbeen called");// thats never calling
if (rowView == null) {
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.tab1_list_layout, null);
ViewHolder viewHolder = new ViewHolder();
viewHolder.linearItemLayout = (LinearLayout) rowView.findViewById(R.id.linearItemLayout);
viewHolder.header = (TextView) rowView.findViewById(R.id.itemHeader);
viewHolder.image = (ImageView) rowView.findViewById(R.id.itemPreImage);
viewHolder.preText = (TextView) rowView.findViewById(R.id.itemPreText);
rowView.setTag(viewHolder);
}
ViewHolder holder = (ViewHolder) rowView.getTag();
JSONObject item = null;
try {
item = profiles.get(position);
header = item.getString("header");
preText = item.getString("previewText");
imageURL = item.getString("previewImageUrl");
} catch (JSONException e) {
e.printStackTrace();
}
Log.d("ADAPTER LOG", header);
holder.header.setText(header);
holder.preText.setText(preText);
int headerHeight = (int) Math.round(header.length() * 1.5);
holder.linearItemLayout.setMinimumHeight(40 + headerHeight + preText.length());
Picasso.with(context).setIndicatorsEnabled(true);
Picasso.with(context).load(imageURL).resize(140,100).into(holder.image);
return rowView;
}
In MainActivity i calling, where "this" = MainActivity;
Loader loader = new Loader(this);
loader.loadTabInfo(0);
Method getView never calling. What I'am doing wrong?
UPD:
Solved:
public void drawTabInfo(String jsonBlob, int tabId)
{
JSONObject dataJsonObj;
PullToRefreshListView list = null;
try {
dataJsonObj = new JSONObject(jsonBlob);
JSONArray jsonData = dataJsonObj.getJSONArray("steals");
ArrayList<JSONObject> data = new ArrayList<JSONObject>();
for (int i=0;i<jsonData.length();i++){
data.add(jsonData.getJSONObject(i));
}
Adapter adapter = new Adapter(contextActivity, data);
//LayoutInflater inflater = LayoutInflater.from(contextActivity);
if (tabId == 0) {
//View view = inflater.inflate(R.layout.listviewlayout, null, false);
list = (PullToRefreshListView) contextActivity.findViewById(R.id.listView);
Your code is fine .. just you need a little bit change in get view method
add a return statement.. like return rowView;
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
Log.d("GETVIEaW", "GETVIEW");
if (rowView == null) {
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.tab1_list_layout, null);
ViewHolder viewHolder = new ViewHolder();
viewHolder.linearItemLayout = (LinearLayout) rowView.findViewById(R.id.linearItemLayout);
viewHolder.header = (TextView) rowView.findViewById(R.id.itemHeader);
viewHolder.image = (ImageView) rowView.findViewById(R.id.itemPreImage);
viewHolder.preText = (TextView) rowView.findViewById(R.id.itemPreText);
rowView.setTag(viewHolder);
}
else{
viewHolder = (ViewHolder) rowView.getTag();
}
viewHolder.preText.setText("Type your name");
return rowView;
}
Are you sure that Log.d() is not called? You may want to change logging scope, usually default scope is info and it doesn't show debug messages. You can try to use Log.i() or change scope.
Try posting your adapter.setNotifyOnChange(true); to handler. Follow this link
I got the following Fragment
public static class DummySectionFragment extends Fragment {
ListView msgList;
ArrayList<StundenplanDetail> details;
AdapterView.AdapterContextMenuInfo info;
public void addVertretung (String Fach, String Raum, String Farbe) {
StundenplanDetail Detail;
Detail = new StundenplanDetail();
Detail.setFach(Fach);
Detail.setRaum(Raum);
Detail.setFarbe(Farbe);
details.add(Detail);
}
/**
* The fragment argument representing the section number for this
* fragment.
*/
XMLParser parser = new XMLParser();
String xml = null;
public static final String ARG_SECTION_NUMBER = "section_number";
public void loadArray (String dayArray) {
}
public DummySectionFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
try{
byte[] inputBuffer = new byte[20000];
File inputFile = new File("sdcard/Android/data/com.approfi.woehlerschule/data.woehler");
FileInputStream fileInputStream = new FileInputStream(inputFile);
fileInputStream.read(inputBuffer);
xml = new String(inputBuffer);
fileInputStream.close();
}catch(IOException e){
Log.d("Fehler:", e.toString());
}
View rootView = inflater.inflate(
R.layout.fragment_stundenplan_dummy, container, false);
if(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)).equals("1")){
int splitpos1 = xml.indexOf("<Montag>");
int splitpos2 = xml.indexOf("</Montag>") + 9;
String xml2 = xml.substring(splitpos1, splitpos2);
org.w3c.dom.Document doc = parser.getDomElement(xml2);
NodeList nl = doc.getElementsByTagName("Stunde");
for (int i = 0; i < nl.getLength(); i++) {
Element e = (Element)nl.item(i);
String Fach = parser.getValue(e, "Fach"); // name child value
String Raum = parser.getValue(e, "Raum"); // cost child value
String Farbe = parser.getValue(e, "Farbe"); // description child value
Log.d("Fail:", Fach + Raum + Farbe);
}
}
/*if(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)).equals("2")){
ListView listViewStundenplan = (ListView) rootView.findViewById(R.id.listViewStundenplan);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item_stundenplan, dienstagArray);
listViewStundenplan.setAdapter(arrayAdapter);
}
if(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)).equals("3")){
ListView listViewStundenplan = (ListView) rootView.findViewById(R.id.listViewStundenplan);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item_stundenplan, mittwochArray);
listViewStundenplan.setAdapter(arrayAdapter);
}
if(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)).equals("4")){
ListView listViewStundenplan = (ListView) rootView.findViewById(R.id.listViewStundenplan);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item_stundenplan, donnerstagArray);
listViewStundenplan.setAdapter(arrayAdapter);
}
if(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)).equals("5")){
ListView listViewStundenplan = (ListView) rootView.findViewById(R.id.listViewStundenplan);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item_stundenplan, freitagArray);
listViewStundenplan.setAdapter(arrayAdapter);
}
*/
msgList = (ListView) rootView.findViewById(R.id.listViewStundenplan);
msgList.setAdapter(new StundenplanAdapter(details, this));
return rootView;
}
}
And that Adapter for my List View
public class StundenplanAdapter extends BaseAdapter {
private ArrayList<StundenplanDetail> _data;
Context _c;
StundenplanAdapter (ArrayList<StundenplanDetail> data, Context c){
_data = data;
_c = c;
}
public int getCount() {
return _data.size();
}
public Object getItem(int position) {
return _data.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null)
{
LayoutInflater vi = (LayoutInflater)_c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_item_stundenplan, null);
}
TextView fachText = (TextView)v.findViewById(R.id.textViewStundenplanFach);
TextView raumText = (TextView)v.findViewById(R.id.textViewStundenplanRaum);
View colorBlock = (View)v.findViewById(R.id.colorBlockStundenplan);
StundenplanDetail msg = _data.get(position);
fachText.setText(msg.fach);
raumText.setText(msg.raum);
colorBlock.setBackgroundColor(37000000);;
return v;
}
}
Now the problem is, that eclipse tells me that The constructor StundenplanAdapter(ArrayList<StundenplanDetail>, Stundenplan.DummySectionFragment) is undefined but i want to use my custom adapter. Every thing works. The List View in my Fragment and the Custom adapter but not if I want to use the custom adapter in my fragment-listview
I hope you can help me
thanks in advance,
MoBr114
Send your FragmentActivity context to your Adapter not Fragment itself!
Try :
msgList.setAdapter(new StundenplanAdapter(details, getActivity()));
You are passing the wrong argument to the Adapter in msgList.setAdapter(). It looks for the context of the Activity non the instance of the class DummySectionFragment.