Created
October 12, 2015 18:37
-
-
Save miaodonghan/ca594b4a5fdef2cfe296 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.miaodonghan.hw3_listview; | |
import android.app.Activity; | |
import android.app.AlertDialog; | |
import android.content.DialogInterface; | |
import android.content.Intent; | |
import android.content.pm.LabeledIntent; | |
import android.os.Bundle; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.view.View; | |
import android.widget.AdapterView; | |
import android.widget.ListView; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
import android.widget.SimpleAdapter; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
public class MainActivity extends Activity { | |
int global_position=0; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
final ListView listView = (ListView) findViewById(R.id.listView); | |
SimpleAdapter adapter = new SimpleAdapter(this, getData(), | |
R.layout.item, new String[]{"img", "text", "n"}, new int[]{R.id.img, R.id.t, R.id.n}); | |
listView.setAdapter(adapter); | |
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { | |
@Override | |
public void onItemClick(AdapterView<?> arg0, View view, int position, long id) { | |
onClickItem(view,position); | |
} | |
}); | |
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { | |
@Override | |
public boolean onItemLongClick(AdapterView<?> arg0, View view, int position, long id) { | |
//Toast.makeText(MainActivity.this,listView.getItemAtPosition(position).toString(), Toast.LENGTH_LONG).show(); | |
global_position = position; | |
longClickItems(view, global_position); | |
return true; | |
} | |
}); | |
} | |
public final static String selected_ID = "will keep this ID"; | |
String selected = "will keep this ID"; | |
//create onClick Event | |
public void onClickItem(View v,int position) { | |
Intent intent = new Intent(MainActivity.this, Item1Class.class); | |
//intent.putExtra(selected_ID, String.valueOf(position)); | |
Map<String,Object> o = (HashMap<String,Object>)getData().get(position); | |
selected = o.get("text").toString(); | |
intent.putExtra(selected_ID, selected); | |
startActivity(intent); | |
} | |
//create OnLongClick Event | |
public void longClickItems(View v, int position){ | |
final int p = position; | |
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); | |
alertDialogBuilder.setMessage("are you sure you really want to delete item" + position + "?"); | |
alertDialogBuilder.setNegativeButton(R.string.negative, | |
new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
/* | |
Intent negativeActivity = new Intent(getApplicationContext(), com.example.miaodonghan.hw3_listview.MainActivity.class); | |
startActivity(negativeActivity); | |
*/ | |
} | |
}); | |
alertDialogBuilder.setPositiveButton(R.string.positive, | |
new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface arg0, int arg1) { | |
Intent positveActivity = new Intent(getApplicationContext(), com.example.miaodonghan.hw3_listview.MainActivity.class); | |
Map<String, Object> o = (HashMap<String, Object>) getData().get(p); | |
List<Map<String, Object>> list1 = new ArrayList<Map<String, Object>>(); | |
o.remove("text"); | |
o.remove("img"); | |
getData().remove(o); | |
startActivity(positveActivity); | |
} | |
}); | |
AlertDialog alertDialog = alertDialogBuilder.create(); | |
alertDialog.show(); | |
} | |
private List<Map<String, Object>> getSubData(int p){ | |
getData(); | |
List<Map<String, Object>> list1 = new ArrayList<Map<String, Object>>(); | |
Map<String, Object> map = new HashMap<String, Object>(); | |
list1 = getData(); | |
return list1; | |
} | |
private List<Map<String, Object>> getData() { | |
List<ListItemData> t = new ArrayList<ListItemData>(); | |
t.add(new ListItemData("Innovation distinguished between a leader and a follower", R.drawable.g)); | |
t.add(new ListItemData("People don\'t know what they want until you show it to them.", R.drawable.yahoo)); | |
t.add(new ListItemData("The only way to be truly satisfied is to do what you believe is great work.", R.drawable.f)); | |
t.add(new ListItemData("That\'s been one of my mantras -- focus and simplicity.", R.drawable.apple)); | |
t.add(new ListItemData("Simple can be harder than complex: You have to work hard to get your thinking " + | |
"clean to make it simple.", R.drawable.amozon)); | |
t.add(new ListItemData("But it\'s worth it in the end because once you get there, you can move mountains.", R.drawable.ms)); | |
t.add(new ListItemData("Great things in business are never done by one person, they\'re done by a team of people.", R.drawable.girl2)); | |
t.add(new ListItemData("You can\'t connect the dots looking forward; you can only connect them looking backward.", R.drawable.cat)); | |
t.add(new ListItemData("Your work is going to fill a large part of your life", R.drawable.dog)); | |
t.add(new ListItemData("I\'m the only person I know that\'s lost a quarter of a billion dollars in one year.", R.drawable.ding)); | |
t.add(new ListItemData("It\'s more fun to be a pirate than join the Navy.", R.drawable.t)); | |
t.add(new ListItemData("Everything is possible.", R.drawable.nike)); | |
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); | |
for (ListItemData li : t) { | |
Map<String, Object> map = new HashMap<String, Object>(); | |
map.put("img", li.getImageId()); | |
if(li.getT().length()>32) { | |
map.put("text", li.getT().substring(0, 32)+"..."); | |
}else{ | |
map.put("text", li.getT()); | |
} | |
list.add(map); | |
} | |
return list; | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.menu_main, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
// Handle action bar item clicks here. The action bar will | |
// automatically handle clicks on the Home/Up button, so long | |
// as you specify a parent activity in AndroidManifest.xml. | |
int id = item.getItemId(); | |
//noinspection SimplifiableIfStatement | |
if (id == R.id.action_settings) { | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment