Java newbabie!!! Please help !!! - Java for Android App Development

PLEASE HELP ME GUYS!!! I HAVE NO CLUE WHAT I SUPPOSE TO DO THIS ASSIGNMENT :
+ Create an app with multiple activities and a ListView with data consisting of instances of a custom model class. Provide an addtional activity allow editing of the data.
+ A ListView with 8 Course Model Class
+ A seperate Activity is display on click of ClickView, which show 3 value (Title, Catalog, Capacity) and editable
-----------------------
So first I create a listview with 8 items on main activity layout, and then this my my MainActivity.class :
ListView listView = (ListView) findViewById(R.id.listView);
String[] items = { "English 156", "Match 232", "Chemistry 110FL", "Physical 051", "Piano 09","Guitar 100","Android Development 207","History 207A" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
Intent myIntent = new Intent(getApplicationContext(), FragmentClass.class);
startActivity(myIntent);
}
});
---------
I have problem that when running app, 2 activity appears at same time...
Please help me with remain codes and what can I do to fix that problems...

my app being shown like this, I want course title, catalog and capacity are shown itemonclick only.

Really???? Noone is wiliing to help me in this case???? SO SAD!!!!

ptt1404 said:
Really???? Noone is wiliing to help me in this case???? SO SAD!!!!
Click to expand...
Click to collapse
Just looks like your throwing a fragment onto the top of a listView ? Not sure what you want, it is not clear

Do you have a second fragment with transparent background?

Related

[Q] ListView SimpleAdapter problem when i scroll,the progressbar move to other item

Hello,
I have a ListView , in this ListView i have a download button for each item, i have an hidden textview with a ID that i need when i click the button (the textview is filled when the listview is populating), and also i have a hidden progress bar for each item, that it appears when the downloading starts , when i click Download, i instance an asyctask, in this async task i send the progressbar object to be able to update their progress value, and the id i need
the problem is when i scroll my list , and im downloading, the progressbar appears in other item , is there any way to avoid that?
my SimpleAdapter
Code:
public View getView(int position, View convertView, ViewGroup parent) {
View row=super.getView(position, convertView, parent);
//Hidden textview
TextView videoIdText = (TextView) row.findViewById(R.id.videoId);
Button downloadButton = (Button) row.findViewById(R.id.videoDownload);
final ProgressBar downloadProgressBar = (ProgressBar) row.findViewById(R.id.downloadProgressBar);
final String videoId = videoIdText.getText().toString();
downloadButton.setOnClickListener(new OnClickListener(){
[user=439709]@override[/user]
public void onClick(View view) {
DownloadTask task = new DownloadTask();
task.setProgressBar(downloadProgressBar);
task.setVideoId(videoId);
task.execute();
}
});
return row;
}
Please post your full code, layout and manifest so I can try and debug it. Thanks.
frenzyboi said:
Please post your full code, layout and manifest so I can try and debug it. Thanks.
Click to expand...
Click to collapse
Hey there are the files, the feed_item, is the one i used to each item of the ListItem

ListView with tabhost with listviews

I want to create a listview. On the last row of the listview, I want to show a tabhost with two tabs and on each tab I would like to load another listview. For this, I've created three XML files.
The main.xml is where I'm loading the initial list. The mainlistdata.xml is how each row of the list is presented. And the last xml is tabslayout.xml. This layout is used for the last row on the main listview. It will load the tabhost. Here is the code with all the xml
My main class is this:
Code:
public class MainActivity extends Activity {
ArrayList<testData> myData = new ArrayList<testData>();
ListView listContent;
TestAdapter dataAdapter;
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] aString = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };
for (int i = 0; i < aString.length; i++) {
myData.add(new testData(i, aString[i]));
}
listContent = (ListView) findViewById(R.id.mainlist);
dataAdapter = new TestAdapter(this, myData,1);
listContent.setAdapter(dataAdapter);
}
}
And this is the class that loads the content on the listviews and where I set the tabhost:
Code:
public class TestAdapter extends ArrayAdapter<testData> {
private Context context;
private ArrayList<testData> data;
private ListView listContent,listContent2;
public TabHost tabs;
TestAdapter dataAdapter;
private int choice;
public TestAdapter(Context context, ArrayList<testData> data, int choice) {
super(context, R.layout.mainlistdata, data);
this.context = context;
this.data = data;
this.choice = choice;
}
[user=439709]@override[/user]
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView;
if (choice == 1) {
if (position == data.size()-1) {
rowView = inflater.inflate(R.layout.tabslayout, parent, false);
TextView first = (TextView) rowView.findViewById(R.id.dataTitle);
TextView second = (TextView) rowView.findViewById(R.id.dataValue);
TextView third = (TextView) rowView.findViewById(R.id.dataValueString);
first.setText("Item " + data.get(position).getIntValue());
second.setText(String.valueOf(data.get(position).getIntValue()));
third.setText(data.get(position).getStringValue());
tabs = (TabHost) rowView.findViewById(android.R.id.tabhost);
tabs.setup();
TabSpec tab1 = tabs.newTabSpec("Tab 1");
TabSpec tab2 = tabs.newTabSpec("Tab 2");
tab1.setContent(R.id.tabOne);
tab2.setContent(R.id.tabTwo);
tab1.setIndicator("One");
tab2.setIndicator("two");
tabs.addTab(tab1);
tabs.addTab(tab2);
tabs.setup();
listContent = (ListView) rowView.findViewById(R.id.listOne);
dataAdapter = new TestAdapter(context, data,2);
listContent.setAdapter(dataAdapter);
listContent2 = (ListView) rowView.findViewById(R.id.listTwo);
dataAdapter = new TestAdapter(context, data,3);
listContent2.setAdapter(dataAdapter);
} else {
rowView = inflater.inflate(R.layout.mainlistdata, parent, false);
TextView first = (TextView) rowView.findViewById(R.id.dataTitle);
TextView second = (TextView) rowView.findViewById(R.id.dataValue);
TextView third = (TextView) rowView.findViewById(R.id.dataValueString);
first.setText("Item " + data.get(position).getIntValue());
second.setText(String.valueOf(data.get(position).getIntValue()));
third.setText(data.get(position).getStringValue());
}
} else if (choice == 2) {
rowView = inflater.inflate(R.layout.mainlistdata, parent, false);
TextView first = (TextView) rowView.findViewById(R.id.dataTitle);
TextView second = (TextView) rowView.findViewById(R.id.dataValue);
TextView third = (TextView) rowView.findViewById(R.id.dataValueString);
first.setText("Item tab1 " + data.get(position).getIntValue());
second.setText(String.valueOf(data.get(position).getIntValue()));
third.setText(data.get(position).getStringValue());
} else {
rowView = inflater.inflate(R.layout.mainlistdata, parent, false);
TextView first = (TextView) rowView.findViewById(R.id.dataTitle);
TextView second = (TextView) rowView.findViewById(R.id.dataValue);
TextView third = (TextView) rowView.findViewById(R.id.dataValueString);
first.setText("Item tab2 " + data.get(position).getIntValue());
second.setText(String.valueOf(data.get(position).getIntValue()));
third.setText(data.get(position).getStringValue());
}
return rowView;
}
}
As you can see on the next image, I'm loading the data and then I'm loading the tabs. The problem is that each tab is only loading the first row of the data. Any idea How can I solve this?
favolas said:
I want to create a listview. On the last row of the listview, I want to show a tabhost with two tabs and on each tab I would like to load another listview. For this, I've created three XML files.
As you can see on the next image, I'm loading the data and then I'm loading the tabs. The problem is that each tab is only loading the first row of the data. Any idea How can I solve this?
Click to expand...
Click to collapse
I don't quite understand what you are trying to do but why do you want the tabs in the last item of the list? Could you just put the tabs in the main activity and add a scrollview to bring the tabs up or set the weight of the layout so the tabs take up the bottom of the screen and the top list would scroll if to long?
Your problem may be fixed if you set the tabs and the lists inside to be a certain size (200dp or something). I know sometimes having it set to wrap_content will only show one list item
Acela1230 said:
I don't quite understand what you are trying to do but why do you want the tabs in the last item of the list? Could you just put the tabs in the main activity and add a scrollview to bring the tabs up or set the weight of the layout so the tabs take up the bottom of the screen and the top list would scroll if to long?
Your problem may be fixed if you set the tabs and the lists inside to be a certain size (200dp or something). I know sometimes having it set to wrap_content will only show one list item
Click to expand...
Click to collapse
Hello. Thanks for your answer.
My initial idea was to create a scrollview. In this scrollview, create the main listview, load the tabhost and them, in each tab, load another listview. After seeing this question, I saw that we shouldn't place a listview inside one scrollview. That's why I came up with this solution.
I've tried to do your solution but changing the height did not solve the problem.
After some debugging I belive that this is not working because the first list loads the last row (the one with the tabs) and then returns, not giving time to load the other lists. Can this be the problem?
Regards,
favolas
favolas said:
Hello. Thanks for your answer.
My initial idea was to create a scrollview. In this scrollview, create the main listview, load the tabhost and them, in each tab, load another listview. After seeing this question, I saw that we shouldn't place a listview inside one scrollview. That's why I came up with this solution.
I've tried to do your solution but changing the height did not solve the problem.
After some debugging I belive that this is not working because the first list loads the last row (the one with the tabs) and then returns, not giving time to load the other lists. Can this be the problem?
Regards,
favolas
Click to expand...
Click to collapse
But why? Why not just ActionBar Tabs with ListViews in them? At any rate, you can only have one View in a ScrollView, which is why people always put i.e. a RelativeLayout in a ScrollView, with the other Views in there.
Hi bassie1995.
Sorry for the late feedback.
bassie1995 said:
But why? Why not just ActionBar Tabs with ListViews in them?
Click to expand...
Click to collapse
This is just because my data is this way. Will then try to follow your suggestion and do actionbar tabs and put listview in them.
Regards,
Favolas

How to launch an Activity on ListView Item click android

hello
I am new to android application development and i am developing and application for learning purpose.
Can any body please help me in creating an activity that launch and activity on ListView Item Click.
I am using Following code for Populating Listview Items.
filename: activity_menu.java
package com.my.android.app;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.app.LauncherActivity.ListItem;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class activity_menu extends Activity {
String[] countries = new String[] {
"Contacts",
"SMS",
"Files & Photos",
"Prefrences",
"Logout",
};
// Array of integers points to images stored in /res/drawable-ldpi/
int[] flags = new int[]{
R.drawable.phonebook,
R.drawable.sms,
R.drawable.filesphotos,
R.drawable.settings,
R.drawable.key,
};
// Array of strings to store currencies
String[] currency = new String[]{
"Manage Contacts Backup",
"Manage SMS Backup",
"Manage files & Photos Backup",
"Set your prefrences",
"Logout of Application",
};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
// Each row in the list stores country name, currency and flag
List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
for(int i=0;i<5;i++){
HashMap<String, String> hm = new HashMap<String,String>();
hm.put("txt", countries);
hm.put("cur", currency);
hm.put("flag", Integer.toString(flags) );
aList.add(hm);
}
// Keys used in Hashmap
String[] from = { "flag","txt","cur" };
// Ids of views in listview_layout
int[] to = { R.id.flag,R.id.txt,R.id.cur};
// Instantiating an adapter to store each items
// R.layout.listview_layout defines the layout of each item
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.listview_layout, from, to);
// Getting a reference to listview of main.xml layout file
ListView listView = ( ListView ) findViewById(R.id.listview);
// Setting the adapter to the listView
listView.setAdapter(adapter);
}
}
Please help on this. waiting for Responses.
Try this
Code:
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
// Do your action here
}
});
There are multiple ways. If using a ListFragment/ListActivity, onListItemClick can also be used. However, I think that that is used especially with a programmatically-defined ListView.
If you want to start a new Activity, do some research about Intents.
SagiLo said:
Try this
Code:
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
// Do your action here
}
});
Click to expand...
Click to collapse
This. Use the position parameter if you want it to only open when a particular item is clicked (It is zero-based, if I remember correctly)
Then, to open the Intent:
Code:
Intent myIntent = new Intent(getApplicationContext(), ActivityName.class);
startActivity(myIntent);
Opening on any listitem click:
Code:
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
Intent myIntent = new Intent(getApplicationContext(), ActivityName.class);
startActivity(myIntent);
}
});
Opening on a specific item:
Code:
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
if (position == ITEM_POSITION_HERE)
{
Intent myIntent = new Intent(getApplicationContext(), ActivityName.class);
startActivity(myIntent);
}
}
});
how so i implement the code to open new activity on row cick?
cyr0s said:
This. Use the position parameter if you want it to only open when a particular item is clicked (It is zero-based, if I remember correctly)
Then, to open the Intent:
Code:
Intent myIntent = new Intent(getApplicationContext(), ActivityName.class);
startActivity(myIntent);
Opening on any listitem click:
Code:
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
Intent myIntent = new Intent(getApplicationContext(), ActivityName.class);
startActivity(myIntent);
}
});
Opening on a specific item:
Code:
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
if (position == ITEM_POSITION_HERE)
{
Intent myIntent = new Intent(getApplicationContext(), ActivityName.class);
startActivity(myIntent);
}
}
});
Click to expand...
Click to collapse
How do i implement this code in my main activity's java file?
suppose, i have a main activity which contains the listview and i have another activity "Activity_game"
and i want to open Activity_game on clicking the first row,
so i put "0" at "ITEM_POSITION_HERE" and "game" at the place of "ActivityName"
but it gives my a bunch of errors and it doesn't work
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
if (position == ITEM_POSITION_HERE)
{
Intent myIntent = new Intent(getApplicationContext(), ActivityName.class);
startActivity(myIntent);
}
}
});
nnnn1111 said:
How do i implement this code in my main activity's java file?
suppose, i have a main activity which contains the listview and i have another activity "Activity_game"
and i want to open Activity_game on clicking the first row,
so i put "0" at "ITEM_POSITION_HERE" and "game" at the place of "ActivityName"
but it gives my a bunch of errors and it doesn't work
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
if (position == ITEM_POSITION_HERE)
{
Intent myIntent = new Intent(getApplicationContext(), ActivityName.class);
startActivity(myIntent);
}
}
});
Click to expand...
Click to collapse
We need to know which errors you get.
Read this: http://forum.xda-developers.com/showthread.php?t=2439748
one question
nikwen said:
We need to know which errors you get.
Read this: http://forum.xda-developers.com/showthread.php?t=2439748
Click to expand...
Click to collapse
my problem is , when i run the app, it doesn't show any rows , not even any listview , or string
(just blank white page)
i have two activities
first one is Activity_main.xml in which ive implemented listview
here is it's java file
package com.example.desi;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
public class MainActivity extends Activity {
String[] items = { "item 1", "item 2", "item 3", "item 4", "item 5" };
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
switch( position )
{
case 0: Intent newActivity = new Intent(this, Munda.class);
startActivity(newActivity);
break;
}
}
}
and another activity is Activity_munda.xml
so, i want to run "Activity_munda" through on row click of listview
i also want to have many activities later in my app
my project is error free right now
do you think i have incomplete code?
nnnn1111 said:
my problem is , when i run the app, it doesn't show any rows , not even any listview , or string
(just blank white page)
i have two activities
first one is Activity_main.xml in which ive implemented listview
here is it's java file
package com.example.desi;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
public class MainActivity extends Activity {
String[] items = { "item 1", "item 2", "item 3", "item 4", "item 5" };
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
switch( position )
{
case 0: Intent newActivity = new Intent(this, Munda.class);
startActivity(newActivity);
break;
}
}
}
and another activity is Activity_munda.xml
so, i want to run "Activity_munda" through on row click of listview
i also want to have many activities later in my app
my project is error free right now
do you think i have incomplete code?
Click to expand...
Click to collapse
Your code is incomplete. You haven't populated the layout "activity_main.xml" here. and haven't added the list view, So it is indeed correct the app displays a blank page with no errors.
vijai2011 said:
Your code is incomplete. You haven't populated the layout "activity_main.xml" here. and haven't added the list view, So it is indeed correct the app displays a blank page with no errors.
Click to expand...
Click to collapse
Right. Read this: http://www.vogella.com/articles/AndroidListView/article.html
i already tried that
vijai2011 said:
Your code is incomplete. You haven't populated the layout "activity_main.xml" here. and haven't added the list view, So it is indeed correct the app displays a blank page with no errors.
Click to expand...
Click to collapse
but with no luck
its very complicated
can you please give me a simple tutorial ?
please dont refer to any other tutorial as i have already tried all of them,
thanks
nnnn1111 said:
but with no luck
its very complicated
can you please give me a simple tutorial ?
please dont refer to any other tutorial as i have already tried all of them,
thanks
Click to expand...
Click to collapse
Come on, you haven't tried all of them. Please don't let everyone else do the work for you. And if you can manage to do it yourself, it will be a better learning experience.
I can offer that you try the tutorial I posted and if your code doesn't work, you can post it together with a logcat and we'll check it.
haha i know
nikwen said:
Come on, you haven't tried all of them. Please don't let everyone else do the work for you. And if you can manage to do it yourself, it will be a better learning experience.
I can offer that you try the tutorial I posted and if your code doesn't work, you can post it together with a logcat and we'll check it.
Click to expand...
Click to collapse
but ive been searching for like 15 days about this problem on stack overflow , saw like 30 posts but they arent clear , please do me a favor and
tell me in few steps you will also feel good helping me.
and it will be a VERY big favor to me , trust me
nnnn1111 said:
but ive been searching for like 15 days about this problem on stack overflow , saw like 30 posts but they arent clear , please do me a favor and
tell me in few steps you will also feel good helping me.
and it will be a VERY big favor to me , trust me
Click to expand...
Click to collapse
Read this about ListActivity. It should answer your questions: http://www.vogella.com/articles/AndroidListView/article.html#listactivity
And I won't post step by step guides on how to implement ListViews. There are many great tutorials out there. I can help you if you have problems/errors with your code, but @vijai2011 has already explained what you need to do.
You cannot expect anyone to spoon feed you. We are here to help people who try to do something but run into problems. Not people who doesnt even try to understand what they do. The guides by vogella are very simple and there is no problem with the guide. You cannot simply copy paste the codes from the tutorial and expect it to compile and work.
vijai2011 said:
You cannot expect anyone to spoon feed you. We are here to help people who try to do something but run into problems. Not people who doesnt even try to understand what they do. The guides by vogella are very simple and there is no problem with the guide. You cannot simply copy paste the codes from the tutorial and expect it to compile and work.
Click to expand...
Click to collapse
i didn't find my answer on that website
nnnn1111 said:
i didn't find my answer on that website
Click to expand...
Click to collapse
Then it may mean that you dont know what you are searching for! Because the tutorial is all about what you need - ListView!

A question about list

Hi.
I am building an app that has a list. All of the clicks on list items result in one activity, like XActivity. XActivity contains a text view. I want that when I click on ListItem1 the text view shows a string forexample string1, then onClickListItem2 it shows string2, onListItemClicX it shows stringX. If it's confusing let me know.
How can I handle it ?
Thanks in advance.
torpedo mohammadi said:
Hi.
I am building an app that has a list. All of the clicks on list items result in one activity, like XActivity. XActivity contains a text view. I want that when I click on ListItem1 the text view shows a string forexample string1, then onClickListItem2 it shows string2, onListItemClicX it shows stringX. If it's confusing let me know.
How can I handle it ?
Thanks in advance.
Click to expand...
Click to collapse
I'm assuming you're using an ArrayAdapter - it would be helpful if you posted some code.
You have to setup an OnItemClickListener for your ListView. In the OnItemClick method, you will have to get the data from the adapter using the position that was clicked. Then create an intent to ActivityX and pass the clicked data.
Code:
[B]ArrayAdapter adapter = new ArrayAdapter();[/B]
listView.setClickable(true);
//Setup the on item click listener for the listview
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Create an intent pointing to XActivity
Intent intent = new Intent(this, XActivity.class);
//Add the clicked string as an extra
intent.putExtra("KEY_CLICKEDSTRING", adapter.getItem(position - 1));
//Start the activity
this.startActivity(intent);
}
});
In ActivityX, simply retrieve the data and set it in the textview:
Code:
@Override
public void onCreate(Bundle savedInstanceState) {
... Activity setup code
TextView textView = findViewById(R.id.yourtextview);
//Get the intent data
String strClicked = getIntent().getStringExtra("KEY_CLICKEDSTRING");
textView.setText(strClicked);
}
Thank you.
But I don't know what value does the "int position" give me ? Forexample does it give the value of "1" or .... ?
I want to know that in order to know which item was clicked and now what to do .
Can anybody help me ?
Thanks in advance.
Like previously mentioned. Post your code and we can help you out better.
not complex, First I make a new ArrayAdapter<string> add adapt it to my listView. Up to know no problem. But in the continue :
// usual codes to answer a click onItemClick(AdapterView<?> av, View v, int position, long id) {
//create a intent as Alkonic said
//My problem }
...
@My problem I want to use a set of codes and understand which item was clicked. I have 41 items and my idea is this :
// getting the text of the clicked item String title = ((TextView)v).getText().toString();
If (title=getString(R.string.theTextIHadAddedToArrayAdapterForItem1)) {
// so item 1 was clicked
//Change the text of thesecond TextView in the second activity ( wich I wrote intent for)
// rewrite the if order 41 times for 41 items
...
But this doesn't work. Can anybody help me ?
@Alconic :
I don't understand the method you wrote for retrieving intent data ( I mean
//Get the intent data String strClicked = getIntent().getStringExtra("KEY_CLICKEDSTRING");
) because the "Key_CLICKEDSTRING" is always constant and one thing. How can I cange it for 41 items ?
Thank you guys anyway.
not complex, First I make a new ArrayAdapter<string> add adapt it to my listView. Up to know no problem. But in the continue :
// usual codes to answer a click onItemClick(AdapterView<?> av, View v, int position, long id) {
//create a intent as Alkonic said
//My problem }
...
@My problem I want to use a set of codes and understand which item was clicked. I have 41 items and my idea is this :
// getting the text of the clicked item String title = ((TextView)v).getText().toString();
If (title=getString(R.string.theTextIHadAddedToArrayAdapterForItem1)) {
// so item 1 was clicked
//Change the text of thesecond TextView in the second activity ( wich I wrote intent for)
// rewrite the if order 41 times for 41 items
...
But this doesn't work. Can anybody help me ?
@Alconic :
I don't understand the method you wrote for retrieving intent data ( I mean
//Get the intent data String strClicked = getIntent().getStringExtra("KEY_CLICKEDSTRING");
) because the "Key_CLICKEDSTRING" is always constant and one thing. How can I cange it for 41 items ?
Thank you guys anyway.
You should post your onItemClick routine. But for going at it blind on our side this is what I would do:
Code:
public void onItemClick(AdapterView<?> av, View v, int pos, long id) {
//Get text from a textview that is on your listview
TextView textView2 = (TextView) v.findViewById(R.id.myTextView);
String txtMytext = textView2.getText().toString();
//Create an intent pointing to XActivity
Intent intent = new Intent(this, XActivity.class);
//Add the clicked string as an extra
intent.putExtra("KEY_CLICKEDSTRING", txtMytext);
//Start the activity
this.startActivity(intent);
}
Without your code, I don't understand what you are comparing with the if statement you posted. Use this along with @Alkonic 's code should get you in the right direction. If it doesn't, we need more code to help better.
torpedo mohammadi said:
not complex, First I make a new ArrayAdapter<string> add adapt it to my listView. Up to know no problem. But in the continue :
// usual codes to answer a click onItemClick(AdapterView<?> av, View v, int position, long id) {
//create a intent as Alkonic said
//My problem }
...
@My problem I want to use a set of codes and understand which item was clicked. I have 41 items and my idea is this :
// getting the text of the clicked item String title = ((TextView)v).getText().toString();
If (title=getString(R.string.theTextIHadAddedToArrayAdapterForItem1)) {
// so item 1 was clicked
//Change the text of thesecond TextView in the second activity ( wich I wrote intent for)
// rewrite the if order 41 times for 41 items
...
But this doesn't work. Can anybody help me ?
@Alconic :
I don't understand the method you wrote for retrieving intent data ( I mean
//Get the intent data String strClicked = getIntent().getStringExtra("KEY_CLICKEDSTRING");
) because the "Key_CLICKEDSTRING" is always constant and one thing. How can I cange it for 41 items ?
Thank you guys anyway.
Click to expand...
Click to collapse
Handling Clicks:
The int position refers to the position of the item that was clicked in the arrayadapter.
Instead of this:
Code:
String title = ((TextView)v).getText().toString();
you can use
Code:
String title = adapter.getItem(position);
That code will take the clicked position and get the clicked string from the adapter.
Intent Data
When you send data with an intent, it has two parts: the Name and Value.
The following code sets the name to a constant "Key_CLICKEDSTRING" and the Value is variable, depending on what was clicked.
Code:
intent.putExtra("KEY_CLICKEDSTRING", adapter.getItem(position));
When you want to access the VALUE, you have to provide the constant name:
Code:
String strClicked = getIntent().getStringExtra("KEY_CLICKEDSTRING");
Hope that helped!

