How to change view and where it should be placed? - android

I want to create a ListView with a "Add new record" button. I've already created custom adapter for my ListVIew, I have the form of new record (i mean the xml), I've set onClick action on button.
Now I`m wondering how to change the view and the most important question. What should happen after changing view?
For example I have onCreate function:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
NewserDatabase ndb = new NewserDatabase(this);
DataInput di = new DataInput();
ArrayList news = di.getNews();
NewsArrayAdapter naa = new NewsArrayAdapter(NewserActivity.this, R.layout.row, news);
setListAdapter(naa);
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View view,
int position, long id) {
Log.i("testy", "I Clicked on Row " + position + " and it worked!");
}
});
}
Now in what method should i manage the new view ? Should it be set in the onItemClick ? Should the activity class change ?
// edit -----------
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
NewserDatabase ndb = new NewserDatabase(this);
DataInput di = new DataInput();
ArrayList news = di.getNews();
NewsArrayAdapter naa = new NewsArrayAdapter(NewserActivity.this, R.layout.row, news);
setListAdapter(naa);
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View view,
int position, long id) {
Log.i("testy", "I Clicked on Row " + position + " and it worked!");
Intent formIntent = new Intent(NewserActivity.this, NewsAddActivity.class);
NewserActivity.this.startActivity(formIntent);
}
});
}

Related

Clickable custom array list

How can I make this custom array list clickable to go to the others activities because I tried the intents but it doesn't work
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<Names> namesArrayList = new ArrayList<Names>();
namesArrayList.add(new Names(R.drawable.call_centre, "Call Centre"));
namesArrayList.add(new Names(R.drawable.soco_academy_icon, "Academy"));
NamesAdapter NamesListAdapter = new NamesAdapter(this, namesArrayList);
ListView list = (ListView) findViewById(R.id.List_View);
list.setAdapter(NamesListAdapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
}
}
In onItemClick() you can determine which item was clicked by doing this:
Name selectedName = NamesListAdapter.getItem(position);
Then you can do whatever you want with that.

How to find the position of item in listview

I have created a Custom listView where i put some text and image but when i clicked in list view their is no response.I have created a custom Listview.
if any one known then please tell me.
my code is here
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final List<pdf> pdfList;
//the listview
final ListView listView;
//initializing objects
pdfList = new ArrayList<>();
listView = (ListView) findViewById(R.id.list);
//adding some values to our list
pdfList.add(new pdf("Book1", "First Year"));
pdfList.add(new pdf( "Book2", "First Year"));
//creating the adapter
ListAdptot adapter = new ListAdptot(this, R.layout.customlistview, pdfList);
//attaching adapter to the listview
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick (AdapterView < ? > adapter, View view, int position, long arg){
if(position==0)
{
Toast.makeText(MainActivity.this, "Position 0", Toast.LENGTH_SHORT).show();
}
else if(position==1)
{
Toast.makeText(MainActivity.this, "Position 1", Toast.LENGTH_SHORT).show();
}
}
}
);
}
}
Get list item positon click as below
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick (AdapterView < ? > adapter, View view, int position, long arg){
String value = listView.getItemAtPosition(position).toString();
}
}
);
If you have any clickable views inside your list item, then it will consume click event of your ListView item click.
Set android:focusable="false" on your Button or ImageButton if you have any.
Reference SO answer

I would like the listview item to display the data of the relevant item in new activity

Currently, the code i have only displays the data (on a new activity when clicked) at index 0 of that list view - not the item i actually clicked. i would like to somehow change my code so it can display the data at index n (n being the item i clicked in list view):
ResultsActivity.java
final ArrayList<String> searchResults = getValuesFromJSON(jsonResult);
final ListView listView = (ListView) findViewById(R.id.listView);
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>
(this, android.R.layout.simple_list_item_1, searchResults);
listView.setAdapter(arrayAdapter);
//list items become clickable and open the movie detail page which displays the movie data of the specific movie
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent newActivity = new Intent(ResultsActivity.this, MoviePage.class);
newActivity.putStringArrayListExtra("movie data", searchResults);
startActivity(newActivity);
}
});
}
MoviePage.java
ArrayList<String> searchResults = getIntent().getStringArrayListExtra("movie data");
TextView t = (TextView)findViewById(R.id.textViewfinal);
t.setText(searchResults.get(9));
Just set a onclick listener on your listview so you can listen for item click events.
Put this in your onCreate method in your activity:
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
//you can move the textview to a class variable if you want
TextView t = (TextView)findViewById(R.id.textViewfinal);
t.setText(searchResults.get(position));
}
});
update: you'll want to pass the position to the next activity the same way your passing the searchResults to MoviePage so just do this:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent newActivity = new Intent(ResultsActivity.this, MoviePage.class);
newActivity.putStringArrayListExtra("movie data", searchResults);
newActivity.putExtra("position",position);//pass the position to the next activity
startActivity(newActivity);
}
});
now in MoviePage.java
ArrayList<String> searchResults = getIntent().getStringArrayListExtra("movie data");
int position = getIntent().getIntExtra("position",0);
TextView t = (TextView)findViewById(R.id.textViewfinal);
t.setText(searchResults.get(position));
Try this :
ResultsActivity.java
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent newActivity = new Intent(ResultsActivity.this, MoviePage.class);
newActivity.putExtra("movie data", searchResults.get(position));
startActivity(newActivity);
}
});
MoviePage.java
Bundle extras = getIntent().getExtras();
if (extras != null) {
TextView t = (TextView)findViewById(R.id.textViewfinal);
t.setText(extras.getString("movie data"));
}

Android listview setOnItemClickListener

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_campaign_list);
Campaign campaign_data[] = new Campaign[]
{
new Campaign(R.drawable.hlbb, "MSIG HLBB PA Extra"),
new Campaign(R.drawable.hlbb, "MSIG HLBB SSP Plus")
};
CampaignAdapter adapter = new CampaignAdapter(this, R.layout.listview_header_row, campaign_data);
listView1 = (ListView)findViewById(R.id.listView1);
listView1.setAdapter(adapter);
listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String item = ((TextView)view).getText().toString();
Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
}
});
}
I can run the code above without setonitemclicklistener function, when i add the setOnItemClickListener, it stopped working.What is the error?? I'm new in Android..Thx
I don't know how is your CampaignAdapter code, but you are trying to cast the row view which you are inflating into TextView. You should use your adapter to access the item using the position of element:
CampaignAdapter adapter = new CampaignAdapter(this, R.layout.listview_header_row, campaign_data);
listView1 = (ListView)findViewById(R.id.listView1);
listView1.setAdapter(adapter);
listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Campaign item = (Campaign) adapter.getItem(position);
// Now you can access to the campaing value that you want
// For instance, item.getText()
Toast.makeText(getBaseContext(), item.getText(), Toast.LENGTH_LONG).show();
}
});

listView item click event not firing

I am having an issue with using a listview that when I click it nothing is happening. I would like for it when I click the listview item to make a toast so I know it is being clicked. I have been trying/researching for a while and nothing. Would anybody mind taking a look to see if I am missing something I just am overlooking? Many thanks in advance!
Here is my class:
public class MyCar extends Activity {
/**
* Called when the activity is first created.
*/
public ListView mylistView;
String carInfo;
private ArrayAdapter<String> mylistAdapter;
ArrayList<String> arrayListCar = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mycar);
mylistView = (ListView) findViewById(R.id.listView);
arrayListCar = new ArrayList<String>();
//Just had to remove setting this adapter 2 times. Took out line below to fix.
mylistView.setAdapter(mylistAdapter);
mylistView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String item = ((TextView) view).getText().toString();
Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
}
});
}
#Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
carInfo = trySomethin();
fillList();
}
public void fillList() {
String make = "";
String model = "";
String[] pieces = carInfo.split("\"");
make = pieces[3];
model = pieces[7];
ArrayList<String> carList = new ArrayList<String>();
carList.add(make + " " + model);
// Create ArrayAdapter using the car list.
mylistAdapter = new ArrayAdapter<String>(MyCar.this, android.R.layout.simple_list_item_single_choice, carList);
mylistView.setAdapter(mylistAdapter);
mylistAdapter.notifyDataSetChanged();
}
}
if you have any elements on listview item change this for them
android:focusable="false"
and if you are changing any elements visibility on runtime you have to handle focus programatically each time you change its visibility.
Try this
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item index
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) listView.getItemAtPosition(position);
// Show Alert
Toast.makeText(getApplicationContext(),
"Position :"+itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG)
.show();
}
});

Categories

Resources