ListView in Fragment not displaying

Hello, I am hoping someone can point out anything I might be doing wrong that is causing my issue. (seems hard to find "best practice" type of advise for newer android features)
My problem is that a ListView which is inside a fragment, controlled by a ViewPager does not get displayed on the screen if I swipe to the next fragment. (but a TextView inside the same fragment shows up fine on all fragment instances),
To set the scene, I have only one fragment class out of which I instantiate all the fragments used by the ViewPager.
Inside the fragments onActivityCreated I create a cursor loader and a custom cursor adapter and on the loaders onLoadFinished I swap the cursor as its supposed to be. Naturally the customCursorAdapter that I created handles the inflation of the listView row in the newView method and binds the data from the cursor in the bindView method.
Does this sound right? I mean I did it this way with the intent of not having tight coupling, having the fragment be independent. So why won't it draw out the listview for subsequent pages? From what I have seen in the logs, all methods get called for all fragments, including getItem, newView, bindView etc.
Final quirk is that the "previous" fragmet's listView does get drawn. Meaning I start on Fragment 1 - listView is there. Move to fragment 2 - listview not there, move to fragment 3 list view not there, move back to fragment 2 list view IS there now, and back to fragment 1 it is NOT there anymore.
I don't have time to post code right now but will as soon as I get a chance.
Thank you all.
Code:
//main activity which creates the view pager and adapter and starts the process
protected void onCreate(Bundle savedInstanceState) {
Log.d(TAG,"onCreate begin");
super.onCreate(savedInstanceState);
setContentView(R.layout.pager_task_tracking);
viewPager = (ViewPager) findViewById(R.id.pager);
FragmentManager fragmentManager = getSupportFragmentManager();
MyPagerAdapter myPagerAdapter = new MyPagerAdapter(fragmentManager);
viewPager.setOffscreenPageLimit(0); // trying to force drawing of fragments
viewPager.setAdapter(myPagerAdapter);
Log.d(TAG,"onCreate viewPager done");
//MyPagerAdapter getItem (extends FregmentSatePagerAdapter
public Fragment getItem(int i) {
Log.d(TAG,"getItem called with item:"+i);
return TaskListFragment.newInstance(i);
}
//fragment onActivityCreated , this fragment instanciated in getView of viewPager
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.d(TAG, "onActivityCreated with datePointer:"+datePointer);
TextView textViewFragmentTitle = (TextView) getView().findViewById(R.id.fragmentTitle);
textViewFragmentTitle.setText("DATE:"+datePointer);
loaderManager = getLoaderManager();
list = (ListView) getActivity().findViewById(R.id.TaskListView);
customAdapter = new CustomCursorAdapter(getActivity(), null, 1 , datePointer);
list.setAdapter(customAdapter);
loaderID = datePointer; //attempt to get fragment listview to draw - not working
loaderManager.initLoader(loaderID, null, this);
}
///cursor loader onFinish
public void onLoadFinished(android.support.v4.content.Loader<Cursor> loader,
Cursor newCursor) {
Log.d(TAG, "onLoadFinished:"+datePointer);
if (customAdapter != null && newCursor != null ){
Log.d(TAG, "onLoadFinished swapCursor:"+datePointer);
customAdapter.swapCursor(newCursor);
//doesnt help customAdapter.notifyDataSetChanged();
} else {
Log.v(TAG,"OnLoadFinished: mAdapter is null");
}
}
//MyCustomCursorAdapter (used by the listview inside the fragment above)
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View retView = inflater.inflate(R.layout.task_list_item_layout, null);
RelativeLayout.LayoutParams relativeLayoutParameters;
Log.d(TAG,"newView layout inflated:"+datePointer);
return retView;
}
///bindView of same MyCustomCursorAdapter
public void bindView(View view, Context context, Cursor cursor) {
Log.d(TAG,"bindView:"+datePointer);
TextView tvName = (TextView) view.findViewById(R.id.textviewItemName);
tvName.setText(cursor.getString(cursor.getColumnIndex(DBHelper.C_TASK_NAME))+" ["+datePointer+"]");
}
Also check out the LogCat logs with description of the action in the name corresponding to the log statements in the code above.
I noticed more detail about what happens (if you look at the logcat you will see also)
When the screen is displayed the first fragment has the listview present, but the fragment has the id/datePointer (same thing) of "0" BUT the listview items have an id/datePointer of "1"
If you look at the initial logcat you will see "newView layout inflated:1" but no "newView layout inflated:0" how this is being skipped I have no idea. And how the id's can be mix-matched between fragment instances baffles me.
And lastly, after scrolling right twice (to the 0,1, 2nd fragment) it did not have the listview as reported, but scrolling BACK ONE the listview shows up in the 1st fragment ... but even more baffling with lisvtView items that report being part of the "0" fragment .... whaaaat?
Why are you using a listview inside fragment when you have listfragment available?
EatHeat said:
Why are you using a listview inside fragment when you have listfragment available?
Click to expand...
Click to collapse
EatHeat, I am doing this for the sake of flexibility for adding additional controls/views to the fragment besides just a list view (Example I envision adding several buttons to the bottom of the fragment/screen which will be "outside" the listview.
Does it make more sense what I am doing now? That is exactly one of the questions I have, its so hard to find advise on what makes more sense when it comes to these complex combinations of viewPager+fragments+custom listViews+loaders
So my current flow is:
1--Activity (creates ViewPager with myPagerAdapter)
2----ViewPager in itst getItem method instantiates a fragment from class TaskListFragment
3-------The fragment in it's onActivityCreated instantiates a CustomCursorAdapter (with null cursor) and initializes a loader
4-------The loader swaps the cursor in it's onLoadFinished (and this is supposed to populate the listview)
Items 2-4 repeat for every "swipe" or every fragment instantiated.
-The listView control XML is defined in an XML file which is used by the fragment's setContentView
-The lisView row layout XML is defined in a separate XML file inflated by the customCursorAdapter's newView method.
SOLVED: (after literally one week of headache and rewriting the code in every way I know)
the fragments onCreateView I had it getting the listview with getActivity() .. but I should have done it with getView()
the solution was replacing this line
Code:
list = (ListView) getActivity().findViewById(R.id.TaskListView);
with this line
Code:
list = (ListView) getView().findViewById(R.id.TaskListView);
That was it!!! all the headache!!
I don't particularly understand what getView does as opposed to getActivity .. but right now I am pleased it works.
Hi Can u please provide me the code .. I an working on same thing .. bt my getView Function is not called ..
Email : [email protected]
Make sure you custom pager adapter returns all pages as per count.
Use recyclerview in fragments
Sent from my Lenovo A6000 using Tapatalk
The offscreen page limit is by default 1. It is useless to set the limit lesser equals as 1.
Code:
viewPager.setOffscreenPageLimit(0);

Categories

Resources